-1

I am using Java (JSP and Servlets) but am stuck. From my research I believe I might need a session level object

The user completes a JSP form, presses submit, and it is POST to the class NewServlet.

Inside NewServlet an object NewFileThing is created, where this information is stored.

    @WebServlet(urlPatterns = {"/NewServlet"})
    public class NewServlet extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

     // Stuff happens here...
String nameOfUser = request.getParameter("name");

    NewFileThing fi = NewFileThing();
    fi.addName(name);

    response.sendRedirect("nextForm");
    }
    }

The user is then redirect to nextForm.jsp. When they complete this form they are sent to FinalServlet.

@WebServlet("/FinalServlet")
@MultipartConfig
public class FinalServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO how can I access object fi (NewFileThing) from here? 
fi.getName(); <--- Error, or if I make new object it's NULL...
}
}

Question: Within FinalServlet, how can I get the same object NewFileThing I created earlier (same instance)? I do not want to create a new one!

Thank you lovely community!

securityauditor
  • 293
  • 3
  • 13

3 Answers3

0

If this is a simple webapp, then 'yes', store your object in the HttpSession.

If this is a more complex webapp, then you can store the object in the HttpSession, but you will need to be 'smarter' about how you do it. Or, consider using a persistent store of some sort.

hooknc
  • 4,854
  • 5
  • 31
  • 60
0

Yes, one way is to store that object in the session.

In NewServlet

NewFileThing fi = NewFileThing();
fi.addName(name);

// get the session
HttpSession session = request.getSession();
session.setAttribute("NewFileThing", fi);

In FinalServlet

// get the session
HttpSession session = request.getSession();

// get object from session
NewFileThing fi =  (NewFileThing) session.getAttribute("NewFileThing");

// Make sure it is on the session
if (fi != null)
{
    String name = fi.getName();
}
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
  • Is there a limit to the size of what can be put into HttpSession? – securityauditor May 22 '16 at 18:22
  • This will really depend on the memory resources of the server. You can definitely store a good amount in there, but I wouldn't get too crazy. You also have to consider how many concurrent users there will be. Try to keep the session as minimal as you can. If you need to store larger objects, then look into other forms of persistent storage (e.g. files, database tables, etc.) – Michael Markidis May 22 '16 at 18:32
  • java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute with name fi. I've googled solution but do not understand: You can make the object serializable by using a serializable List implementation and making sure that the objects in the list are also serializable. – securityauditor May 22 '16 at 18:40
  • Short answer is that the limit is the JVM. In your case you are probably ok if it's a fairly simple web app. I would also look into concurrency and state fullness considerations (here is a start: http://stackoverflow.com/questions/616601/is-httpsession-thread-safe-are-set-get-attribute-thread-safe-operations) – Michael Markidis May 22 '16 at 18:41
  • You are probably running in a J2EE container that requires the object to be serializable. Either make the object serializable or just store the individual attributes of the NewFileThing object in the session. Test this out by just storing the name in the session and not the whole object. You should be able to put a String in the session. Just test that out first. – Michael Markidis May 22 '16 at 18:47
  • Still a little lost about the serialisable stuff sorry. – securityauditor May 22 '16 at 18:48
  • 1
    Serialization is another topic all together. I wouldn't worry about that right now. Just see if you can store a single String in the session in the first servlet and access that string in the final servlet. – Michael Markidis May 22 '16 at 18:50
  • It allows me to send Strings etc... but not the object sadly. Is there a solution as I really need to send the object, not a String. – securityauditor May 22 '16 at 18:52
  • On the class definition for NewFileThing add implements Serializable so you will have `public class NewFileThing implements Serializable {` – Michael Markidis May 22 '16 at 19:09
  • It works thank you! I read about serializable before from a Java book but it slipped my mind. – securityauditor May 22 '16 at 19:40
0

response.sendRedirect() will not retain your request data. You have to use RequestDispatcher class. There are multiple ways to transfer data in multiple class. You can put it in page scope, request scope, session scope or application scope according to your need. In page scope your object data will retain in same page only. In request scope your object will retain upto next immediate page. In session scope your object will retain in multiple pages in same application same session time. In application scope your object can travell into multiple applications also.

Most commonly used scopes are request and session.

Session must be used with extra care it can messup with other session if not used properly.

In your case it seems like there are multiple jsp and servlets where object needs to travell before its being used. So better to put in session. And after use remove it from session. So messup chances are less.

In first servlet put object in session

NewFileThing fi = NewFileThing();
fi.addName(name);

// get the session
HttpSession session = request.getSession();
session.setAttribute("NewFileThing", fi);

Use it where you want // get the session HttpSession session = request.getSession(); // get object from session NewFileThing fi = (NewFileThing) session.getAttribute("NewFileThing");

// Make sure it is on the session
if (fi != null)
{
    String name = fi.getName();
}
Akash
  • 74
  • 3