Reputation: 99
I have one jsp page in which i am getting some values from user and it is going to servlet and in that servlet i am calling some method which is available in data manager class and in that class doing some operation and finally returing to previous servlet then displaying some result on jsp page and after that suppose user going to another page then how it will make session for another request so user's id should available for another operation how to maintain session, in normal case i know how to maintain session but here i am using bean and separate class for operation so please any one can help me out here here is my code.... this one is jsp page getting user id and password here and sending to servlet
<form method="post" action="LoginServlet">
<table align="center">
<tr>
<td>Username : </td>
<td><input type="text" id="userName" name="eid" size="15" maxlength="8" /></td>
</tr>
<tr>
<td>Password : </td>
<td><input type="password" id="password" name="password" size="15" maxlength="20"/> </td>
</tr>
<td colspan="2" align="right"><input type="submit" value="Login" /></td>
</tr>
</table>
</form>
in this servlet it is calling one method of another class
public class LoginServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
try
{
HttpSession session = request.getSession(true);
UserBean user = new UserBean();
user.setEid(request.getParameter("eid"));
user.setPassword(request.getParameter("password"));
user = DataManager.login(user);
if (user.isValid())
{
if(user.getAccess_type().equals("admin"))
session.setAttribute("currentSessionUser",user);
String address ="/admin.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(address);
dispatcher.forward( request, response);
}
this one is data manager class
public class DataManager
{
static Connection currentCon = null;
static ResultSet rs = null;
static int i;
static Statement st1;
public static UserBean login(UserBean bean)
{
Statement stmt1 = null;
String username = bean.getEid();
String password = bean.getPassword();
String searchQuery ="select * from empinfo where eid='"+ username+ "' AND password='"+ password+ "'";
System.out.println("Your user name is " + username);
System.out.println("Your password is " + password);
System.out.println("Query: "+searchQuery);
try
{
currentCon = ConnectionManager.getConnection();
stmt1=currentCon.createStatement();
rs = stmt1.executeQuery(searchQuery);
boolean more = rs.next();
if (!more)
{
System.out.println("Sorry, you are not a registered user! Please sign up first");
bean.setValid(false);
}
else if (more)
{
String Name = rs.getString("eid");
String nam = rs.getString("name");
String passwrd = rs.getString("password");
String mailid = rs.getString("mailid");
String temp =rs.getString("temporaryadd");
String perm =rs.getString("permanentadd");
String qual = rs.getString("qualification");
String sx = rs.getString("sex");
String dob = rs.getString("dob");
String contact = rs.getString("contactnum");
String skills =rs.getString("skills");
String access_type = rs.getString("access_type");
String bloodgroup =rs.getString("bloodgroup");
System.out.println("Welcome " + nam);
bean.setEid(Name);
bean.setName(nam);
bean.setPassword(passwrd);
bean.setMailid(mailid);
bean.setAccess_type(access_type);
bean.setBloodgroup(bloodgroup);
bean.setDob(dob);
bean.setTemporaryadd(temp);
bean.setQualification(qual);
bean.setPermanentadd(perm);
bean.setSkills(skills);
bean.setSex(sx);
bean.setContactnum(contact);
bean.setValid(true);
}
}
now it is going back to loginservlet and will display admin page with associated value now if i will click to any other operation which are available on admin page so how this user id and other things will be available to that page so i can use it
Upvotes: 1
Views: 63
Reputation: 3450
You may simply use
UserBean user = (UserBean) session.getAttribute("currentSessionUser");
Upvotes: 1