I am studying programming as a hobby. I started studying because I wanted to create a secondhand book sales site, but in order to dynamically generate web pages and perform data processing on the server, knowledge of Java Servlet (Java Servlet) is required. I started studying at ... (^_^;) Considering the secondhand book site as an example, the concept of session, which holds the information that the customer put in the cart, was not easy to understand, so I wrote this article !! ヾ ^ _ ^ ♪
--Session --Mechanism to maintain the status of each user
--In the Servlet, handle using the javax.servlet.http.HttpSession
interface
CounterServlet.java
package session;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CounterServlet extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
//1.Get session
HttpSession session = request.getSession();
//2.Getting an object from a session
Integer count = (Integer)session.getAttribute("count");
//3.Counter+1
if(count == null){
count = new Integer(0);
}
count = new Integer(count.intValue() + 1);
//4.Store objects in session
session.setAttribute("count", count);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><body>");
//Display session ID
out.println("<p>sessionId=" + session.getId());
//Display session ID
out.print("<p>count=" + count);
//Show link to this Servlet
String linkurl = request.getRequestURI();
out.println("<p><a href=\"" + linkurl + "\">RELOAD</a>");
out.println("</body></html>");
}
}
Passing data in session scope The data registered in the request scope could not be passed across requests, but the data registered in the session scope can be passed.
--request scope --Objects valid between the Servlet and the forwarded JSP that run in a single HTTP protocol --Destroy when the request is processed --Set as HttpServletRequest attribute
--session scope --Objects valid in Servlets and JSPs that run in the same session --Abandoned at the end of session after a certain period of time --Set as an attribute of HttpSession
Methods for manipulating session-scoped objects
Object getAttribute(java.lang.String name)
Gets the object specified by the argument. Returns null if it does not exist.
Since the returned type is Object type, it seems that it is often used by casting appropriately.
void setAttribute(java.lang.String name, java.lang.Object value)
Register the object specified by the argument value with the name name.
void removeAttribute(java.lang.String name)
Deletes the object specified by the argument name.
java.util.Enumeration getAttributeNames()
Returns a list of registered names.
Session timeout If you do not delete the session data, unused data will remain on the server. As a workaround, the application server has a mechanism to check the elapsed time since the client last accessed and delete it.
web.xml
<session-config>
<session-timeout>3</session-timeout>
</session-config>
Recommended Posts