java Web session Technology (cookie)

Keywords: Java encoding Session xml

java Web(cookie) session Technology

Set xml configuration information
  <!-- ************************** -->
      Create a select -- >
     < servlet name > demo05 < / servlet name > <! -- 2. Name select -- >
     < servlet class > come. Lanou3g. Demo05 < / servlet class > <! -- 3. Set select address -- >
  </servlet>
  <!-- ************************** -->
  < servlet mapping > <! -- register configuration access path -- >
     < servlet name > demo05 < / servlet name > <! -- configure the name of this component to be consistent with the previous -- >
     < URL pattern > / servlet / demo05 < / url pattern > <! -- configure the access address of this component -- >
  </servlet-mapping>
  <!-- ************************** -->
     <!-- ************************** -->
      Create a select -- >
     < servlet name > demo06 < / servlet name > <! -- 2. Name select -- >
     < servlet class > come. Lanou3g. Demo06 < / servlet class > <! -- 3. Set select address -- >
  </servlet>
  <!-- ************************** -->
  < servlet mapping > <! -- register configuration access path -- >
     < servlet name > demo06 < / servlet name > <! -- configure the name of this component to be consistent with the previous -- >
     < URL pattern > / servlet / demo06 < / url pattern > <! -- configure the access address of this component -- >
  </servlet-mapping>
  <!-- ************************** -->
       <!-- ************************** -->
      Create a select -- >
     < servlet name > demo07 < / servlet name > <! -- 2. Name select -- >
     < servlet class > come. Lanou3g. Demo07 < / servlet class > <! -- 3. Set select address -- >
  </servlet>
  <!-- ************************** -->
  < servlet mapping > <! -- register configuration access path -- >
     < servlet name > demo07 < / servlet name > <! -- configure the name of this component to be consistent with the previous -- >
     < URL pattern > / servlet / demo07 < / url pattern > <! -- configure the access address of this component -- >
  </servlet-mapping>

1. Set last access time

import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo05 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Set the encoding format of the response
        response.setContentType("text/html;charset=UTF-8");
        //Tell the server what encoding format the requested content is
        request.setCharacterEncoding("UTF-8");

        //Read cookie
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            //Traversing array
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("lastTime")) {
                    //Take out the value of the cookie
                    String value = cookie.getValue();
                    //String to long
                    long time = Long.parseLong(value);
                    //Transfer date
                    Date  date = new Date(time);
                    //Create a date format to display dates
                    //Parameter is the date format you want to display
                    SimpleDateFormat dateFormat = 
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    //Format time
                    String lastTime = dateFormat.format(date);
                    //Respond to browser
                    response.getWriter().write("Last visit time:" + lastTime);
                }
            }
            }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

2. Set up the form and print the last access time

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
 * Log user name after login
 * 
 */
public class Demo06 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Set the encoding format of the response
        response.setContentType("text/html;charset=UTF-8");
        //Tell the server what encoding format the requested content is
        request.setCharacterEncoding("UTF-8");
        String username = "";
        //Read cookie
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("username")) {
                    username = cookie.getValue();

                }
            }
        }
        System.out.println(username);
        response.getWriter().write("<form action='demo07' method='post'>");
        response.getWriter().write("User name:<input type=\"text\" name=\"username\"><br/>");
        response.getWriter().write("Password:<input type=\"text\" name=\"password\"><br/>");
        response.getWriter().write("remember:<input type=\"checkbox\" name=\"remember\" value=\"\"><br/>");
        response.getWriter().write("<input type=\"submit\" value= \"Sign in\">");
        response.getWriter().write("</form>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

3. Accept form data and record user cookie s

package come.lanou3g;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Demo07 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Set the encoding format of the response
        response.setContentType("text/html;charset=UTF-8");
        //Tell the server what encoding format the requested content is
        request.setCharacterEncoding("UTF-8");

        //Get requested parameters
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String remember = request.getParameter("remember");
        //Create cookie
        Cookie cookie = new Cookie("username", username);
        //Set cookie path
        cookie.setPath("/sh-web-servlet003/servlet");
        //Process business logic
        if(username.equals("wangl")
                && password.equals("123")){
            //Judge whether to click the remember button
            if (remember != null) {
                System.out.println(remember + "123123123");
                //Need to remember (set survival time)
                cookie.setMaxAge(Integer.MAX_VALUE);
            }else {
                //Delete the original cookie
                cookie.setMaxAge(0);
            }
            //Add to response
            response.addCookie(cookie);
            response.getWriter().write("Login successfully");
        }else {
            response.getWriter().write("Login failed");
            //Go back to the login page in 3 seconds
            response.setHeader("refresh", "3;url=demo06");
        }
        //Request distribution

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

Posted by seodevhead on Sat, 04 Apr 2020 11:11:53 -0700