aspose file tools*
The moose likes Servlets and the fly likes how to create a session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how to create a session" Watch "how to create a session" New topic
Author

how to create a session

Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

package fastlearn;

import java.io.*;
import java.util.*;
import java.net.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;


/**
*
* @author RITU
*/
public class login extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String eml=request.getParameter("fclognm");
String p=request.getParameter("fcpwd");
String user=request.getParameter("fcuser");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbcdbc:dbfastl earn");
PreparedStatement ps=con.prepareStatement("Select * from LogDetails where User_email='"+eml+"' and User_pwd='"+p+"'and User_Access='"+user+"'");
ResultSet rs=ps.executeQuery();
int count=0;
while(true){
if(rs.next()){
if(user.equals("admin")) {
RequestDispatcher dr=request.getRequestDispatcher("Admnstr.html");
dr.forward(request, response);
HttpSession session=request.getSession(true);
session.setAttribute("usr", user);
session.setAttribute("eml", eml);

}
else if(user.equals("fac")) {
RequestDispatcher dr=request.getRequestDispatcher("Faculty.jsp");
dr.forward(request, response);
HttpSession session=request.getSession(true);
session.setAttribute("usr", user);
session.setAttribute("eml", eml);
}
else if(user.equals("stud")){
RequestDispatcher dr=request.getRequestDispatcher("Student.jsp");
dr.forward(request, response);
HttpSession session=request.getSession(true);
session.setAttribute("usr", user);
session.setAttribute("eml", eml);
}
}
else {
RequestDispatcher dr=request.getRequestDispatcher("login.jsp");
dr.forward(request, response);
count++;
}
con.close();
// if(count>3){
// session.invalidate();
// out.println("<br>Sorry!! Your Account has been blocked!");
}

}


catch(Exception e){
out.println(e);
}
finally {

out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}

I want to start the session once the user successfully logged in, bt its showing null wen, on the user's page, I m printing out- session.getAttribute("eml"); I want to print out the login name instead.
Where I am goin wrong??
Is there any other way to start a session to get the result??


With regards,
rituparna
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Rituparna, Welcome to JavaRanch.
Please UseCodeTags to post your source code, that way the source code is more presentable.

In the code- You are setting the values in the Session after you are forwarding the request. This might affect the setting of the value in the Session. Though not sure about it.


Mohamed Sanaulla | My Blog
T Jung
Greenhorn

Joined: Feb 08, 2011
Posts: 4

What about skiping all the database releated stuff to check wether it is the servlet stuff that is not working?
like:


May be you should first set the session attribute and perform the RequestDispater.forward after that. Like in the code above. In your code you invoke the forward and then set the attribiute.
Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

true, i've tried that. once i set the attribute and session made true bfore forwarding the request, first time its showing bt 2nd time on it jst showing or smthng like the previous logged user's logname!!
should i use session context to store data??
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Rituparna Duttagupta wrote:true, i've tried that. once i set the attribute and session made true bfore forwarding the request, first time its showing bt 2nd time on it jst showing or smthng like the previous logged user's logname!!
should i use session context to store data??

I havent been able to understand the above reply. Can you be elaborate? or might be if someone else understood the question.

And also Please UseRealWords - Not 'bt', 'bfore' 'jst', 'smthng'- It would be difficult to comprehend.

Rituparna Duttagupta
Ranch Hand

Joined: Feb 08, 2011
Posts: 55

ok. actually, i put request.getSession(true); before RequestDispatcher.forward() then, on the first execution i get the desired result, but after that every execution gives out either null, or the login name of another user, who have logged in earlier, say if i log in as farruk for the first time. but after that if i log in as manisha, i am getting either null or farruk!
why is this happenning?
should i store the values in ServletContext, instead of HttpSession?? will that work?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

You can try using getSession() instead of getSession(true)

From the API:
Java API wrote: HttpSession getSession() - Returns the current session associated with this request, or if the request does not have a session, creates one.
HttpSession getSession(boolean create) - Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to create a session
 
Similar Threads
regarding session transfer from servlet to jsp
Problem in running session beans
Httpsesion Listener is not working properly?
java.lang.NullPointerException
logout.jsp not working