Access to a JSF session limited by a bean from a servlet that is called by an applet built into the JSF webapp

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

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

as described in this post. But I only get null as a result, although the UserBean instance is deprecated. These are the annotations / imports that I use for userBean:

 import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class UserBean implements Serializable{ ... } 

Some information Why I can not get rid of the servlet: I have an applet for downloading files on my jsf page. This applet expects an address where it can send a POST request. (I cannot edit this mail request to add more fields or anything else). Then the post method of my servlet saves the file. This task cannot be completed using a managed bean because the servlet must be annotated using @MultiPartConfig, and I cannot add this annotation to a managed jsf bean.

+4
source share
1 answer

If it returns null , then this can only mean two things:

  • JSF has not created a bean yet.
  • The applet-servlet interaction does not use the same HTTP session as webapp.

Given how you described the functional requirement, I think this is the last. You need to make sure that you pass the webapp session ID along with the HTTP request from the applet. This can be in the form of a JSESSIONID cookie attribute or a JSESSIONID URL.

First you need to tell applets about the session identifier that webapp uses. You can do this by passing a parameter to the <applet> or <object> while holding the applet

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

( #{session} is an implicit JSF EL variable that references the current HttpSession , which in turn has a getId() method; you do not need to create a managed bean for one or the other, the above line of code is completed as-is)

which can be found in the applet as follows:

 String sessionId = getParameter("sessionId"); 

You have not described how you interact with the servlet, but provided that you use the standard Java SE URLConnection for this, pointing to the @WebServlet("/servleturl") servlet, then you can use setRequestProperty() to set the request header:

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

Alternatively, you can also pass it as an attribute of the URL path:

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

(note that the case matters in both cases)

In any case, this way the applet-servlet interaction will occur in the same HTTP session as the JSF beans.

+6
source

Source: https://habr.com/ru/post/1443635/


All Articles