Results 1 to 6 of 6
Thread: Session starting
- 02-09-2011, 06:21 AM #1
Member
- Join Date
- Feb 2011
- Location
- India
- Posts
- 22
- Rep Power
- 0
Session starting
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("jdbc:odbc: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??
- 02-09-2011, 07:20 AM #2
First You must use special tag for formatting code. About your question. You have null, when you show need param because you to do dr.forward(request, response); all parameters which you wrote in session cleaned.
Skype: petrarsentev
http://TrackStudio.com
- 02-09-2011, 08:38 AM #3
Member
- Join Date
- Feb 2011
- Location
- India
- Posts
- 22
- Rep Power
- 0
thanx,
well, bt wen i m creating d the session by puting request.getSession(true); before forward(request, response), it's working for te 1st time bt frm 2nd time on... its again showing null, if refreshed its showing the value!!!
- 02-09-2011, 09:26 AM #4
It is properly. You read Java Doc about methods getSession(boolean)? There say that Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session. when you do forward you clear all parametric in session. You can create yourself SessionContext implements Serializable, SessionContextInterface and save need data there.
Skype: petrarsentev
http://TrackStudio.com
- 02-10-2011, 06:47 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You're forwarding and then setting the values in the session. Consequently those values are not set during the processing of the JSP.Java Code:dr.forward(request, response); HttpSession session=request.getSession(true); session.setAttribute("usr", user); session.setAttribute("eml", eml);
As a general rule a forward should be the last thing you do.
Forwarding does not clear the session, by the way. It would be particularly useless if it did.
- 02-11-2011, 05:51 AM #6
Member
- Join Date
- Feb 2011
- Location
- India
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Starting Out...
By maknib in forum Java GamingReplies: 1Last Post: 11-11-2010, 08:15 PM -
How to Kill session at application context level by using session Id
By Kishore.Kumar in forum Java ServletReplies: 1Last Post: 04-21-2009, 11:20 PM -
[SOLVED] Access to default session deniedAccess to default session denied
By jazz2k8 in forum NetworkingReplies: 1Last Post: 03-10-2009, 01:12 PM -
How do I create session for a user when S/he login and expire the session.
By dpk_vash in forum Web FrameworksReplies: 2Last Post: 12-23-2008, 06:35 PM -
just starting
By specbailey in forum New To JavaReplies: 23Last Post: 08-13-2007, 11:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks