Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session in jsp

 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I wish to know how to put session. In a way once a user logs in the session must end only when the user logs out. Like the one used in this forum once I log in all my posts are visible but when loged out non of them are visible.

Thank you in advanece
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, not really the type of thing to be doing from a JSP. Maintaining state and other types of processing is best done in servlet controllers.

That said, using the session is easy. You can obtain the session from the request instance passed to a servlet and use the setAttribute() and getAttribute() methods to create and fetch scoped variables.

To track logins, place a scoped variable in the session when a user successfully logs in, and remove it when he or she logs out. The presence of the scoped variable indicates that the user is logged in.

If the session times out, the scoped variable is automatically removed even if the user doesn't remember to log out.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In servlet Controller use .
This will create a session for you. If the session already exists then it will be assigned to session object here. Otherwise the controller will create one for you. You need not check whether the session exists or not. Every thing is handled by the container itself.

Once you logout you can use

Otherwise you can mention the session time out using .
Here you have to mention it in form of seconds. i.e. for 1 hour you have to mention it as 60*60. For each and every session you create you can specify the time.
If you don't want to mention it each and every time, you can set it in the application level in the deployment descriptor.



In the deployment descriptor you mention the time in minutes. Here 60 means 60 minutes.

Hope this may help you. Have a nice day.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick response.

In my case I have 1st jsp onsubmit of which it will takes to the 2nd jsp, on submit of the 2nd jsp, I am redirecting it to the 2nd jsp again. The session value passed in the 1st jsp must be valid till I close the browser. Is it posible. Am new to java . Please help.


Thank you in advance.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi Lohi wrote:Thank you for the quick response.

In my case I have 1st jsp onsubmit of which it will takes to the 2nd jsp, on submit of the 2nd jsp, I am redirecting it to the 2nd jsp again. The session value passed in the 1st jsp must be valid till I close the browser. Is it posible. Am new to java . Please help.


Thank you in advance.


Hi Mahi, can you explain it clearly
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am accepting user in put in say A.jsp and in Action class am inserting the user's input into the DB and passing the one of the user input in session to B.jsp.

In B.jsp user has to fill some fields and select yes or no to continue. If the user selects yes then in B.jsp's action class am inserting some values to the DB and redirecting it to B.jsp.

Now the session value passed to B.jsp at the first by A.jsp must be carried again.

Hope am clear

Thank you in advance
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi, one thing you must remember, a session once created will have a time out, it depends on the programmer, if the programmer doesn't specify the session time out then the container will take care of it.
Not all the containers will take care about session time out, a few will do for the programmers, for example tomcat will invalidate the sessions after 30 minutes default. If you want to handle it you ca. If you want the session not to time out use . It means session will never expire.

Normally people won't do this. You said that the session must never expire until the browser window is open. We cannot know how much time a user will keep his browser open. Each user has is own way.

The session expires only if the user is idle for 30 minutes or the specified time. Take a scenario, you have specified that the session time out is 60 minutes, a user is idle for 59 minutes, then the user has clicked some link, now after 1 minute the session wont expire, again its life is extended to 60 minutes. If the user performs an action after 60 minutes, since the session is expired the user can't see any result.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do see my post above your

Hope you dont mind
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need not bother about the session, you can again use it in B.jsp even though you did not forward the session from B.jsp to B.jsp.
The session is present at the application level until the session time outs.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
geting error of the kind "java.lang.NumberFormatException: For input string: "ordr (value_in_session)"
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahi Lohi wrote:geting error of the kind "java.lang.NumberFormatException: For input string: "value_in_session"


Please paste your code.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Action Class

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String out_name=request.getParameter("outlet");
String salemane=request.getParameter("salename");
SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
Date now = new Date();
String strDate = sdfDate.format(now);
Class.forName("com.mysql.jdbc.Driver");
String url=("jdbc:mysql://localhost:3306/warehouse?"+"user=root&password=123456");
Connection con=DriverManager.getConnection(url);
ResultSet rst=null;
PreparedStatement stm = con.prepareStatement("insert into order_intake (outlet_name,sales_man,order_date) values(?,?,?)");
stm.setString(1, out_name);
stm.setString(2, salemane);
stm.setString(3, strDate);
stm.executeUpdate();
ResultSet rs=null;
PreparedStatement stmtk = con.prepareStatement("update orde set ord_no=ord_no+1");
stmtk.executeUpdate();


return mapping.findForward(SUCCESS);

}
}

Success forward will direct to


Action class for the above JSP


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpSession;
/**
*
* @author Mahi
*/




 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please format the code using ''code button''
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brother I am sorry to say that I don't know anything about struts. Some code phrases are confusing me.
 
Mahesh Lohi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your interest in solving my problem.

Happy Weekend

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic