0

I have created session variables since I want to transfer data from one JSP to another. Each of my JSPs is a tab. I transfer data given from the user from one JSP to another using the request body. I have <form>..><button type = "submit"/> </form> in the last JSP from where I submit the data and use it in a Java class.

When I try to access the session data from all the JSP pages, it returns a null value for all the data.

How should I transfer all the session data from all the JSPs to a Java class? Please note that each JSP is a tab, and I submit data from the last JSP.

Code:

<% 
    String joindate = request.getParameter( "joindate" );
    session.setAttribute("joindate",joindate);
    String birthdate = request.getParameter( "birthdate" );
    session.setAttribute("birthdate",birthdate);
%>
13
  • 1
    How do you set the session variable ? how do you try to read it ? without posting some code it's difficult to understand the source of the problem Commented Mar 9, 2013 at 3:55
  • 1
    show the code in jsp which is setting the value in session pls Commented Mar 9, 2013 at 3:55
  • <% String joindate = request.getParameter( "joindate" ); session.setAttribute( "joindate",joindate); String birthdate = request.getParameter( "birthdate" ); session.setAttribute( "birthdate",birthdate); %> This is a sample code of how I set session variables. I mean this is how I had done. Is it right? Commented Mar 9, 2013 at 3:56
  • And how is session variable set here? Did you use HttpSession session = request.getSession() or a servletcontext implementation? Commented Mar 9, 2013 at 3:58
  • No I havent used the above what u suggested. How do I do that? Commented Mar 9, 2013 at 3:58

1 Answer 1

1

In general, just try to avoid scriptlets. You should definitely do what Makoto recommended: use a MVC pattern by having your JSPs submit their data to a servlet, which does the heavy duty lifting.

Whenever you have a form in a JSP, consider using

<form action="servletpattern?action=add_date" method="post">

Then in the servlet:

 HttpSession session = request.getSession();
 String action = request.getParameter("action");
 if(action.equals("add_date")){
     String joindate = request.getParameter( "joindate" );
     session.setAttribute("joindate",joindate);
     String birthdate = request.getParameter( "birthdate" );
     session.setAttribute("birthdate",birthdate);
 } else if(action.equals("someotheraction"){
     //do something else
 }

In the JSP you should again not use scriptlets but instead access the session variables through EL :

join date: ${joindate}, birth date: ${birthdate}
Sign up to request clarification or add additional context in comments.

10 Comments

zz3599 : Thanks a lot. I will try this out. in my java class where I am trying to access the variables I am extending MVCPortlet class . Now for using session I will need to extend HttpSession class too right? But java doesn't support multiple inheritance. so how do i go about this? create another separate java class? I haven't worked with servlets so I am getting confused
Is it possible to get the variable values without using httpsession? what i mean is is there any other way?
What do you mean? You are getting the variables from the request, and then setting in them in the session. If you want your variables to persist across multiple JSP's (in your case) then you want to set it in the session.
Yes thats right. so i think i will have to use the approach that u suggested. but i need to extend mvcportlet class and if now that m using session i need to use httpsession class. how do i extend both these classes as java doesnt support multiple inheritance? m confused now
No, you do not need to extend httpsession class. Just import javax.servlet.http.Httpsession
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.