Total members 10262 | Gratitudes |It is currently Thu May 24, 2012 9:43 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: help for making web pages using servlets
PostPosted: Tue Jan 20, 2009 6:23 pm 
Offline
Newbie
User avatar

Joined: Tue Jan 20, 2009 5:33 pm
Posts: 1
Has thanked: 0 time
Have thanks: 0 time

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?


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: help for making web pages using servlets
PostPosted: Tue Jan 20, 2009 10:12 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 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>");
}
}

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

 Similar topics
 multi-frameset and non-re-sizable html frame pages
 How to design these JSP pages?
 material of jsp and servlets
 connection is not making
 Muddled pages
 Data Access Object in Servlets.............
 Making calculator in JAVA
 How can I enable session tracking for JSP pages if the brows
 More Servlets and JavaServer Pages
 Making fantasy game?

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team