Java Web session Technology

1.Cookie object:

cookie is a session technology, which is used to save the data in the session to the user's browser, so that the browser and server can better interact with each other;

Create Serlet:

Create a chapter05 project, create a package you like, and write a Servlet class called lastaccessservlet in the package to obtain Cookie information and send the current time Cookie value to the client;

The code is as follows:

package chapter05.access;

import java.io.IOException;
import java.util.Date;

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

import jdk.javadoc.doclet.Reporter;

/**
 * Servlet implementation class LastAccessServlet
 */
@WebServlet("/LastAccessServlet")
public class LastAccessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LastAccessServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		Cookie[ ] cookies=request.getCookies();
		String lastTime=null;
		for (int i = 0; cookies != null&&i<cookies.length; i++) {
			String name = cookies[i].getName();
			if ("lastaccess".equals(name)) {
				lastTime=cookies[i].getValue();
			}
		}
		if (lastTime==null) {
			response.getWriter().print("This is your first visit to this website!");
		}else {
			response.getWriter().print("When was your last visit:"+lastTime);
		}
		String time = String.format("%tF%<tT", new Date());
		Cookie cookie=new Cookie("lastaccess", time);
		Cookie cookie1 =new Cookie("dashuju","2004");
		response.addCookie(cookie);
		response.addCookie(cookie1);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Results achieved:

Refresh time of the second access:

 

2.Session object:

Cookie technology can save the user's information in their respective browsers, and can realize data sharing under multiple requests. However, if more information is transmitted, using cookie technology will obviously increase the difficulty of server program processing. At this time, Session technology can be used;

Implement shopping cart:

To create a book Class:

package chpter05.session;

import java.io.Serializable;

public class Book implements Serializable{
	private static final long serialVersionUID = 1L;
	private  String id;
	private  String name;
	public  Book() {
	}
	public  Book(String id,String name) {
		this.id=id;
		this.name=name;
	}
	public String  getId() {
		return id;
	}
	public void setId(String id) {
		this.id=id;
	}
	public String  getName() {
		return name;
	}
	public void setName(String name) {
		this.name= name;
	}

}

Create database simulation class:

package chpter05.session;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;


public class BookBD {
private static  Map<String,Book> map = new LinkedHashMap<String,Book>();
	static {
		map.put("1", new Book("1","javaweb development"));
		map.put("2", new Book("2","jdbc development"));
		map.put("3", new Book("3","java Basics"));
		map.put("4", new Book("4","struts frame"));
		map.put("5", new Book("5","hibernate development"));
	}
	public static Collection<Book> getAll() {
		return map.values();
	}
	public static Book  getBook(String id) {
		return map.get(id);
	}
}

Create Servlet:

1. Create a class named ListBookServlet to operate. Click the "buy" link to add books to the shopping cart;

Effect achieved:

 

  Create a servlet class named PurchaseServlet:

  This code is used to save to the Session object;

Create a Servlet class named CartServlet to display the list of user charity books:

Code implementation effect:

 

  Enable user login:

1. Create encapsulated user information class:

2. Create indexServlet class to display the homepage of the website. If the user does not log in, the homepage interface prompts the user to log in. Otherwise, the user's logged in information is displayed to judge whether the user really logs in:

  3.LoginServlet is used to display the interface after user login:

  4. Creating the LogouServlet class will remove the user information and jump to the home page:

5. Create an entrepreneurial face with the name login.html, and the page contains form information:

 

 

Keywords: Java SSL server

Added by api on Wed, 10 Nov 2021 17:27:30 +0200