0

我是 JSP 和 Servlet 的新手,所以请耐心等待!我对 JSP 和 Servlet 之间的通信有疑问。

我正在尝试创建一个登录页面。

这是我的 LogIN 索引 JSP 页面

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<html>
    <head><title>  index.jsp</title></head>
    <body bgcolor="orange">
    <h2> <font color=white> JSP index.jsp at</font>
            <%= request.getContextPath () %>
            <font color=white>
            </font></h2><hr>


            <form action="LibrarySysServlet" method ="POST">
            <font color=navy><h3>Login</h3>
                <table>
                <tr>
                    <td> MemberId </td>
                    <td align="left"><input type="text" name="memId" length="30"/></td>
                </tr>
                <tr>
                    <td> Password </td>
                    <td align="left"><input type="text" name="password" length="30"/></td>
                </tr>
                </table>
                <br>
                <p><input type="submit" value="Submit"/></p>

                </font>
            </form>

            <br>
    </body>
</html>

我真的很困惑在登录时为索引页面的表单操作放置什么,因为目的地会根据登录是否正确而发生变化。

对于我的 Servlet 方面,我尝试扫描索引页面并将其传递给一个函数以检查 logIn 是否正确并根据不同的答案重定向它。

但是,在运行时,它不会重定向,而是继续执行表单操作中指定的任何内容。

代码的简短片段(没有粘贴整个内容,因为它太长了):

    RequestDispatcher dispatcher;
                ServletContext servletContext = getServletContext(); //links to index.JSP

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


                if ("index".equals(page)) {
                    if (logIn(request)) {
                      dispatcher= servletContext.getRequestDispatcher("/result.jsp");
                     } else {
                     dispatcher= servletContext.getRequestDispatcher("/Menu.jsp");
                     }
                 }

HELP真的很困惑!我究竟做错了什么??

4

3 回答 3

0

您可以利用容器并使用基于 FORM 的身份验证。在 web.xml 中:

   <login-config>
      <auth-method>FORM</auth-method>
      <form-login-config>
         <form-login-page>login.html</form-login-page>
         <form-error-page>login_error.html</form-error-page>
      </form-login-config>
   </login-config>

然后在 login.html 中:

   Please log in:
   <form method="POST" action="j_security_check">
      <input type="text" name="j_username">
      <input type="password" name="j_password">
      <input type="submit" value="Login">
   </form>

您必须使用 j_security_check、j_username 和 j_password 才能使其工作。

于 2013-03-26T04:40:39.683 回答
0
  RequestDispatcher dispatcher;
                ServletContext servletContext = getServletContext(); //links to index.JSP

               //check here your username and password and put into a variable true or false



                    if (variable== true) {
                      dispatcher= servletContext.getRequestDispatcher("/result.jsp");
                     } else {
                     dispatcher= servletContext.getRequestDispatcher("/Menu.jsp");
                     }
             }
于 2013-03-26T04:17:32.107 回答
0

最好使用 reponse.sendRedirect("{url}") 而不是调度程序。

还要测试你的登录方法,看看它是否如你所愿

于 2013-03-26T04:21:44.260 回答