Question subject: help for making web pages using servlets
Posted: Tue Jan 20, 2009 6:23 pm
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?
msi_333
Question subject: Re: help for making web pages using servlets
Posted: Tue Jan 20, 2009 10:12 pm
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 .
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=" " ;
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 .