aspose file tools*
The moose likes Servlets and the fly likes how to send session info from Applet to 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 send session info from Applet to servlet?" Watch "how to send session info from Applet to servlet?" New topic
Author

how to send session info from Applet to servlet?

Chris Garrison
Ranch Hand

Joined: Jun 13, 2002
Posts: 44
I've read several examples but I am now confused.
I have a servlet forwarding control to an applet. Once that applet is complete, I want it to forward to another servlet but I don't know how to maintain the session. I'm trying include session information as a parameter from the servlet to the applet.
<APPLET code="UpdateChecker.class" codebase="<%=serverHomeURL%>actions/applet"
width="161" height="20">
<PARAM name="StatusURL"
value="<%=serverHomeURL%>actions/QueryTextStatus.jsp">
<PARAM name="ForwardURL" value="<%=serverHomeURL%>actions/result.jsp">
<PARAM name="refreshMS" value="1000">
<PARAM name="sessionID" value="<%=session.getId().toString()%>">
</APPLET>
So far, I have been able to send a string for session.getID() and I've also created a cookie which has JSESSION123456....
When I'm forwarding control from the applet to the servlet, how do I send the session info?
Thanks
[ September 18, 2003: Message edited by: Chris Garrison11 ]

May the force be with you, always.
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
In your applet you want to do a http post/get to the next servlet, and just set a "Cookie" header to the jsessionid.
Something like:

I should note that the format, name, and value of the cookie has to match
eg.
When the server sets a cookie for the browser, it will issue a header such as this:
Set-Cookie: MyCookie=1234567890; Path=/mydomain.com; ...
The browser must then also set a header when communication back to the server, such as:
Cookie: MyCookie=1234567890

So if you try some code and it doesn't appear that session info is being preserved, make sure your applet is formatting the "Cookie: " header with correct name and value.
[ September 18, 2003: Message edited by: James Swan ]
Chris Garrison
Ranch Hand

Joined: Jun 13, 2002
Posts: 44
Thanks for your time and effort, James. I really appreciate it.
That's sorta' the road I've been working on. I'll keep the cookie stuff in mind. Thanks for the heads up.
One question:
I think the problem might lie in the fact that I'm simply trying to forward control to the servlet rather than get some value or parameter, etc back from it.
In other words, once the applet stops doing it's thing it forwards control.
getAppletContext().showDocument(new URL(forward));
I'm not really wanting anything back from the servlet. Do I still benefit from using the HttpURLConnection class???

For a better understanding of what I'm trying to do...
The first servlet is pretty much the final step in a process of building a query. The query is submitted for processing and the servlet (the first servlet) forwards to the applet. The applet spins and counts the number of results returned. Once the query reaches "complete" status, it forwards to the final servlet which displays the results. (the applet is basically cosmetic)
When the second servlet finally is "called," I have to somehow tell it THIS is the particular session I want you to display results for. Right now when the second servlet is called is doesn't know which one I want so I guess it's creating a new one.
If that isn't confusing enough, I'm doing all of this for eventual use in a portlet so I'm already using HttpURLConnection in my controlling servlet to get the output of the servlets I'm talking about above.
I sure wish I was smarter.
[ September 18, 2003: Message edited by: Chris Garrison11 ]
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Hi Chris,
ok, I get you now. What I posted prior isn't really relevant to your problem.
From what I understand, the forwarding mechanism:
getAppletContext().showDocument(new URL(forward));
isn't preserving your cookie (ie session info).
I'm kind of grasping for ideas now (haven't really done before, what you're trying to do).
idea 1:
- figure our how to call a javascript method from your applet, this method would play around with "document.cooky" (note rename cooky to cookie) and see if setting the cookie here effects the forward command issued by your applet

idea 2:
- configure your appserver to accept session info rewritten in the URL
- eg. depending on your appserver, your call to your servlet might look something like:
getAppletContext().showDocument(new URL("http://myserver.com/MyServlet?sessionid=1234567890&etc..."));

idea 3:
- ditch the applet (it's too hard)
Chris Garrison
Ranch Hand

Joined: Jun 13, 2002
Posts: 44
idea #3 is my favorite. But I don't think anyone is going to go for it.
Thanks for your time.
I'll keep trying.
 
Don't get me started about those stupid light bulbs.
 
subject: how to send session info from Applet to servlet?