i/o from jsp to a file (via creating an object)
Hello everyone,
I created 3 JSP pages: 1 - login, 2-displays 4 checkboxes with a submit, 3-displays the info submitted and also creates a file to store this information. I have a separate StudentElective.java where I created an object.
page.3 jsp looks like this:
Code Java:
<% String knum = (String) session.getAttribute("knum");
String mycourse = (String) session.getAttribute("course");
String[] id = request.getParameterValues("id");%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Page 3</title>
</head>
<body>
<h3>Thank you, <%=knum%>!</h3>
You have selected
<%
int i;
for (i = 0; i < id.length; i++) {
out.print(id[i]);
if (i == 0) {
out.print(" and ");
}
}
%>!
<br>
Your application is now registered!
<% StudentElective se = new StudentElective();
se.setKnum(knum);
se.setCourse(mycourse);
se.setElchoice(id);
try {
File DATAFILE = new File("c:/Users/k000000/Documents/studentData.dat");
if (!DATAFILE.exists()) {DATAFILE.createNewFile();}
FileOutputStream DATA = new FileOutputStream(DATAFILE);
ObjectOutputStream SAVE = new ObjectOutputStream(DATA);
SAVE.writeObject(se);
} catch (Exception X) {
System.out.print("\n\tFile Access Error. Could not SAVE.");
}
%>
</body>
</html>
My object file:
Code Java:
public class StudentElective implements java.io.Serializable {
private String knum,
course;
private String[] elchoice;
public StudentElective() {
}
public StudentElective(String knum, String course, String[] elchoice) {
this.knum = knum;
this.course = course;
this.elchoice = elchoice;
}
public String getCourse() {
return course;
}
public String[] getElchoice() {
return elchoice;
}
public String getKnum() {
return knum;
}
public void setCourse(String course) {
this.course = course;
}
public void setElchoice(String[] elchoice) {
this.elchoice = elchoice;
}
public void setKnum(String knum) {
this.knum = knum;
}
public void print() {
System.out.print(this.knum);
System.out.print(this.course);
System.out.print(this.elchoice);
}
}
I have 2 questions:
1. At the moment everytime I write new information into a file, my old info is gone.How can I modify my code so everytime I write a new info into a file it will be saved onto a new line. In other words, how can I open my file for multiple entries everytime new instance of object gets created?
2.I made an error somewhere when I was trying to pass selected checkboxes to an array in my object. Everytime I read the info from file created it shows something like
[Ljava.lang.String;@5456a499
instead of the names of my checkboxes.Where I made this mistake and how to fix it?
Thank you in advance for any help possible.
Re: i/o from jsp to a file (via creating an object)
Quote:
Originally Posted by
Panoksa
...
1. At the moment everytime I write new information into a file, my old info is gone.How can I modify my code so everytime I write a new info into a file it will be saved onto a new line.
Here's the thing: ObjectOutputStream objects are not designed to be appendable. Reference the ObjectOutputStream API
I mean, you can open the underlying FileOutputStream in appendable mode, but when you create an ObjectOutputStream object the constructor immediately writes a new Serializable header to the stream, which results in a corrupted stream at that point (the end of the old file) when you append things and then, later, try to read in the new stuff.
At least that what I observed. (I'm new to Java, and I would welcome any enlightenment here. It wouldn't be the first time I overlooked something that is "easy and obvious" to anyone with more than four months experience.)
I keep thinking that they could have made the ObjectInputStream readObject() "smarter," to get past this, but maybe that wasn't a good idea either. Maybe there is a Good Reason why things are as they are.
Anyhow...
Maybe you can read all of the previous stuff from the file and then create a brand new file and ObjectOutputStream and write the old stuff and then the new stuff. You can write as many objects as you want in a single session with a single new file, right? (By packaging the objects in a collection, I'm thinking. Maybe an ArrayList or some such thing?)
There is some discussion about a way to create appendable ObjectOutputStream objects is here: http://stackoverflow.com/questions/1...38141#12438141 I haven't tried it (because, for one thing, I couldn't think of a bullet-proof way of knowing how much stuff to read), but those guys always seem to be really on top of things.
Quote:
Originally Posted by
Panoksa
2....Everytime I read the info from file created it shows something like
System.out.println(se.getElChoice());
.
.
[Ljava.lang.String;@5456a499
Your object's elchoice is an array of Strings. If you use something like System.out.println(arrayName) for any kind of array, you get the name of the array and a hashcode. To print the contents of an array in a single print statement, you can use the Arrays static toString() method:
(You might want to change the print() method in your StudentElective class to something like this also.)
Cheers!
Z