• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Applet Communication

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shiva Selvadurai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic