Q)Which is the servicing method in a realtime servlet?
doGet/doPost
Q)How to end the user/client session in a Java web application?
=>Ending the session in server's view point is nothing but destroying the session object that the container maintains.
=>session is ended in two ways.
1.)when user explicitly logs out.
2.)when session is timedout(session expiry)
=>If a user is inactive with the website for more than a specified period of time
after session is allotted(created),container forcibly logs the user out by destroying the session implicilty. This is known as session expiry OR session time out.
=>Every server has default time period for session timeout. It is 30 minutes in case of Tomcat.
=>We can change it in two ways.
a) by calling setMaxInactiveInterval(int seconds) on session object.
b.)By specifying in web.xml the session timeout period in minutes.
For eg. session time out period configured here is 3 minutes.
3
=>By calling invalidate() method, session is destroyed when user explicitly logs out.
Note:- we can verify if the request comes after the session is timed out using
isRequestedSessionIdValid() method of HttpServletRequest object.
Q)Java web application in which data is shared in session scope.
sessionscopeapplication
user.html
WEB-INF
web.xml
lib
src
SourceServlet.java
TargetServlet.java
classes
SourceServlet.class
TargetServlet.class
http://localhost:8081/sessionscopeapplication/user.html
user.html
-----------
user name entry screen
--------------------------------
web.xml
----------
four
SourceServlet
five
TargetServlet
four
/source
five
/target
----------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SourceServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException
{
String user=request.getParameter("username");
HttpSession session=request.getSession();
session.setAttribute("usr",user);//sharing data in session scope
System.out.println(session.getId());
System.out.println("Is this a new session:"+session.isNew());
System.out.println("Default session time out period in seconds:"+session.getMaxInactiveInterval());
session.setMaxInactiveInterval(180);
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("");
out.println("");
out.println("CLICK HERE TO GET USER NAME");
out.println("");
out.println("");
out.close();
}
}
-------------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TargetServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException
{
HttpSession session=request.getSession();
System.out.println(session.getId());
System.out.println("Is this a new session:"+session.isNew());
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("");
out.println("");
if(request.isRequestedSessionIdValid())
out.println("USER NAME IS:"+session.getAttribute("usr"));
else
out.println("You are timed out
");
out.println("");
out.println("");
out.close();
}
}
********************************************************
Q)Java web application in which, shopping cart is implemented using session
tracking.
shoppingcartapplication
shoppage.html
WEB-INF
web.xml
lib
src
ShopServlet.java
classes
ShopServlet.class
http://localhost:8081/shoppingcartapplication/shoppage.html
shoppage.html
-----------------
WELCOME TO SHOPPING MALL
--------------------------------------
web.xml
----------
Shopping
ShopServlet
Shopping
/shopsession
--------------------------------------
//ShopServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ShopServlet extends HttpServlet
{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
String code=null;
String qty=null;
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.println("");
pw.println("");
HttpSession session=request.getSession();
String sb=request.getParameter("s");
if(sb.equals("ADD ITEM"))
{
code=request.getParameter("pCode");
qty=request.getParameter("q");
session.setAttribute(code,qty);
response.sendRedirect("shoppage.html");
}
else if(sb.equals("REMOVE ITEM"))
{
code=request.getParameter("pCode");
session.removeAttribute(code);
response.sendRedirect("shoppage.html");
}
else if(sb.equals("SHOW ITEMS"))
{
Enumeration e=session.getAttributeNames();
if(e.hasMoreElements())
{
pw.println("YOUR SHOPPING CART ITEMS
");
while(e.hasMoreElements())
{
String c=(String)e.nextElement();
pw.println("PRODUCT CODE:"+c);
pw.println(" QUANTITY IS:"+session.getAttribute(c)+"
");
}//while
}//end of if
else
{
pw.println("NO ITEMS PLEASE
");
}
pw.println(" WANT TO SHOP MORE!");
}//outer if
else
{
session.invalidate();
pw.println(" THANK YOU! VISIT AGAIN TO SHOP AGAIN");
}
pw.println("");
pw.println("");
pw.close();
}//doPost
}//class