Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

java - Accessing JSF session scoped bean from servlet which is called by applet embedded in JSF webapp

I need to access a session scoped bean from a servlet. I already tried

UserBean userBean = (UserBean) request.getSession().getAttribute("userBean");

as described in this post. But I only get null as result, altough an instance of UserBean is alreay instatiated. Those are the annotations/imports I use for userBean :

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
 ... }

Some Background why I can't get rid of the servlet : I have a file upload applet in my jsf page. This applet expects an adress where it can send it's POST request. (I can't edit this post request to add more fields or something). The post method of my servlet then stores the file. This job can't be done by a managed bean because the servlet has to be annotated with @MultiPartConfig and I can't add this annotation to the jsf managed bean.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If it returns null, then it can only mean 2 things:

  1. JSF hasn't created the bean beforehand yet.
  2. The applet-servlet interaction doesn't use the same HTTP session as the webapp.

Given the way how you described the functional requirement, I think it's the latter. You need to ensure that you pass the session identifier of the webapp along with the HTTP request from the applet as well. This can be in the form of the JSESSIONID cookie or the jsessionid URL path attribute.

First, you need to tell the applet about the session ID the webapp is using. You can do it by passing a parameter to <applet> or <object> tag holding the applet

<param name="sessionId" value="#{session.id}" />

(the #{session} is an implicit JSF EL variable referring the current HttpSession which in turn has a getId() method; you don't need to create a managed bean for that or so, the above line of code is complete as-is)

which can be retrieved in the applet as follows:

String sessionId = getParameter("sessionId");

You didn't describe how you're interacting with the servlet, but assuming that you're using the standard Java SE URLConnection for this, pointing to the @WebServlet("/servleturl") servlet, then you can use setRequestProperty() to set a request header:

URL servlet = new URL(getCodeBase(), "servleturl");
URLConnection connection = servlet.openConnection();
connection.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);
// ...

Alternatively, you can also pass it as a URL path attribute:

URL servlet = new URL(getCodeBase(), "servleturl;jsessionid=" + sessionId);
URLConnection connection = servlet.openConnection();
// ...

(please note that the case matters in the both cases)

Either way, this way the applet-servlet interaction will take place in the same HTTP session as JSF managed beans.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...