<!--案例----登录错误时显示错误界面-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<center>
<h2>用户登录</h2>
<form action="login" method="get">
<p>账号:<input type="text" name="name"></p>
<p>密码:<input type="text" name="passWord"></p>
<input type="submit" value="登录">
</form>
</center>
</body>
</html>
package com.pojo.test1;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "loginServlet", urlPatterns = "/login")
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userName="daiping";
String userPwd="123456";
String name=request.getParameter("name");
String passWord=request.getParameter("passWord");
if (!name.equals("userName")){
request.setAttribute("errorMessage","账户错误");
request.getRequestDispatcher("/loginError.jsp").forward(request,response);
}else if (!passWord.equals("userPwd")){
request.setAttribute("errorMessage","密码错误");
request.getRequestDispatcher("/loginError.jsp").forward(request,response);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
<%--
Created by IntelliJ IDEA.
User: pan
Date: 2021/7/19
Time: 17:15
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<center>
<h2>用户登录界面错误</h2>
<div>
<%=request.getAttribute("errorMessage")%>
</div>
</center>
</body>
</html>