Tue Jan 20, 2009 6:23 pm
Tue Jan 20, 2009 10:12 pm
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>");
}
}
session.getId();
session.getMaxInactiveInterval();
session.getCreationTime();
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>");
}
}
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
Powered by phpBB © phpBB Group.