2013-03-26 61 views
0

我是JSP和Servlet的新手,請耐心等待一段時間! 我在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真糊塗!我究竟做錯了什麼??

+0

這是什麼索引 – PSR 2013-03-26 04:15:31

+0

index.jsp是第一個正在運行的asp。 – user2208039 2013-03-26 04:19:37

+0

做'重定向'而不是'轉發',否則'LibrarySysServlet'將會持久在url中。 – Sorter 2013-03-26 04:21:57

回答

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"); 
        } 
      } 
+0

雖然我通過了另一個函數,但我做到了。不過,我認爲我的索引頁不與我的servlet通信。 – user2208039 2013-03-26 04:20:41

+0

你的意思是'if(variable)',可能只是改變JSP而不是重複調度程序代碼。 – 2013-03-27 02:30:44

0

最好是使用reponse.sendRedirect(「{url}」)而不是調度程序。

還要測試你的登錄方法看看它是否如你所願

+1

,我也認爲request.getPathInfo()不應該是「索引」....嘗試一個System.out.println(頁)調試 – 2013-03-26 04:23:18

+0

嗨,我試過了,但我意識到,它是在我的servlet意義不是它甚至沒有通過try {}運行,而是作爲一個例外被捕獲。任何想法爲什麼? – user2208039 2013-03-26 04:37:08

0

你可以充分利用基於表單的容器和使用認證。在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工作。