Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to create a session

 
Rituparna Duttagupta
Ranch Hand
Posts: 55
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
T Jung
Greenhorn
Posts: 9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic