将login.jsp提交的用户名和密码由FirstServlet(映射地址为/servlet/firstServlet.do)获取进行判断,如果登录成功,则跳转至SecondServlet(映射地址为/servlet/secondServlet.do)显示用户名信息,如果登录不成功,则跳转至登录失败页面hw/failed.jsp。 要求:用户名和密码存在WEB-INF/userinfo.txt文件中,若页面出现404错误则统一跳转至404.html页面,500错误则统一跳转至500.html页面,并提示出错信息。
时间: 2023-05-30 11:05:39 浏览: 169
首先,需要编写login.jsp页面,其中包含一个表单,用户输入用户名和密码,并将其提交给FirstServlet:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/servlet/firstServlet.do" method="post">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
然后,编写FirstServlet,获取用户名和密码,并进行判断:
```java
import java.io.BufferedReader;
import java.io.FileReader;
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;
@WebServlet("/servlet/firstServlet.do")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 从userinfo.txt文件中读取用户名和密码
BufferedReader reader = new BufferedReader(new FileReader(getServletContext().getRealPath("/WEB-INF/userinfo.txt")));
String line = null;
boolean success = false;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (username.equals(parts[0]) && password.equals(parts[1])) {
success = true;
break;
}
}
reader.close();
if (success) {
// 登录成功,跳转到SecondServlet
request.setAttribute("username", username);
request.getRequestDispatcher("/servlet/secondServlet.do").forward(request, response);
} else {
// 登录失败,跳转到failed.jsp
response.sendRedirect(request.getContextPath() + "/hw/failed.jsp");
}
}
}
```
如果登录成功,将用户名存储在request中,并将请求转发到SecondServlet;如果登录失败,则重定向到failed.jsp页面。
接下来,编写SecondServlet,显示用户名信息:
```java
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;
@WebServlet("/servlet/secondServlet.do")
public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = (String) request.getAttribute("username");
response.setContentType("text/html");
response.getWriter().println("<html><head><title>Welcome</title></head><body>");
response.getWriter().println("<h1>Welcome, " + username + "!</h1>");
response.getWriter().println("</body></html>");
}
}
```
最后,编写错误处理页面404.html和500.html,并在web.xml中配置错误页面:
```xml
<web-app>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
</web-app>
```
这样,当出现404或500错误时,就会自动跳转到相应的错误页面。
阅读全文