Link to home
Create AccountLog in
Java

Java

--

Questions

--

Followers

Top Experts

Avatar of dr_alinaeem
dr_alinaeem

Http Session tracking
hi,

could someone help me with a simple session tracking servlet...? i have a table in sql that holds user names and passwords, and i want to begin a new session for them when they login successfully.

i have seen many shopping cart examples but i do not know how to make a even simpler one just for logging in.

any sample codes would be very appreciated...thanks...

:)

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of zzynxzzynx🇧🇪


SOLUTION
Avatar of Mick BarryMick Barry🇦🇺

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of dr_alinaeemdr_alinaeem

ASKER

thank you for your replies... :o)

...'objects'...i understand the part about beginnning a session and putting attributes into it, i maanged to do this in a servlet....however, i am having problems with using the same session across two servlets....

this is the coding for the first servlet  'login'...

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.servlet.http.HttpSession.*;

public class login extends HttpServlet{
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException{

String user_name=req.getParameter("ex_user_name");
String pass=req.getParameter("ex_pass");

PrintWriter out = res.getWriter();

String details = null;

    try
    {
        details = function1(user_name,pass);
    }
    catch (SQLException e) {}

    if (details == null)
    {
        out.println("Your user name or password is incorrect");
    }
    else
    {
         HttpSession sess = req.getSession(true);
        sess.setAttribute("userName", details);
        out.println(details);
        out.println("<HTML><BODY><FORM ACTION='/servlet/read' METHOD='POST'>");
        out.println("<input type='submit'></form></body></html>");*/

    } // end of details condition

} // end of doPost


public String function1(String user_name2,String pass2) throws SQLException
{

    String db_info = null;
    String url="jdbc:postgresql://********";
    String dbuser="***";
    String dbpass="*****";

try{
    Class.forName("org.postgresql.Driver");
    Connection conn=DriverManager.getConnection(url,dbuser,dbpass);

    Statement smt = conn.createStatement();
   
    String query="select * from CUSTOMER"+
                 " where USER_NAME = '"+user_name2+"' and CUST_PASS = '"+pass2+"'";
   
    ResultSet rs = smt.executeQuery(query);

    if (rs.next())
    {
        db_info = rs.getString("CUST_NAME1");
     }

    conn.close();
}
catch (Exception e)
    {
        System.err.println("Exception: " + e + "\n" + e.getMessage());
        e.printStackTrace();
    }
    return db_info;

}

}

on this servlet, the user name prints out fine.....then in another servlet, called 'read'...this is the coding...


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.http.HttpSession.*;

public class read extends HttpServlet{
        public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{

PrintWriter out = response.getWriter();

HttpSession sess = request.getSession();

String foo = (String) sess.getAttribute(userName);
       
out.println(foo);


}

}


but the 'read' servlet just prints out 'null' not the user name.....please help...i have also tried using getValue() instead of getAttribute but no luck... :o(

dr_alinaeem

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


ASKER CERTIFIED SOLUTION
Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.

Avatar of Mick BarryMick Barry🇦🇺

Do you have cookies enabled in your browser?
If not you'll need to use URL rewriting to handle session tracking.
Java

Java

--

Questions

--

Followers

Top Experts

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.