source file name: SessionServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet(urlPatterns = {"/SessionServlet"})
public class SessionServlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
HttpSession hs=request.getSession();
Integer cnt=(Integer)hs.getAttribute("count");
if(cnt==null)
cnt=new Integer(1);
else
cnt=new Integer(cnt.intValue()+1);
hs.setAttribute("count", cnt);
out.println("<h1> Counter : "+cnt);
out.println("<html>");
out.println("<head>");
out.println("<title> Session Servlet program </title>");
out.println("</head>");
out.println("<body>");
out.println("<form action=http://localhost:8080/ServletPrgs/SessionServlet method=get >");
out.println("<input type=submit value=click >");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
@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 {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
0 Comments
Post a Comment