Results 1 to 6 of 6
  1. #1
    Rituparna is offline Member
    Join Date
    Feb 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Exclamation 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??

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  3. #3
    Rituparna is offline Member
    Join Date
    Feb 2011
    Location
    India
    Posts
    22
    Rep Power
    0

    Default 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!!!

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Java Code:
    dr.forward(request, response);
    HttpSession session=request.getSession(true);
    session.setAttribute("usr", user);
    session.setAttribute("eml", eml);
    You're forwarding and then setting the values in the session. Consequently those values are not set during the processing of the JSP.
    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.

  6. #6
    Rituparna is offline Member
    Join Date
    Feb 2011
    Location
    India
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Starting Out...
    By maknib in forum Java Gaming
    Replies: 1
    Last Post: 11-11-2010, 08:15 PM
  2. Replies: 1
    Last Post: 04-21-2009, 11:20 PM
  3. Replies: 1
    Last Post: 03-10-2009, 01:12 PM
  4. Replies: 2
    Last Post: 12-23-2008, 06:35 PM
  5. just starting
    By specbailey in forum New To Java
    Replies: 23
    Last Post: 08-13-2007, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •