Sponsered Links
Categories
Sponsered Links

Servlet Session Tracker Example

 

This is a simple session tracker example of servlet.

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

public class SessionTracker extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        HttpSession session = req.getSession(true);
        Integer cnt = (Integer)session.getValue("tracker.cnt");
        if (cnt == null)
            cnt = new Integer(1);
        else
            cnt = new Integer(cnt.intValue() + 1);
        session.putValue("tracker.cnt", cnt);
        pw.println("<HTML><HEAD><TITLE>SessionTracker</TITLE> </HEAD>");
        pw.println("<BODY><H1>Session Tracking Demo</H1>");
        pw.println("You've visited this page " + cnt + ((cnt.intValue() == 1) ? " time." : " times."));
        pw.println("<P>");
        pw.println("<H2>Here is your session data:</H2>");
        String[] names = session.getValueNames();
        for (int i = 0; i < names.length; i++) {
            pw.println(names[i] + ": " +    session.getValue(names[i]) + "<BR>");
        }
        pw.println("</BODY></HTML>");
    }
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved