aspose file tools*
The moose likes Servlets and the fly likes how to invoke applet from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to invoke applet from servlet" Watch "how to invoke applet from servlet" New topic
Author

how to invoke applet from servlet

Sagar Surana
Greenhorn

Joined: Dec 23, 2000
Posts: 21
Happy New Year Everybody...
i have a small problem in folloeing code
code
-----------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import Test2;
public class testsrevlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
//Always pass the ServletConfig object to super class
super.init(config);
}

//Process the Http Get request
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException , IOException {

doPost(request, response);
}

//Process the Http Post request
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException , IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Enumeration parameters = request.getParameterNames();
String param = null;
if(parameters.hasMoreElements() ) {
param = (String)parameters.nextElement();
out.println("<Applet code=Test.java height=113 width=115>");
out.println("<param name=item value= " + param + ">");
out.println("</Applet>");

out.println("</body></html>");
}
out.close();
}

//Get Servlet information
public String getServletInfo() {
return "ex1 Information";
}
}
----------------------------------------------
in the above code the applet is not getting invoked properly
only the background is seen in the browser
please help


------------------
Sagar Surana
optimist


Sagar Surana<BR>optimist
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
The reason is that the browser is going to request the applet class files using a URL based on the servlet address!!!
This is also the reason that <img tags in servlet generated pages frequently don't work.
The best solution I know is to put a <base> tag in the <head> area of your generated page - this will tell the browser to request class files, image files, etc etc. from a URL that the web server knows.
<base href="http://myserver/application/" >
for example
Bill

------------------
author of:
Sagar Surana
Greenhorn

Joined: Dec 23, 2000
Posts: 21
thanku Bill,
is it possible to pass parameters from servlet to applet continuously
i mean to say if i have a application in which i have a servlet which gets the value to person logged on and now i eant to pass it on to the applet to see the persons logged on is it possible. I also want the applet to be refreshed automatically
so that all of them who are connected to site can see members logged on u can say as good as chat room
but the applet should also be invoked from the servlet

thanku


------------------
Sagar Surana
optimist
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Yes applets and servlets can communicate easily. Our host here at Javaranch has developed some classes for this which are pretty cool. Unfortunately I can't remember exactly where they are on this site so you will have to dig.
Since the basic pattern of a servlet is to respond to a request with a response, the easiest thing is to have the applet query the servlet for new data.
Bill

Chandrasekaran Sathasivam
Greenhorn

Joined: Dec 29, 2000
Posts: 11
Hello Mr.Bill,
Regarding ur explanation about base tag, could u explain how to specify href attribute tag value if my applet class file is available in the directory say c:\servlet.
I've tried like this
<base href=c:\servlet>
But the browser doesn't load the applet class file and says that class file not found.
I'm very happy if u will explain the solution for this problem.
With regards
Chandru
ray bond
Ranch Hand

Joined: Oct 11, 2000
Posts: 111
instead of writing c:\servlets try c:/servlet ,
hope this works
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
I do not know if this help but I managed to invoke an applet within one of the servlet I developed. Goto http://www.mycgiserver.com/servlet/smallalley.smallcon.jshitcounter.userlogin to have a look at the application.
At the bottom of the application is the code for the applet. To put the applet in my servlet, I did what like what you have done:-
out.println("<APPLET......"> and so on.
If you can see the counter at the bottom, it means the applet is working from within the servlet.
Chandrasekaran Sathasivam
Greenhorn

Joined: Dec 29, 2000
Posts: 11
Hai guys,
Thanks for ur response. But both the replies didn't help me. I've specified the path like c:/servlet. But browser says the same error that class file not loaded.
For the second reply the link http://www.mycgiserver.com/servlet/smallalley.smallcon.jshitcounter.userlogin hasn't connected.
Could any body help me? What happen Mr.Bill?
With regards
Chandru
ray bond
Ranch Hand

Joined: Oct 11, 2000
Posts: 111
the code that you have posted above in it you have written ,
<applet code=test.java..... you should write test.class,
am i right.
Sagar Surana
Greenhorn

Joined: Dec 23, 2000
Posts: 21
hello,
sorry to say but
the way u specified < Applet code=...
in the servlet doesn't work with the browser instead it is better to invoke a html file for the applet from the servlet

------------------
Sagar Surana
optimist
Sagar Surana
Greenhorn

Joined: Dec 23, 2000
Posts: 21
Another small problem,
i did connect applet and servlet invoking the html file from servlet for applet and then Forming URL connection with the servlet from applet to write and read objects
my problem is that i am trying to pass a object( ofcouse serialized) which contains a java.awt.List object as its field
the problem is when i do
connection.getInputStream(); i get EOFException for io
and it returns null
i don't get the problem
and could anybody explain me
setRequestProperty( String key, String value)
what does it do and what key and value mean in it....
preety urgent..
thnaku
------------------
Sagar Surana
optimist
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
Sometimes www.mycgiserver.com goes down. This is a free service so there is nothing much I could complain about.
Try the link again.
With the servlet, it should look something like:-
out.println("<applet code=\"jshitcounterapplet.class\"\n");
out.println("codebase=\"http://www.mycgiserver.com/~smallalley/smallcon/jshitcounter/"\n");
...............
........
Am not sure what you are trying to say in your last posting. Good luck.
Chandrasekaran Sathasivam
Greenhorn

Joined: Dec 29, 2000
Posts: 11
Hai,
Consider the following coding. It's concerned with calling an applet from servlet.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Helloser extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html><head><h1>Hello World</h1>");
out.println("<base href=\"c:\\servlet\"></head>");
out.println("<applet code=hello.class height=400 width=400>");
out.println("</applet></html>");
out.close();
}
}
Here hello.class is the compiled code of the hello.java applet file, which is available in the directory c:\servlet. I've used servletrunner, jsdk application for this program. I've specified the address to see the page in browser like,
http://localhost:8080/servlet/Helloser
it calls the Helloser servlet and while loading hello applet class, it says that hello class not found. Here Helloser.java is the servlet file name available in the c:\servlet directory.
What is wrong in the above code? Please explain.
With regards
Chandru
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
Try placing the applet somewhere else (eg. directory where the html files are served) and not in the directory where the servlets reside.
Chandrasekaran Sathasivam
Greenhorn

Joined: Dec 29, 2000
Posts: 11
Hai,
I've done what u say. ie., placing applet file in another directory. But still the same problem exists.
I've put my applet in c:\javasrc dir. and change the href attribute of base tag as c://javasrc.
Could u help me.
With regards
Chandru
Mark Leong
Ranch Hand

Joined: Dec 11, 2000
Posts: 60
Lets take it step by step.
First, the applet should be test.class, not test.java.
Can you invoke the applet from a html page (not from servlet)?
Try placing the applet in the default directory where the html pages are served out from your server.
Can other applets work or the problem is common to all applets on the server?
Try restoring the default setting for your server and see if problem is still there.
Have fun.
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
hai
its simple just try giving the url of the applet in the codebase parameter . try the following example

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Helloser extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html><head><h1>Hello World</h1>");
out.println("<applet <b>codebase=\"d:\\jcp_samples\" </b> code=AppletTest.class height=400 width=400>");
out.println("</applet></html>");
out.close();
}
}

where code base ia where applet resides,

HTH
sunil.s
------------------
"Winners don't do different things
They do things differently"


"Winners don't do different things<br /> They do things differently"
sunilkumar ssuparasmul
Ranch Hand

Joined: Dec 13, 2000
Posts: 142
in the codebase <b> and </b> was given to highlight codebase it has got nothing to do .
Chris Klu
Greenhorn

Joined: Jan 03, 2001
Posts: 18
This problably won't solve you problem, but you really should put a <BODY> tag after </HEAD>. Don't forget </BODY>.
Glen Tanner
Ranch Hand

Joined: Apr 16, 1999
Posts: 147

The chapter that Mr. Brogden refers to is at: http://javaranch.com/samps/2809ch12.pdf
Chandrasekaran Sathasivam
Greenhorn

Joined: Dec 29, 2000
Posts: 11
Hay,
Thanks all guys. I've got the result for calling applet from servlet. It perfectly works by placing codebase attribute with applet tag, that what sunilkumar told. Once again I thank to all guys response my request.
With regards
Chandru
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
Thanks from my side for all guys, as the above discussion helped me a lot in my current project.
Kudos to JavaRanch,
Rajesh Hegde
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: how to invoke applet from servlet