0

我正在尝试使用 .jsp 和 servlet 创建一个简单的登录页面。单击 LogIn.jsp 表单中的按钮后,我似乎无法触发 doPost 方法。我的代码如图所示:

登录.jsp

    <%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>
    <head><title>LogIn JSP Page</title></head>
    <body bgcolor="white">
        <h1>LogIn JSP Page</h1>
        <form action="CheckLogIn" method="POST">
            <table>
                <tr>
                    <td align="right">Member ID:</td>
                    <td align="left"><input type="text" name="memId" length="30"/>
                    </td>
                </tr>
                <tr>
                    <td align="right">Password:</td>
                    <td align="left"><input type="text" name="pw" length="30"/>
                    </td>
                </tr>
            </table>
            <p><input type="submit" value="Log In"/></p>
        </form>
    </body>
</html>

CheckLogIn.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<html>
    <head>
        <title>CheckLogIn JSP Page</title>
    </head>
    <body>
        <h1>Check LogIn</h1>
        <%
            String data=(String)request.getAttribute("data");
        %>
        <table>
            <tr>
                <td align="left"><%=(String)data%></td>
            </tr>
        </table>
    </body>
</html>

库SystemServlet.java

    public class LibrarySystemServlet extends HttpServlet {
    @EJB
    private MemberManagerRemote mm;

    private String success=null;

    public void init(){
        System.out.println("LibrarySystemServlet: init()");
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        System.out.println("LibrarySystemServlet: processRequest()");
        try {
            RequestDispatcher dispatcher;
            ServletContext servletContext=getServletContext();

            String page=request.getPathInfo();
            page=page.substring(1);

            if("CheckLogIn".equals(page)){
                success=logIn(request);
                request.setAttribute("data", success);
            }
            else{
                page="Error";
            }
            dispatcher=servletContext.getNamedDispatcher(page);
            if(dispatcher==null){
               dispatcher=servletContext.getNamedDispatcher("Error");
            }
            dispatcher.forward(request, response);
        } catch(Exception ex) {
            log("Exception in LibraySystemServlet.processRequest()");
        }
    } 

    private String logIn(HttpServletRequest request){
        String s;
        String memberId=request.getParameter("memId");
        String pw=request.getParameter("pw");
        if(pw.equals(mm.getMemberPw(memberId))){
            s="Success";
        }
        else{
            s="Failed";
        }
        return s;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        System.out.println("LibrarySystemServlet: doGet()");
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        System.out.print("LibrarySystemServlet: doPost()");
        processRequest(request, response);
    }

    public void destroy(){
        System.out.println("LibrarySystemServlet: destroy()");
    }
}

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>LibrarySystemServlet</servlet-name>
        <servlet-class>servlets.LibrarySystemServlet</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>CheckLogIn</servlet-name>
        <jsp-file>/CheckLogIn.jsp</jsp-file>
    </servlet>
    <servlet>
        <servlet-name>Error</servlet-name>
        <jsp-file>/Error.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>CheckLogIn</servlet-name>
        <url-pattern>/CheckLogIn</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Error</servlet-name>
        <url-pattern>/Error</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>LibrarySystemServlet</servlet-name>
        <url-pattern>/librarySystemServlet/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>LogIn.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

任何帮助将不胜感激!

4

1 回答 1

0

在您的 web.xml 文件中,您需要将 CheckLogIn 的请求映射到 servlets.LibrarySystemServlet 类而不是 CheckLogIn.jsp

<servlet>
        <servlet-name>CheckLogIn</servlet-name>
        <servlet-class>servlets.LibrarySystemServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>CheckLogIn</servlet-name>
        <url-pattern>/CheckLogIn</url-pattern>
</servlet-mapping>
于 2013-03-20T14:43:49.503 回答