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

Why do we need mapdispatchtoprops?

473


What is apache struts cve 2017 5638?

542


What is the difference between Jakarta Struts and Apache Struts? Which one is better to use?

574


How long do struts last?

526


What are pull and push mvc architecture and which architecture does struts2 follow?

506






What do you mean by a custom tag?

541


What is difference between lookupdispatchaction and dispatchaction?

627


What are the steps used to setup dispatch action?

495


What is difference between actionform and dynaactionform?

591


How to handle exceptions in structs?

568


Explain the life cycle of a request in struct2 application?

490


What is struts xml?

437


What’s the utilization of resource bundle properties file in struts validation structure?

621


Which configuration file is used for storing jsp configuration information in struts?

539


How many instances of servlet usually run in a struts application ?

540