Switch to full style
HTML,DHTML,Javascript,XML,CSS
Post a reply

help for making web pages using servlets

Tue Jan 20, 2009 6:23 pm

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?



Re: help for making web pages using servlets

Tue Jan 20, 2009 10:12 pm

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>");
}
}


Post a reply
  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     -  
 How can I enable session tracking for JSP pages if the brows     -  
 JavaFX Program to display IE Favorites web pages     -  
 multi-frameset and non-re-sizable html frame pages     -  
 material of jsp and servlets     -  
 How does JSP differ from Servlets?!!!     -  
 Displaying Images using servlets     -