aspose file tools*
The moose likes Servlets and the fly likes Servlet Applet Communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Applet Communication" Watch "Servlet Applet Communication" New topic
Author

Servlet Applet Communication

Shiva Selvadurai
Greenhorn

Joined: Sep 13, 2005
Posts: 7
could anyone tell me how can i read a file using servlet and display it in applet , criteria is that the file to be read is a log file ( i.e) one which is executing always ,could any one get me an idea or a sample code for this problem
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
Those are two different, unrelated problems:

1) reading an open file into a servlet
If you can't open the file for reading in Java, make a copy on the OS level and read that copy. Runtime.exec can help with that.

2) transferrring data from a servlet to an applet
You can use the java.net.URL.getContent() method to access the servlet, and retrieve any data it returns.


Ping & DNS - updated with new look and Ping home screen widget
Shiva Selvadurai
Greenhorn

Joined: Sep 13, 2005
Posts: 7
hi Ulf ,
i couldn't understand what you say regarding the file reading , i got the output for reading a file using servlet and displaying it in applet , the problem which i have now is that i could not read va log file , if i try it my applet doesn't get loaded , get me a suggestion.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Refer www.coreservlets.com you will get info about how Applet and servlet can communicate.

or directly you can refer:
http://csajsp-chapters.corewebprogramming.com/CSAJSP-Chapter17.pdf
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
i got the output for reading a file using servlet and displaying it in applet , the problem which i have now is that i could not read va log file , if i try it my applet doesn't get loaded

I though I understood what you're trying to do, but apparently I don't. You're trying to read a file in a servlet, and then you want to transfer some of that information to an applet for display purposes?
Now the servlet can't read the file, and you think that leads to the applet not being initilized? If that's the problem, I think it would be best if you posted a short, relevant piece of your code that doesn't work as you expect it to. And describe in more detail what you're trying to do, because I don't quite understand your description.
Shiva Selvadurai
Greenhorn

Joined: Sep 13, 2005
Posts: 7
Ulf,
this is my code
ServletOutputStream sos = resp.getOutputStream();
FileReader fr = new FileReader("../../test.log");
BufferedReader br = newBufferedReader(fr);
String str;
while(true)
{
str = br.readLine();
if(str==null) sos.println(str);
else Thread.sleep(3000);
}



herethe aspect is when i try to read the log it could have reached eof but after sometime it some contents could have be upated for ex:
at 2:00 the last two lines may be
SPORT
-1
at 2:10 this could be modified as
SPORT
CRICKET
FOOTBALL
SOCCER
-1

i use sleep to allow time for the file being wirtten and then i read the same and send it to applet , but i couldn't get this aspect and my applet is not getting intialized.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
This isn't going to work for two reasons.
First, the connection to the applet will time out after a while if you don't send anything. Depending on how the applet connects to the server, you might be able to configure the timeout period. The java.net.URL and java.net.URLConnection classes don't allow this, but the Jakarta Commons HttpClient can do this. I can't give specifics because you don't mention how you make the applet/servlet connection.

Second, you can't use a Reader on an open file that keeps changing the way you do it. You'll need to read it, close the reader, and then after some time open it again and read the full contents of the file.

Lastly, I'm not sure why you talk about the applet initialization failing. The initialization should be done in the init() and start() methods, which should finish executing before you ever make the connection to the servlet.
Shiva Selvadurai
Greenhorn

Joined: Sep 13, 2005
Posts: 7
Hi Ulf,
the applet gets intialzied but it doesn't get loaded when i run . sorry for mistyping the aspect.The applet servlet communication takes place as follows

readServlet(){
URL url = new URL(getCodeBase(),"ViewServlet");
InputStream in = sendGetMessage(url);
DataInputStream result = new DataInputStream(in);
String line;
while((line=result.readLine())!=null){
logView.append(str);
}
result.close();
}

URL ser = null;
public InputStream sendGetMessage(URL url){
this.ser =ser;
URL url = new URL(ser.toExternalForm());
URLConnection con = url.openConnection();
return con.getInputStream();
}


this is my applet to servlet communication code is there any problems , how could i solve the problems both in applet and servlet;
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
public InputStream sendGetMessage (URL url)

This probably needs to read

public InputStream sendGetMessage (URL ser)

because "ser" is not otherwise assigned a value.

And what is "str"? You're reading data into "line".

I'm not sure why you create two URLS, but something like the following might work (unless toExternalForm does something crucial):

[ September 14, 2005: Message edited by: Ulf Dittmer ]
Shiva Selvadurai
Greenhorn

Joined: Sep 13, 2005
Posts: 7
regardin applet to servlet i don't have issue but how could i read ths log file and display the same in the applet(as and when log file is updated the applet should also be updated).
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
In my earlier post I suggested a way for the servlet to read the file. Does that not work for you for some reason?

As to the applet needing to see the updates: It will need to query the server periodically. Just one call over a connection that stays open won't do. I'd suggest coming up with a mechanism where reading the file and writing to the applet are separate from one another. The reading should happen in its own thread, and whenever the applet queries the servlet, any updates can be sent to the client.
Shiva Selvadurai
Greenhorn

Joined: Sep 13, 2005
Posts: 7
hi Ulf ,
are you asking me to do as the code below :

readFile()
{
FileReader fr = new FileReader("/test.log");
BufferedReader br = new BufferedReader(fr);
String str;
while(true){
str = br.readLine();
if(str!=null) writeApplet(str);
else Thread.sleep(1000);
}
}
ServletOutputStream sos = response.getOutputStream();
writeApplet()
{
sos.println(str);
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39543
    
  27
Absolutely not. What you have posted has the same problems all the earlier versions had. I'll say it one more time: This will not work the way you'd like it to work.

You can't keep the connection to the applet open indefinitely - the applet will need to access the servlet repeatedly.

You will need to read the file over and over, while keeping track of what you have already sent to the applet.

I'd like to help you, but you need to re-read what I posted earlier. Your proposed solution has serious flaws that cannot be overcome by simple changes like you have made.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Applet Communication
 
Similar Threads
Applet + Database
writing to a local file
applet to jsp communication
html+applet+servlet=how to do
how to invoke applet from servlet