Reputation: 1321
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);
%>
Upvotes: 0
Views: 16062
Reputation: 647
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}
Upvotes: 1