ZBLOG

假设用户登录页面login.jsp在web工程web2024的hw目录下,即访问login.jsp的URL地址是http://localhost:8080/web2024/hw/login.jsp,将login.jsp提交的用户名和密码由FirstServlet(映射地址为/servlet/firstServlet.do)获取进行判断,如果登录成功,则跳转至SecondServlet(映射地址为/servlet/secondServlet.do)显示用户名信息,如果登录不成功,则跳转至登录...

根据你提供的需求,下面是一个实现示例,包括 login.jspFirstServletSecondServlet 的代码,以及如何处理错误页面。

1. 文件结构

web2024/
│
├── hw/
│   ├── login.jsp
│   ├── failed.jsp
│
├── WEB-INF/
│   ├── userinfo.txt
│   └── web.xml
├── 404.html
└── 500.html

2. userinfo.txt

WEB-INF/userinfo.txt 中存储用户信息,例如:

username1:password1
username2:password2

3. login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h2>Login Page</h2>
    <form action="<%= request.getContextPath() %>/servlet/firstServlet.do" method="post">
        Username: <input type="text" name="username"/><br/>
        Password: <input type="password" name="password"/><br/>
        <input type="submit" value="Login"/>
    </form>
</body>
</html>

4. FirstServlet.java

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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@WebServlet("/servlet/firstServlet.do")
public class FirstServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 验证用户名和密码是否正确。
        boolean isValidUser = false;

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(getServletContext().getResourceAsStream("/WEB-INF/userinfo.txt")))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(":");
                if (parts.length == 2 && parts[0].equals(username) && parts[1].equals(password)) {
                    isValidUser = true;
                    break;
                }
            }
        }

        if (isValidUser) {
            response.sendRedirect(request.getContextPath() + "/servlet/secondServlet.do?username=" + username);
        } else {
            response.sendRedirect(request.getContextPath() + "/hw/failed.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 通常不支持 GET 请求,但可以重定向到登录页。
        response.sendRedirect(request.getContextPath() + "/hw/login.jsp");
    }
}

5. SecondServlet.java

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 {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        
        response.setContentType("text/html;charset=UTF-8");
        
        response.getWriter().println("<h1>Welcome, " + username + "!</h1>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         // 不支持POST请求,这里可以重定向到其他地方或返回错误。
         doGet(request, response);
     }
}

6. web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    
    <!-- 定义Error Pages -->
    <error-page error-code="404">
        <location>/404.html</location>
    </error-page>

    <error-page error-code="500">
        <location>/500.html</location>
    </error-page>

</web-app>

7. 错误页面 (404.html500.html)

404.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>404 Not Found</title>
</head>
<body>
<h1>Error 404 - Page Not Found</h1>
<p>The page you are looking for does not exist.</p>
<a href="/web2024/hw/login.jsp">Go to Login Page</a>
</body>
</html>

500.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>500 Internal Server Error</title>
</head>
<body>
<h1>Error 500 - Internal Server Error</h1>
<p>An unexpected error occurred on the server.</p>
// 可考虑输出异常信息,但需谨慎以免泄露敏感数据。
<a href="/web2024/hw/login.jsp">Go to Login Page</a>

<!-- 提示出错信息 -->
<% 
String errorMessage = (String)request.getAttribute("javax.servlet.error.message"); 
if(errorMessage != null){
out.println("<p>Error details: " + errorMessage + "</p>");
} 
%>

<!-- 提示用户联系管理员等 -->
<p>If the problem persists, please contact support.</p>

</body></html>

以上代码实现了你所要求的功能,包括用户登录验证、成功与失败的跳转、错误处理等。确保在Tomcat服务器中正确部署该Web应用程序,并根据需要调整路径和内容。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=20900

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?