tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Johnny Kewl" <j...@kewlstuff.co.za>
Subject Re: How to read file content from servlet?
Date Mon, 14 May 2007 09:08:02 GMT
Teh... you kinda got the right idea... but I think the question is wrong 
thats why u getting no responses.

You cant get a servlet to call an applet....
but I have a feeling what you really asking is how to get a response from 
the servlet.
This stuff you should find in a google so I'm going to let you do that 
first... if you get stuck, I'll show you how to do it...
Search for how to POST to a servlet... google!
Can combine the search with HTTPClient or URLConnection.... you'll find lots 
of articles

So wot you do is post a parameter that says.... fileToGet=MyGameFile.game
Then remember that just like a form... a servlet wants to respond... so in 
the servlet you do something like this....

               if the parameter is MyGameFile....... then......


                File x = new File(PathToGameFile);

//So your applet knows when its got the whole file.... maybe you can send 
this as a long instead and just read first first long as file length.
                response.setContentLength((int)x.length());


                OutputStream out = response.getOutputStream(); //Create a 
stream
                FileInputStream in = null;
                try {
                    in = new FileInputStream(fileName);
                    int c; //Change this to a buffer if more speed 
required... unlikely.
                    while ((c = in.read()) != -1) { out.write(c);}
                } finally {
                    if (in != null) { in.close();   }
                }
                 out.close();

Should get u going......

----- Original Message ----- 
From: "Teh Noranis Mohd Aris" <teh_noranis@yahoo.com>
To: <users@tomcat.apache.org>
Sent: Monday, May 14, 2007 5:57 AM
Subject: How to read file content from servlet?


> Dear All,
>
>  I have an applet that sends data name, score and ScoreFile to a servlet 
> that get these data to be saved in a file. My question is, how can I send 
> these data from the servlet to the applet (reversed) so that I can use 
> only the data name in processing? Please help. Thank you.
>
>  Yours Sincerely;
>  TEH
>
>  The applet is as follows:
>
>  URL servletUrl = null;
>  URLConnection con;
>  String servletName = "http://localhost:8080/examples/servlet/ServerSide";
>  try {
>  servletUrl = new 
> URL(servletName+"?name="+name+"&score="+score+"&ScoreFile="+ScoreFile);
>  con=servletUrl.openConnection();
>  con.setUseCaches(false);
>  BufferedReader buf = new BufferedReader(new 
> InputStreamReader(con.getInputStream())); }
>  catch(Exception e) {
>  System.out.println("Exception caught..."+e); }
>
>  The servlet is as follows:
>
>  String name = request.getParameter("name");
>  int score = Integer.parseInt(request.getParameter("score"));
>  String ScoreFile = request.getParameter("ScoreFile");
>
>  String fileName = "C:/temp/"+ScoreFile;
> FileWriter resultsFile = new FileWriter(fileName,true);
> PrintWriter toFile = new PrintWriter(resultsFile,true);
> toFile.println(name+" "+score);
> toFile.close();
>
>
>
>
> ---------------------------------
> Park yourself in front of a world of choices in alternative vehicles.
> Visit the Yahoo! Auto Green Center. 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message