views:

202

answers:

1

Hi, I want to pass parameters betweeen applet and jsf components So when a value of a input textbox changed, its binding backing bean makes connection to a servlet. The servlet create an attribute and save to HttpSession using (request.getSession(true)).setAttribute(name, value);

Then at some event, applet will access another servlet. This servlet will try to retrieve the Attribute saved to Session previously.

However, everytime, the attirbute returned is null as the new session is created instead.

My question is: Is the session should be persist? ( I checked allowcookies, session timeout for weblogic)

If yes, what might go wrong with my app?

Thanks a lot for your help.

Regards K.

A: 

Sessions are backed by cookies. In a JSP/Servlet environment the cookie name is jsessionid. To access the same session, the applet has to fire a request with the desired session cookie in the header. Also, you need to ensure that the servlet is running/listening in the same domain and context.

To start, pass the session ID as a parameter to the applet:

<param name="jsessionid" value="${pageContext.session.id}">

Then, in the Applet connect the Servlet as follows:

String jsessionid = getParameter("jsessionid");
URL servlet = new URL(getCodeBase(), "servleturl");
URLConnection connection = servlet.openConnection();
connection.setRequestProperty("Cookie", "jsessionid=" + jsessionid);
// ...

Here servleturl obviously should match servlet's url-pattern in web.xml. This should give the same session back in the servlet on request.getSession().

BalusC
Hi Balus, thanks for your help again :)I tried that. Set the request property "Cookie" for both the servletConnection in applet and one in the backing bean to another servlet. I check the jsessionid set in both case are the same. But in both servlet connection, the session created for the request are new with different session Id. (I checked using session.IsNew())
Khue Vu
Then it's running in different domain and/or context. Did you check that as well?
BalusC
I think it might be the reason. The applet is embeded in a jsf page. The path to access the jsf is http://host:port/webapp/faces/page.jspx while the codebase path of the applet is http://host:port/webapp/applet.class But it is strange that between consecutive request to servlet from the backing bean (request is invoked everytime the input textbox value changed) the new session also created.
Khue Vu
The JSF path is not relevant. Your problem is the applet-servlet communication. What context is the servlet running in?
BalusC
The context of servlet is the same as the applet host:port/webapp/servleturl. As in my app there are two processes involved: applet-servlet communication and backing bean-servlet communicaiton. Both creates new session when it requests servlet.
Khue Vu