Total members 11892 |It is currently Fri Sep 20, 2024 7:35 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





how to make web pages using servlets and html pages. how to do session tracking, work with cookies, and invoke another pages in it?

can u give me any simple example of codding to understand these things?




Author:
Newbie
User avatar Posts: 1
Have thanks: 0 time

1. Example on session tracking , is the tracking of use login overall your site , the session is tracked using the HttpSession class , session is used to save data at the server side for a limited time period .

Code:
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionTracker extends HttpServlet
{
  public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
  {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    HttpSession session = req.getSession(true);

    String user = (String) session.getAttribute("username");
    String content=" " ;     
     
    if (user == null) {
       content="<a herf='http://www.mysite/login/'>Login</a>";
    } else {
       content="Welcome "+user+" , <a herf='http://www.mysite/logout/'>Logout</a> ";
    }

     
    out.println("<html><head><title>Site main page</title></head>");
    out.println("<body><h1>Login/Logout session tracking </h1>");
    out.println(content);// Show a link for login or logout
    out.println("</body></html>");
  }
}


There is functions with the session object that may help you alot ,
- To get session ID
Code:
session.getId();


- Get the max time of session to be expired
Code:
session.getMaxInactiveInterval();


- Get the session created Time
Code:
session.getCreationTime();


2 .Cookies are the instances that are saved at the client side browser to keep information to be used for the next visits .In servlets , the cookies are sent using the request object .We will check if an cookie is already created otherwise we will create one . Here for example i will save the username in a cookie .

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

public class SettingandReadingCookies extends HttpServlet
{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
       
        out.println("<HTML><HEAD><TITLE>WEB_PAGE</TITLE></HEAD><BODY>");

        Cookie[] cookies = request.getCookies();
        boolean Cookiethere = false;

        for(int i = 0; i < cookies.length; i++) {
            Cookie currentCookie = cookies[i];
            if (currentCookie.getName().equals("username")) {
                out.println("username = " + currentCookie.getValue());
                Cookiethere = true;
            }
        } 

        if (!Cookiethere) {
            Cookie newcookie = new Cookie("username", "msi_333");
           
            response.addCookie(newcookie); // Add Cookie at client
        }

         
        out.println("This page show your username when reloaded.");
        out.println("</BODY>");
        out.println("</HTML>");
}
}


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : help for making web pages using servlets
 Making calculator in JAVA     -  
 Making Colors Transparent with imagecolortransparent()     -  
 How to design these JSP pages?     -  
 Mapping JSPs pages     -  
 JavaFX Program to display IE Favorites web pages     -  
 How can I enable session tracking for JSP pages if the brows     -  
 multi-frameset and non-re-sizable html frame pages     -  
 material of jsp and servlets     -  
 How does JSP differ from Servlets?!!!     -  
 Servlets how to upload Files     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com