大家好,我是一名入门的菜鸟,如果你不经意间翻开了我的文章,谢谢您,您的支持是我前进的动力,让我们一起加油!!
由于不是名牌大学,只是一个普普通通的专科生,所以,我想通过自己的努力来获得我想要的,我不会放弃我的梦想,我也曾幻想着我成功的时候在朋友边吹嘘,也曾想象到我失败时候潦倒的样子,幻想始终是幻想,我会努力的,加油,你一定能行
流程
步骤:
- 首先在servlet中创建init()方法,初始化计数为0
- 用户输入账号密码, 跳转到Servlet进行判断
- 如果账号密码正确,创建得到上下文对象
- 从上下文域中得到num计数
- 创建原子类
- 并把域中的内容存到原子类中
- 计数+1,并且把+1后的计数再更新到上下文域中
- 重定向到另一个显示第多少个用户登录的servlet
- 账号密码错误,返回登录页面重新登录
效果图
这里我用了三个不同的浏览来模拟了多个用户登录的效果
-
IE浏览器登录账户
-
谷歌浏览器登录账户
-
火狐浏览器登录账户
代码
html代码:
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020/4/22
Time: 15:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h2>用户登录</h2>
<form action="Test04Servlet" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>
servlet
package com.aoshen.Test;
import javax.servlet.ServletContext;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.atomic.AtomicInteger;
@WebServlet(value = "/Test04Servlet")
public class Test04Servlet extends HttpServlet {
//第一次访问的时候执行1次
@Override
public void init() throws ServletException {
//向上下文域中添加初始值
getServletContext().setAttribute("num", 0);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//登录判断用户名密码是否正确
String username = request.getParameter("username");
String password = request.getParameter("password");
//不要直接使用int类型的变量,使用AtomicInteger
//如果正确,次数就加1
if ("admin".equals(username) && "123".equals(password)) {
//从上下文域中取出值
ServletContext application = getServletContext();
int num1 = (int)application.getAttribute("num");
AtomicInteger num = new AtomicInteger(num1);
//加1放进上下文域中
application.setAttribute("num",num.incrementAndGet());
//重定向,跳转到另一个servlet,显示
response.sendRedirect("show");
}else{
PrintWriter out = response.getWriter();
out.print("登录失败<a href='index.jsp'>请重试</a>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
showServlet
package com.aoshen.Test;
import javax.servlet.ServletContext;
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 java.io.IOException;
import java.io.PrintWriter;
@WebServlet(value = "/show")
public class ShowServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ServletContext application = getServletContext();
Object num = application.getAttribute("num");
PrintWriter out = response.getWriter();
out.print("<h2>登录成功,您是第" + num + "个登录的用户</h2>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}