session menagement in servlets explaion briefly?

Answer Posted / abhijit dutta

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class SessionSample extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {


// Step 1: Get the Session object

boolean create = true;
HttpSession session = request.getSession(create);

// Step 2: Get the session data value


Integer ival = (Integer)
session.getAttribute ("sessiontest.counter");
if (ival == null) ival = new Integer (1);
else ival = new Integer (ival.intValue () +
1);
session.setAttribute ("sessiontest.counter",
ival);

// Step 3: Output the page

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Session Tracking
Test</title></head>");
out.println("<body>");
out.println("<h1>Session Tracking Test</h1>");
out.println ("You have hit this page " + ival + "
times" + "<br>");
out.println ("Your " + request.getHeader("Cookie"));
out.println("</body></html>");
}
}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which interceptor is responsible for mapping request parameters to action class Java Bean properties?

758


What is container struts2?

743


What is the use of token interceptor in Struts2?

842


Define struts?

812


Which servlet does the struts framework use?

715


What is Struts2?

811


What does apache struts do?

681


How to use forward action to restrict a strut application to mvc?

780


What do you know about validation plugin ?

694


What is the purpose of constant tag in struts.xml?

708


How can we handle exceptions thrown by application in Struts2?

719


How are interceptors and servlet filters different?

689


What is the purpose of @key?

767


Explain about struts dispatch action?

745


Explain struts.devmode?

738