Last active
October 13, 2016 14:38
-
-
Save ypetya/4903cb676cf2905716fa7c90ad3c9a59 to your computer and use it in GitHub Desktop.
minimal in-memory session state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package example; | |
| import java.io.IOException; | |
| import java.io.PrintWriter; | |
| import java.util.Map; | |
| import java.util.concurrent.ConcurrentHashMap; | |
| 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 Peter_Kiss | |
| */ | |
| public class NewServlet extends HttpServlet { | |
| static final Map<String,String> STATE = new ConcurrentHashMap<>(); | |
| /** | |
| * Processes requests for both HTTP <code>GET</code> and <code>POST</code> | |
| * methods. | |
| * | |
| * @param request servlet request | |
| * @param response servlet response | |
| * @throws ServletException if a servlet-specific error occurs | |
| * @throws IOException if an I/O error occurs | |
| */ | |
| protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |
| throws ServletException, IOException { | |
| response.setContentType("text/html;charset=UTF-8"); | |
| try (PrintWriter out = response.getWriter()) { | |
| /* TODO output your page here. You may use following sample code. */ | |
| out.println("<!DOCTYPE html>"); | |
| out.println("<html>"); | |
| out.println("<head>"); | |
| out.println("<title>Servlet NewServlet</title>"); | |
| out.println("</head>"); | |
| out.println("<body>"); | |
| out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>"); | |
| out.println("<pre>"); | |
| HttpSession session = request.getSession(); | |
| out.println("SessionId: " + session.getId()); | |
| Integer counter = (Integer)session.getAttribute("counter"); | |
| if(counter==null) { | |
| counter = 1; | |
| } else { | |
| counter+=1; | |
| } | |
| out.println("counter: " + String.valueOf(counter)); | |
| session.setAttribute("counter", counter); | |
| STATE.put(session.getId(), String.valueOf(counter)); | |
| out.println("allSessionStates: "); | |
| for (Map.Entry e : STATE.entrySet()) { | |
| out.println("key: " + e.getKey()); | |
| out.println("value: " + e.getValue()); | |
| } | |
| out.println("</pre>"); | |
| out.println("</body>"); | |
| out.println("</html>"); | |
| } | |
| } | |
| // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> | |
| /** | |
| * Handles the HTTP <code>GET</code> method. | |
| * | |
| * @param request servlet request | |
| * @param response servlet response | |
| * @throws ServletException if a servlet-specific error occurs | |
| * @throws IOException if an I/O error occurs | |
| */ | |
| @Override | |
| protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
| throws ServletException, IOException { | |
| processRequest(request, response); | |
| } | |
| /** | |
| * Handles the HTTP <code>POST</code> method. | |
| * | |
| * @param request servlet request | |
| * @param response servlet response | |
| * @throws ServletException if a servlet-specific error occurs | |
| * @throws IOException if an I/O error occurs | |
| */ | |
| @Override | |
| protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
| throws ServletException, IOException { | |
| processRequest(request, response); | |
| } | |
| /** | |
| * Returns a short description of the servlet. | |
| * | |
| * @return a String containing servlet description | |
| */ | |
| @Override | |
| public String getServletInfo() { | |
| return "Short description"; | |
| }// </editor-fold> | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment