javascript - Passing session variable data from jsp to java - Stack Overflow

I have created session variables since I want to transfer data from one JSP to another.Each of my JSPs

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);
%>

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);
%>
Share Improve this question edited Mar 9, 2013 at 4:07 Makoto 107k27 gold badges198 silver badges236 bronze badges asked Mar 9, 2013 at 3:54 Seeya KSeeya K 1,3316 gold badges29 silver badges43 bronze badges 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 – Nir Alfasi Commented Mar 9, 2013 at 3:55
  • 1 show the code in jsp which is setting the value in session pls – aksappy 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? – Seeya K 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? – aksappy Commented Mar 9, 2013 at 3:58
  • No I havent used the above what u suggested. How do I do that? – Seeya K Commented Mar 9, 2013 at 3:58
 |  Show 8 more ments

1 Answer 1

Reset to default 1

In general, just try to avoid scriptlets. You should definitely do what Makoto remended: 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}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745132537a4613042.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信