How to use HttpSession to implement QQ access records in Java, this article reveals the answers to you. The specific content is as follows
1. Write QQ space data class (QQS.java)
public class QQS { private static LinkedHashMap<Integer, String> qqs = new LinkedHashMap<Integer, String>(); static{ qqs.put(10001, "Zhang San"); qqs.put(10002, "Li Si"); qqs.put(10003, "Wang Wu"); qqs.put(10004, "Zhao Liu"); qqs.put(10005, "Tian Qi"); qqs.put(10006, "Jiao Ba"); qqs.put(10007, "Hou Jiu"); qqs.put(10008, "Liu Shi"); qqs.put(10009, "Xiao Er"); } public static LinkedHashMap<Integer, String> getQqs() { return qqs; }}2. Write a page of real QQ data and browsing history (ListServlet.java)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // Get Session object HttpSession session = request.getSession(); // Set Chinese data response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); // Get output stream PrintWriter out = response.getWriter(); // Get QQS data LinkedHashMap<Integer, String> qqs = QQS.getQqs(); Set<Map.Entry<Integer, String>> set = qqs.entrySet(); Iterator<Map.Entry<Integer, String>> it = set.iterator(); // Output page structure out.println("<html><head><title>QQ list</title><style>a{margin-right:20px;}</style></head><body>"); out.println("<hr/><br/>"); out.println("<h3>QQ list</h3>"); out.println("<hr/><br/>"); out.println("<hr/><br/>"); out.println("<hr/><br/>"); out.println("<hr/><br/>"); // Loop out the hyperlink of QQ space while(it.hasNext()){ Map.Entry<Integer, String> entry = it.next(); Integer num = entry.getKey(); String name = entry.getValue(); out.println("<a href=/"/day08/store?num="+num+"/">"+name+"</a>"); } // Output browsed record information out.println("<hr/><br/>"); out.println("<h3>QQ browsing history</h3>"); out.println("<hr/><br/>"); // Get access record data String history = (String) session.getAttribute("history"); if(history == null){ out.println("<font color=/"red/">Sorry, there is no access record at present...</font>"); }else{ // Loop through the record data accessed by the user String[] nums = history.split(","); for(String num:nums){ String name = QQS.getQqs().get(Integer.parseInt(num)); out.println(name+" ,"); } } // Close the page structure out.println("</body></html>"); }3. Write a page that stores browsing QQ space (StoreQQServlet.java)
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { // Get the Session object HttpSession session = request.getSession(false); // Get the request parameter String num = request.getParameter("num"); // Get the data in the Session String history = (String) session.getAttribute("history"); // Determine the data if(history == null){ // First visit session.setAttribute("history", num); // history=10001 }else{ // Visit multiple session.setAttribute("history", history+","+num); // Set the number of nums and the order of display String[] qqs = history.split(","); // Convert an array into a collection that is convenient for operation List<String> list = Arrays.asList(qqs); // Convert List to LinkedList to facilitate operation of data LinkedList<String> linked_list = new LinkedList<String>(); linked_list.addAll(list); // Determine the number of QQs that appear if(qqs.length < 3 ){ if(linked_list.contains(num)){ // history=10002,1003 // If linked_list.remove(num); linked_list.addFirst(num); }else{ // history=1004,10002,1003 // If linked_list.addFirst(num); } }else{ // >= 3 if(linked_list.contains(num)){ // history=10002,10003,10004 10004 // If linked_list.remove(num); linked_list.addFirst(num); }else{ // history= 10005 ,10002,10003 // does not include linked_list.removeLast(); linked_list.addFirst(num); } } // Access records with good times and order are linked_list StringBuffer sb = new StringBuffer(); for(String new_num:linked_list){ sb.append(new_num+","); } String new_history = sb.toString(); session.setAttribute("history", new_history); } // Redirect to QQ list page response.sendRedirect("/day08/list"); }The above code stores the user's browsing history in the session object, but the object is in the server memory and has a valid time limit. If the time comes, the session will be destroyed.
The default time is half an hour (30 minutes).
4. Configure the valid time of the Session
In the web.xml of each website, you can configure the valid time of the session object created by the website. Note that the unit is minutes when configuring.
Thread.slessp (millisecond units), Cookie.setMaxAge (second units), session (minute units)
<session-config> <session-timeout>2</session-timeout> Unit is minutes</session-config>
5 Disable Cookies
Cookies can use the client to store session data.
HttpSession can use cookies to store SessionID information.
In fact, in the browser settings, you can reject cookie information sent back by the website.
At this time, accessing the above cases will cause a null pointer exception to occur. If you need to fix the website, you must use URLRewriting technology.
URLRewriteting
Analyze the causes of the above problems:
The server has created a Session object, but since the browser prohibits the reception of cookies, the server cannot send the ID value of the created Session to the browser in the form of the Set-Cookie response header for storage, so the Session ID will not be slacked off during the second visit, so the Session cannot be found.
Common methods
String encodeRedirectURL(String url) Add Sessionid information to the specified redirect path String encodeURL(String url) Add Sessionid information to the normal URL address
Principles of implementation:
"Recode all URL addresses in the page using the above method"
1 Modify the above program
1 ListServlet.java
String path = "/day08/store?num="+num; path = response.encodeURL(path); out.println("<a href='"+path+"'>"+name+"</a>");2. StoreQQServlet.java
String path = "/day08/list";path = response.encodeRedirectURL(path);response.sendRedirect(path);
The above is all about this article, I hope it will be helpful to everyone's learning.