Skip to main content

Session Tracker Servlet

 

JSP Page:index.jsp

<%-- 
    Document   : index
    Created on : Feb 28, 2012, 3:32:07 PM
    Author     : dell_pc
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Access Servlet by servlet's Name!</h1>
        
        <a href="SessionTrackerServlet">SessionTrackerServlet</a><br>
        <a href="enumerationServlet">enumerationServlet</a><br>
    </body>
</html>

 


Servlet:SessionTrackerServlet.java

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
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;
/**
 *
 * @author dell_pc
 */
public class SessionTrackerServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet SessionTrackerServlet at " + request.getContextPath() + "</h1>");
out.println(" <form name=\"form\" action=\"SessionTrackerServlet\" method=\"POST\">");
out.println("<BR><input type=\"submit\" value=\"Session Tracker \" />");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
    @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
/*
 * Get the no. of time access this page, and store value into
 * client's session under the name session.active
 */
HttpSession session = request.getSession();
String string;
Integer count;
synchronized (session) {
count = (Integer) session.getAttribute("session.active");
if (count == null) {
count = new Integer(1);
string = "Welcome User";
} else {
count = new Integer((count.intValue()) + 1);
string = "Welcome Back, User";
}
session.setAttribute("session.active", count);
}
// Invoke methods in doPost
printHeader(out);
printSessionData(out, count, string, session);
printFooter(out);
}
    @Override
public String getServletInfo() {
return "SessionTrackerServlet";
}
/* -- Private Method -- */
// Method handle Output Form header
private void printHeader(PrintWriter out) {
out.println("<html>");
out.println("<head>");
out.println("<title> Session tracker </title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Information about session</h1>");
}
// Method handle output Form center
private void printSessionData(PrintWriter out, Integer count, String string, HttpSession session) {
out.println("<b><I>" + string + "</I></b>");
out.println("<BR>No. of time page visit : ");
out.println("<b>" + count + ((count.intValue() == 1) ? " times." : " times.") + "</b>");
out.println(" <hr align=\"left\" width=\"35%\"/>");
out.println("<b>Other Session Property</b>");
out.println("<BR>Current Session id: " + session.getId() + " <I>(keep it secret)</I>");
out.println("<BR>Max Timeout: " + session.getMaxInactiveInterval());
out.println("<I>(" + session.getMaxInactiveInterval() / 60 + " minutes)</I>");
out.println("<BR>Creation time: " + session.getCreationTime());
out.println("<I>(" + new Date(session.getCreationTime()) + ")</I>");
out.println("<BR>Last access time: " + session.getLastAccessedTime());
out.println("<I>(" + new Date(session.getLastAccessedTime()) + ")</I>");
}
// Method handel Output Form Footer
private void printFooter(PrintWriter out) {
out.println("</body>");
out.println("</html>");
out.println("<BR><BR><a href=\"SessionTrackerServlet\">Return to Home Page</a>");
}
}

 


Output:


jsp


s1


s2


Technorati Tags: ,

Comments

Popular posts from this blog

WAP to calculate the monthly telephone bills as per the following rule: Minimum Rs. 200 for upto 100 calls. Plus Rs. 0.60 per call for next 50 calls. Plus Rs. 0.50 per call for next 50 calls. Plus Rs. 0.40 per call for any call beyond 200 calls.

  #include<iostream.h> #include<conio.h> void main() { int calls; float bill; cout<<" Enter number of calls : "; cin>>calls; if (calls<=100) bill=200; else if (calls>100 && calls<=150) { calls=calls-100; bill=200+(0.60*calls); } else if (calls>150 && calls<=200) { calls=calls-150; bill=200+(0.60*50)+(0.50*calls); } else { calls=calls-200; bill=200+(0.60*50)+(0.50*50)+(0.40*calls); } cout<<" Your bill is Rs. "<<bill; getch(); }   Output: Enter number of calls : 190 Your bill is Rs.250

Write a program to calculate the total expenses. Quantity and price per item are input by the user and discount of 10% is offered if the expense is more than 7000.

  #include<iostream.h> #include<conio.h> void main() { int totalexp, qty, price, discount; cout<<" Enter quantity: "; cin>>qty; cout<<" Enter price: "; cin>>price; totalexp=qty*price; if (totalexp>7000) { discount=(totalexp*0.1); totalexp=totalexp-discount; } cout<<" Total Expense is Rs. "<<totalexp; getch(); }