Avatar of dr_alinaeem
dr_alinaeem

asked on 

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...

:)
Java EEJava

Avatar of undefined
Last Comment
Mick Barry
Avatar of zzynx
zzynx
Flag of Belgium image

Avatar of rk_radhakrishna
rk_radhakrishna

SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of dr_alinaeem
dr_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
ASKER CERTIFIED SOLUTION
Avatar of mak415
mak415

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

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.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo