Java Forum / General / August 2006
save input from web form to a .dat file
Nancy.Nicole@gmail.com - 11 Aug 2006 15:25 GMT I'm trying to get the input from the user to a .dat file that I can sort, read, etc. internally. However, I am having problems with DataAccessor.load() --> It throws an exception...perhaps part of the problem?
Here is the method from the class that is the applet:
public int save(String agency, String producer, String insured, String effectiveDate, String G, int territory, int GSlim, int GSLpayroll, String GKLLtype, int vehLim, int GKlim, int GKdeduct, String MPcoverage, int MPlimit, String form, int protcls, String construct, String replacement, int BLim, int BCim) { QuoteEntry onlineEntry = new QuoteEntry(); int quoteNumb = 0; try{ entryList = DataAccessor.load(); } catch(Exception e) { EntrySorter entryList = new EntrySorter(); } // end tryentryList = DataAccess.load();
quoteNumb = entryList.sortByQuoteNum(); quoteNumb = quoteNumb + 1; onlineEntry.insured = insured; onlineEntry.quoteNum = String.valueOf(quoteNumber); onlineEntry.effectiveDate = effectiveDate; onlineEntry.producerID = producer; onlineEntry.agency = agency; onlineEntry.state = G; onlineEntry.territory = String.valueOf(territory); onlineEntry.gkllSelect = GKLLtype; onlineEntry.gkllDeduct = String.valueOf(GKdeduct); onlineEntry.gkllLimit = String.valueOf(GKlim); onlineEntry.gkllVehLimit = String.valueOf(vehLim); onlineEntry.gslPayroll = String.valueOf(GSLpayroll); onlineEntry.gslLimit = String.valueOf(GSlim); onlineEntry.mpSelect = MPcoverage; onlineEntry.mpLimit = String.valueOf(MPlimit); onlineEntry.propReplacement = replacement; onlineEntry.construct = construct; onlineEntry.propForm = form; onlineEntry.protectClass = String.valueOf(protcls); onlineEntry.blLimit = String.valueOf(BLim); onlineEntry.bcLimit = String.valueOf(BCim); StringTokenizer tok = new StringTokenizer(note.getText(), "\n"); for(int i=0; i < QuoteEntry.maxNotes; i++){ onlineEntry.notes[i] = ""; } // end for try{ while(tok.hasMoreTokens()){ onlineEntry.notes[counter++] = tok.nextToken(); } } catch(ArrayIndexOutOfBoundsException e){ quoteNumb = -1; } entryList.addElement(onlineEntry); try { DataAccessor.save(entryList); } catch(Exception e) { quoteNumb = 0; } try{ entryList = DataAccessor.load(); } catch(Exception e) { EntrySorter entryList = new EntrySorter(); } // end tryentryList
return quoteNumb; } // end save
and the DataAccessor class: class DataAccessor{ static String file = "data.dat"; public static EntrySorter load()throws IOException{ boolean empty = true; String line; EntrySorter entryList = new EntrySorter(); try{ BufferedReader input = new BufferedReader(new FileReader(file)); while((line = input.readLine()) !=null && line.length() != 0){ empty = false; int counter = 0; QuoteEntry entry = new QuoteEntry(); EntryTokenizer dt = new EntryTokenizer(line); entry.quoteNum = dt.nextToken(); entry.insured = dt.nextToken(); entry.effectiveDate = dt.nextToken(); entry.producerID = dt.nextToken(); entry.agency = dt.nextToken(); entry.state = dt.nextToken(); entry.territory = dt.nextToken(); entry.gkllSelect = dt.nextToken(); entry.gkllDeduct = dt.nextToken(); entry.gkllLimit = dt.nextToken(); entry.gkllVehLimit = dt.nextToken(); entry.gslPayroll = dt.nextToken(); entry.gslLimit = dt.nextToken(); entry.mpSelect = dt.nextToken(); entry.mpLimit = dt.nextToken(); entry.propReplacement = dt.nextToken(); entry.construct = dt.nextToken(); entry.propForm = dt.nextToken(); entry.protectClass = dt.nextToken(); entry.blLimit = dt.nextToken(); entry.bcLimit = dt.nextToken(); entry.CID = dt.nextToken(); input.readLine(); while(!(line = input.readLine()).equals("$$")){ entry.notes[counter++] = line; } entryList.addElement(entry); } input.close(); } catch(FileNotFoundException e){ new File(file).createNewFile(); return null; } if(empty) return null; return entryList; }
public static void save(EntrySorter entryList)throws IOException{ FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); PrintWriter outFile = new PrintWriter(bw); for(int i=0; i < entryList.size(); i++){ QuoteEntry entry = (QuoteEntry) entryList.elementAt(i); outFile.print("*" + entry.quoteNum); outFile.print("~*"); outFile.print(entry.insured); outFile.print("~*"); outFile.print(entry.effectiveDate); outFile.print("~*"); outFile.print(entry.producerID); outFile.print("~*"); outFile.print(entry.agency); outFile.print("~*"); outFile.print(entry.state); outFile.print("~*"); outFile.print(entry.territory); outFile.print("~*"); outFile.print(entry.gkllSelect); outFile.print("~*"); outFile.print(entry.gkllDeduct); outFile.print("~*"); outFile.print(entry.gkllLimit); outFile.print("~*"); outFile.print(entry.gkllVehLimit); outFile.print("~*"); outFile.print(entry.gslPayroll); outFile.print("~*"); outFile.print(entry.gslLimit); outFile.print("~*"); outFile.print(entry.mpSelect); outFile.print("~*"); outFile.print(entry.mpLimit); outFile.print("~*"); outFile.print(entry.propReplacement); outFile.print("~*"); outFile.print(entry.construct); outFile.print("~*"); outFile.print(entry.propForm); outFile.print("~*"); outFile.print(entry.protectClass); outFile.print("~*"); outFile.print(entry.blLimit); outFile.print("~*"); outFile.print(entry.bcLimit); outFile.print("~*"); outFile.print(entry.CID); outFile.println(); outFile.println("$$"); for(int j=0; j < QuoteEntry.maxNotes; j++){ if(entry.notes[j] != ""){ outFile.println(entry.notes[j]); } } outFile.println("$$"); } outFile.close(); } }
Do you know what the problem is? I really need to figure this out and I just can't seem to.
Thanks!
Andrew Thompson - 11 Aug 2006 16:27 GMT > I'm trying to get the input from the user to a .dat file that I can > sort, read, etc. internally. However, I am having problems with > DataAccessor.load() --> It throws an exception... ..What exception? A CodeIsFormingAnIndependentRepublicException exception? Something else**, perhaps?
>...perhaps part of the problem? Exceptions, especially ones that cannot be readily explained, often are part of the problem.
> Here is the method from the class that is the applet: Is your applet signed?
> public int save(String agency, String producer, String insured, String I will trim most of your 180 lines of code snippets*, to point out that any applet that performs actions that require 'trust' will fail in an unsigned applet.
> public static void save(EntrySorter entryList)throws IOException{ > FileWriter fw = new FileWriter(file); ..
> Do you know what the problem is? I really need to figure this out and I > just can't seem to. ..and getting access to writing a file on the end user's machine is something that is only accessible to a 'trusted' applet. That applet gains this by being signed and accepted by the user.
* As an aside, please refrain from posting 180 lines of code snippets. Code snippets are usually not that helpful in solving code problems (I am simply making random guesses about a very common problem).
Further, it rarely requires 180 lines of code to express a problem. I note that in line after line of your posted snippets, there were very similar code - did you try trimming or commenting entire sections of that code to confrim that the problem perstisted?
Much more effective for communicating a problem (and shorter, in this instance) would be an SSCCE, for more explanantion and tips, read here.. <http://www.physci.org/codes/sscce/>
Just before I forget, there are a lot of places in the code where it catch's an Exception but proceeds processing. I suggest you change any code like..
try { ....do stuff } catch(Exception e) { ...do other stuff }
To..
try { ....do stuff } catch(Exception e) { e.printStackTrace(); ...do other stuff }
** Then it should be possible to give us more details on those exceptions. ;-)
HTH
Andrew T.
Nancy.Nicole@gmail.com - 11 Aug 2006 18:37 GMT > ..What exception? > * As an aside, please refrain from posting 180 lines of > code snippets. Code snippets are usually not that helpful > in solving code problems (I am simply making random > guesses about a very common problem). Uh, an IOException which was in my code...part of why I posted it.
> ..and getting access to writing a file on the end user's > machine is something that is only accessible to a > 'trusted' applet. That applet gains this by being signed > and accepted by the user. I am not attempting to read a file on the user's machine; I want to read a file located on my server.
> did you try > trimming or commenting entire sections of that code > to confrim that the problem perstisted? Of course. I know that I can read the file...but when I sort it, the program dies. What's weird is that I can perform the same function from a separate class and it works perfectly. Other than that; I've never done this before....
Oliver Wong - 11 Aug 2006 19:27 GMT >> ..What exception? >> * As an aside, please refrain from posting 180 lines of [quoted text clipped - 11 lines] > I am not attempting to read a file on the user's machine; I want > to read a file located on my server. Is your data accessor class running on the client or the server?
if (on the client) { You'll need to provide an URL that the client can connect to to download the file. } else { You'll need to provide a way for the client to connect to your data accessor to get the info it wants. }
- Oliver
Nancy.Nicole@gmail.com - 11 Aug 2006 20:48 GMT > Is your data accessor class running on the client or the server? > [quoted text clipped - 7 lines] > > - Oliver It is running on my server. I call the file from DataAccessor. Also, I have now included the full path to the file, but it still doesn't work...
class DataAccessor{ static String file = "L\\public\\Garage\\data.dat"; .....}
Shouldn't my class be trying to find this filepath on my server?
Thanks!
Oliver Wong - 11 Aug 2006 21:07 GMT >> Is your data accessor class running on the client or the server? >> [quoted text clipped - 18 lines] > > Shouldn't my class be trying to find this filepath on my server? Assuming "my class" refers to DataAccessor, and assuming it's running on the server, yes. (I'm not sure what OS would recognize the "L" prefix though).
However there's still the question of how the client gains the ability to communicate with this class. Sockets? HTTP? Something else?
- Oliver
Nancy.Nicole@gmail.com - 11 Aug 2006 21:24 GMT > > Shouldn't my class be trying to find this filepath on my server? > [quoted text clipped - 6 lines] > > - Oliver My applet is running inside of a web page. The information gathered from the user should be saving to the .dat file. If I run a separate program (that performs the same functions) internally, the data is saved. However, from the web page it is not captured.
Oliver Wong - 11 Aug 2006 21:31 GMT >> > Shouldn't my class be trying to find this filepath on my server? >> [quoted text clipped - 13 lines] > program (that performs the same functions) internally, the data is > saved. However, from the web page it is not captured. Applets are run on the client machine, not the server. If you want the applet running on the client machine to be able to access the file on the server, you need to provide access to that file somehow. If you're doing read-only, the easiest way is to server that file over HTTP.
- Oliver
Nancy.Nicole@gmail.com - 11 Aug 2006 23:23 GMT > Applets are run on the client machine, not the server. If you want the > applet running on the client machine to be able to access the file on the > server, you need to provide access to that file somehow. If you're doing > read-only, the easiest way is to server that file over HTTP. > > - Oliver I need to be able to write to the file using the data from the applet. If I publish the file so that I can call it from a web browser, will I be able to write to it?
Andrew Thompson - 12 Aug 2006 03:46 GMT > > Applets are run on the client machine, not the server. If you want the > > applet running on the client machine to be able to access the file on the > > server, you need to provide access to that file somehow. If you're doing > > read-only, the easiest way is to server that file over HTTP. ..
> I need to be able to write to the file using the data from the applet. > If I publish the file so that I can call it from a web browser, will I > be able to write to it? That would require some help from an application on the server. The applet sends data to the server pogram, which writes the file.
Andrew T.
Nancy.Nicole@gmail.com - 12 Aug 2006 16:51 GMT > That would require some help from an application on the server. > The applet sends data to the server pogram, which writes the file. > > Andrew T. Hmm... I actually started out doing something like that. See I have an internal database interface that I wrote so that the input I'm getting online could be modified and such. I originally tried to send my data to that program and write it from there and I wasn't doing it right I guess. Can I just call that program and method such as Class.Method()? That's what I was doing but my program would still die at that point.
Thanks.
Andrew Thompson - 12 Aug 2006 17:27 GMT > > That would require some help from an application on the server. > > The applet sends data to the server pogram, which writes the file. ....
> Hmm... I actually started out doing something like that. See I have an > internal database interface that I wrote so that the input I'm getting > online could be modified and such. I originally tried to send my data > to that program and write it from there and I wasn't doing it right I > guess. Can I just call that program and method such as Class.Method()? > That's what I was doing but my program would still die at that point. The applet cannot 'just call' methods in the application that exists on the server, if that is what you mean.
The server application requires some form of public interface that the applet accesses. It might be as simple as the applet encoding the data in the URL parameters and calling a particular servlet or PHP script.
OTOH, allowing 'the public' to update infromation on your server is a dangerous proposition - will this applet be available on the big bad internet, or a nice 'safe' little intranet?
Andrew T.
Nancy.Nicole@gmail.com - 13 Aug 2006 00:44 GMT > OTOH, allowing 'the public' to update infromation on your > server is a dangerous proposition - will this applet be > available on the big bad internet, or a nice 'safe' little intranet? > > Andrew T. Well, my applet is a quoting system. Agents will be able to log in on a secure site and get a quote for their customers. I want the quote to be saved to a database so that the underwriters at my company can see the quote for reference (it's a liability thing). On the internal side, my underwriters have an interface where they can add quotes, delete quotes, and modify quotes with notes and such. What I originally did was send the data from the quote that the agent got and sent that data to the internal interface. My internal interface has about 4 subclasses, one of which it calls to save the file each time the interface is opened. I originally sent the online data to that interface and attempted to save the quote in the same manner as one of my internal underwriters would input a quote.
Does that make any sense?
Oliver Wong - 14 Aug 2006 14:47 GMT >> OTOH, allowing 'the public' to update infromation on your >> server is a dangerous proposition - will this applet be [quoted text clipped - 16 lines] > > Does that make any sense? Security isn't easy to get right. If this is for a "real" business, and if you're not quite sure what you're doing, you should strongly consider hiring a consultant or another developer. Otherwise, you risk allowing the public unfettered access to all your business internals.
- Oliver
Nancy.Nicole@gmail.com - 14 Aug 2006 17:00 GMT I think what I need to do is create an instance of my AddressBook class in order to sort from it. However, the constructor for AddressBook throws an IOException. If I call the following:
Method save() throws Exception { quoteNumber = AddressBook.methodThatThrowsException(); }
I get errors because ActionPerformed() calls save() and apparently ActionPerformed can not throw an Exception.
If I use Try{ AddressBook ab = new AddressBook(); } catch(Exception e){ // do something here }
It always falls to the catch and my instance is never created. How do I get around this? Is this the way to solve?
Oliver Wong - 14 Aug 2006 18:16 GMT > I think what I need to do is create an instance of my AddressBook class > in order to sort from it. However, the constructor for AddressBook [quoted text clipped - 17 lines] > It always falls to the catch and my instance is never created. > How do I get around this? Is this the way to solve? The solution depends on which exception is being thrown.
Read http://java.sun.com/docs/books/tutorial/essential/exceptions/
- Oliver
Nancy.Nicole@gmail.com - 14 Aug 2006 18:38 GMT > The solution depends on which exception is being thrown. > > Read http://java.sun.com/docs/books/tutorial/essential/exceptions/ > > - Oliver Thanks. It throws an IOException
Nancy.Nicole@gmail.com - 14 Aug 2006 20:07 GMT Okay, I think I've found the problem. My applet class tries to send the data to my interface class. The interface class can work independently and can save, modify and delete entries. However, if I try to save data from the applet (passing as parameters to the interface), the program dies. If I create an instance of my interface class (which is what I am doing when I am working with it independently), my program should work....I think. However, when I try to create an instance of my interface from my applet (i.e. DataClass dc = new DataClass()), I have a problem. DataClass() the constructor throws an IOException.
If I use try{}catch(){}, it always falls to catch and if I try to throw an IOException on the method that creates an instance of DataClass(), ActionPerformed yells. ActionPerformed calls getQuote()throws Exception which calls DataClass()throws Exception. ActionPerformed cannot throw exceptions.... How in the world am I supposed to fix this? I tried to read the guide Oliver suggested, but I don't think I'm quite understanding...
Thanks!
Oliver Wong - 14 Aug 2006 21:11 GMT > Okay, I think I've found the problem. My applet class tries to send the > data to my interface class. The interface class can work independently [quoted text clipped - 13 lines] > read the guide Oliver suggested, but I don't think I'm quite > understanding... The root problem is that code running on the client's machine cannot directly invoke the methods of an object running on the server's machine. You have to provide some sort of webservice via PHP or JSP, or create a client/server architecture via sockets or RMI.
- Oliver
Nancy.Nicole@gmail.com - 14 Aug 2006 21:24 GMT > The root problem is that code running on the client's machine cannot > directly invoke the methods of an object running on the server's machine. > You have to provide some sort of webservice via PHP or JSP, or create a > client/server architecture via sockets or RMI. > > - Oliver Ohhh... Thank you very much for your help, Oliver. I hate to be such a pain, but I'm a computer science student and I've really gone above my level of expertise in the internship I've got here. Do you know where I could learn about PHP, JSP or the such so that I may fix this? I don't know much about the networking side...
Thank you so much. You have been worlds of help.
Oliver Wong - 14 Aug 2006 22:25 GMT >> The root problem is that code running on the client's machine cannot >> directly invoke the methods of an object running on the server's machine. [quoted text clipped - 8 lines] > > Thank you so much. You have been worlds of help. The PHP/JSP solution would potentially require some changes to the server. For example, if you were previously running plain vanilla Apache to serve HTTP content, you'd have to add the PHP module to Apache to enable it to process PHP pages. In other words, if you're working in company and you don't personally own the servers, you should probably get permission before proceeding down this route. Maybe their system admin will veto the PHP idea altogether, in which case you'd have to look for an alternative solution.
That said, you can find instructions on installing PHP, and a basic starter tutorial at http://www.php.net/manual/en/
From there, you can do a google search for "Learn PHP" or "PHP Tutorial" and get links like http://www.w3schools.com/php/default.asp
If you have further questions about PHP, you should probably ask in a PHP newsgroup, instead of this Java newsgroup.
- Oliver
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|