サーブレットは、4つのドメインオブジェクトレコードページのアクセス情報を使用します

package com.zhm;

public class User {
	private String username;
	private String password;
	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}
}

package com.zhm;

import java.io.IOException;

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

@WebServlet("/zhuce")
public class ZhuceServlet extends HttpServlet{
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		//接收前端的用户名和密码
		User u = new User();
		u.setUsername(req.getParameter("username"));
		u.setPassword(req.getParameter("pwd"));
		
		//记录注册人数
		if(req.getServletContext().getAttribute("count") == null) {
			req.getServletContext().setAttribute("count",1);
		}else {
			req.getServletContext().setAttribute("count",(Integer)req.getServletContext().getAttribute("count")+1);
		}
		
		HttpSession session = req.getSession();
		session.setAttribute("user",u);
		resp.sendRedirect("/servlet/main.jsp");
	}
}





<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="/servlet/zhuce" method="post">
		<input type="text" name="username" />
		<input type="password" name="pwd" />
		<button>注册</button>
	</form>
</body>
</html>






<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="com.zhm.User" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	欢迎您:<%=((User)session.getAttribute("user")).getUsername() %>
	目前<%=application.getAttribute("count") %>人注册过
</body>
</html>
リリース5元の記事 ウォンの賞賛1 ビュー46

おすすめ

転載: blog.csdn.net/weixin_45570207/article/details/104535199