#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    15
    Rep Power
    0

    sessions in servlets


    im a novice java programmer,,

    i developing a servlet which takes in username and password and logs a user onto a database..

    im trying to uses sessions in my servlet but i keep getting an error saying that HttpSession is an unresolved symbol. wots the problem,,

    here us all the servlet code,,is not pretty but it works,,,well apart from the sessions

    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;


    // You need to import the java.sql package to use JDBC
    import java.sql.*;

    public final class LoginServlet1 extends HttpServlet
    {

    public void doGet(HttpServletRequest request,HttpServletResponse response)
    {
    String username = null;
    String password = null;
    String strHTML = "";
    boolean success = false;
    Connection conn=null;

    username = request.getParameter("Username");
    password = request.getParameter("Password");

    if (((username==null) || (username.compareTo("")==0)))
    {
    strHTML = strHTML + "<title> Lab #11 Login Error </title>";
    strHTML = strHTML + "</head>";
    strHTML = strHTML + "<body bgcolor=white>";
    strHTML = strHTML + "Massive Error --- you need to provide a username ...please try again ...<BR><BR>";
    strHTML = strHTML + "Back to the login page ..<A HREF=\"../login.html\"> click here </A>";
    strHTML = strHTML + "</body>";
    strHTML = strHTML + "</html>";
    success = false;
    }
    else if (((password==null) || (password.compareTo("")==0)))
    {
    strHTML = strHTML + "<title> Lab #11 Login Error </title>";
    strHTML = strHTML + "</head>";
    strHTML = strHTML + "<body bgcolor=white>";
    strHTML = strHTML + "Massive Error --- you need to provide a password ... please try again ...<BR><BR>";
    strHTML = strHTML + "Back to the login page ...<A HREF=\"../login.html\"> click here </A>";
    strHTML = strHTML + "</body>";
    strHTML = strHTML + "</html>";
    success = false;
    }
    else
    {
    try
    {
    // Load Oracle driver
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    // Connect to the database
    String dbaddress ="jdbcracle:thin:@macha.wit.ie:1521:MACH";
    conn = DriverManager.getConnection (dbaddress, username,password);
    strHTML = strHTML + "connection successfull ....<BR><BR>";


    success= true;

    }
    catch (SQLException sqlex)
    {
    strHTML = strHTML + "connection failed ....<BR>";
    if (sqlex.getErrorCode()==1017)
    {
    strHTML = strHTML + "OUPS !!! wrong username or password lads ...please try again ...<BR><BR>";
    }
    else
    {
    strHTML = strHTML + "SQL Error happened ....<BR>";
    strHTML = strHTML + "Error Number = " +sqlex.getErrorCode() + "<BR>";
    strHTML = strHTML + "Error Message = " + sqlex.getMessage() + "<BR>";
    strHTML = strHTML + "ah common cheer up, it's not the end of the world...<BR>";
    }
    strHTML = strHTML + "Back to the login page<A HREF=\"../login.html\"> click here </A>";
    success = false;
    }

    }

    if (success=true)
    {

    try
    {
    HttpSession login=request.getSession(true);
    login.getValue("username");
    login.getValue("password");
    getServletConfig().getServletContext().getRequestDispatcher("/jsp/query.jsp").forward(request,response);

    }

    catch (Exception ex) {

    ex.printStackTrace ();

    }



    }
    }}
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    138
    Rep Power
    12
    Not sure if the code you have given has any parts removed. If it does then this could explain the problem. If not it may still prevent some problems in the future.

    In the code you gave I didn't see where strHTML was actually output.

    From the javadoc refrence

    To make sure the session is properly maintained, you must call
    this method before the response is committed.
    So try moving your get session up near the top of the doGet
    so you can make sure that a you haven't written out anything to the response yet.
    --

    ngibsonau
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Location
    europe
    Posts
    287
    Rep Power
    14
    i'm learning java too so i dont know much about that but
    i dont think this

    if (success=true)

    is a good idea at all
    could this be the reason for ur errors?
    cheers, jens
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    138
    Rep Power
    12

    Talking


    yeah "if(success = true)" should be "if(success == true)"
    --

    ngibsonau
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    NE Pa.
    Posts
    96
    Rep Power
    14
    Actually folks, why are we comparing a boolean to true??? It's a boolean right? So its either true or false right? The proper way to code this is

    if(success) {

    }

    There is absolutely no reason to do if (success == true)

    Also I have to wonder, why is your class declared as final.....Im not sure you want to do this in a servlet unless you have some special reason?

    Lastly I have to agrre with ngibsonau 1000%. NEVER, & I mean NEVER get the session after any response work is done. Dont even set the content type or get a PrintWriter before getting the session. I make it a habit to get the session the VERY FIRST LINE of any servlet I need it in. Think of the steps this way & you'll never go wrong:

    1. Get any incoming variables, including sessions
    2. do whatever it is you need to do with the incoming data
    3. Do your output.
    Last edited by DC Dalton; March 7th, 2003 at 10:53 AM.
    DC Dalton
    DCD Designs
    SCJP
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    15
    Rep Power
    0

    Unhappy thanks


    when i said i was a novice i meant a complete novice, this is somebodies elses code that we were given to adjust to use sessions, not only do i not have a clue about sessions the same is true of java, could somebody tell me exactly where to put in the session elements,

    all i want to do with this is register the username and password as session variables.

    thanks for any help....

IMN logo majestic logo threadwatch logo seochat tools logo