Session layer technology-session
session technology take down!
First, first organize a few doubts in the learning process
-
How are cookies and sessions created respectively?
- First of all cookie is a class which needs to be created manually by java backend developers.
Cookie cookies1 = new Cookie(keya, valuea); - Second session is the interface object of the HttpSession interface, which is created by the server, note that the creation here does not mean that you access the URL in the browser at the same time, he created, but in the servlet rewrite of the service or doXXX method by HttpSession session
= (); or HttpSession session = (true); created either way. - Note: In the default access to the jsp file, at the same time you open the site on the creation of the Session object, this is because the default setting in the jsp is true, <%@ page session = "true"%>, when the jsp file is compiled to generate the .java code, which will be a sentence session = (); so, in the tomcat start default access to the jsp page, this code will help you create a session, in essence, or to obtain the session object by determining whether there is no time to automatically create or select the previous session object.
- First of all cookie is a class which needs to be created manually by java backend developers.
-
With the help of this problem solving() the object fetched is created by is?
- () gets an object of type PrintWriter, which is automatically created by the server (that is, the servlet container) when this code is used to print a string to the web page.
- There may be doubt, why not just new ah to create this object! This I organize through the search, a simple understanding: () output is a stream object (the so-called stream object, is the print results can be output on the page), if new PrintWriter () an object, and then the output will not be output to the web page, but the idea of the print desk, this is just my own personal understanding, easy for me to remember! Everyone has a good can share with me, learning never ends, haha!
Second, ServletC is used to implement the process of session technology
First of all, briefly that introduces the session technology processing flow:
- user's first visit to the server, the server does not have a Session, automatically create session objects and ID (this ID is equivalent to a special cookie, used to save each Session object unique id value, he is special on the special name = JSESSIONID, value = the value of the id, the name of the ordinary cookie can be arbitrarily up to the value can be changed at will, while the ID created by the server session object name is fixed, the value is also unique. Ordinary cookie name can be arbitrarily, the value can be arbitrarily changed, but the ID name of the Session object created by the server is fixed, the value is also unique.
- The server responds to this special cookie to the client and waits until the next time the user visits, the Servlet can get the JSESSIONID value unique to each user, thus storing records of the data.
Third, ServletC code specifically how to achieve it
- First I'm going to call the getParameter() method through the request object to get information about the user accessing the server.
- Immediately after that, I call the getSession() method of the request object to determine (which is judged by the value of JSESSIONID) whether the user is a session that has been created before, if so, it will not be recreated again and will be fetched directly, if not, it will be automatically recreated and fetched.
- After that, I store the obtained user information into the Session object (equivalent to a personal profile).
- Finally, the value of the JSESSIONID is responded to the client via the response object (a special cookie).
The specific code implementation is as follows:
package ;
import ;
import ;
import ;
import ;
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;import ;import ;import ;import
import ;
@WebServlet(name = "ServletC", urlPatterns = "/ServletC")
public class ServletC extends HttpServlet {
public class ServletC extends HttpServlet { @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//Receive the value of the user parameter in the request, receive the name.
String username1 = ("username");//call the getParameter method of the req object to get the value of the usrname parameter
//This method passes in the parameter name to get the value of the parameter
// Create the session object
HttpSession session1 = ();//specific meaning of the above has been said
//Store the user's information into the session object.
("username", "yzh");; //Check the JSPS of the session object.
//View the JSESSION value of the session object.
String id01 = ();
("JSESSIONID = " + id01); //Respond to the user's message.
//Respond to the user information
().println("Responded");; //Respond to the user's message.
}
}
Fourth, ServletB to get the session object has been created to store the user information
I have been through the ServletC will be the user information into the creation of the Session, then how to through the next visit, according to the special cookie to identify the user, and the user information extracted to print out the ServletB to give us the answer!(Little mockery: a little bit of style! (Haha)
package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
@WebServlet("/ServletD")
public class ServletD extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session2 = ();//rejudgesessionDoes it already exist,pre-existing,Aim directly at him.
("last (week etc)session(used form a nominal expression)idvalue is:" + ());
String username2 = (String)("username1");//call (programming)getAttribute()方法得到该用户(used form a nominal expression)属性值
().println(username2);//come on,showcase!
}
}
Let's get a little better every day. Steady is the way to go in the long run!