2015-12-22 37 views
1

我已經構建了一個帶有兩個jsp文件和控制器的小型servlet。Servlet getServletContext()。getRequestDispatcher顯示url行中的屬性

這是login.jsp

<body> 
    <section class="loginform cf"> 
    <form name="login" action="controller/login" method="get" onsubmit="return validateForm()" 
     accept-charset="utf-8"> 
     <ul> 
      <li><label for="username">Username</label> <input type="text" 
       name="username" ></li> 
      <li><label for="password">Password</label> <input 
       type="password" name="password" ></li> 
      <li><input type="submit" value="Login"></li> 
     </ul> 
    </form> 

    <form action="http://localhost:8080/ToDoListProj" method="get"> 
     <input title="Register" type="submit" value="Not Yet A Member?" formaction="register.jsp"/> 
     <br/> 
    </form> 
    </section> 
</body> 

,這是控制器:

package il.ac.hit.controller; 

import il.ac.hit.model.HibernateToDoListDAO; 
import il.ac.hit.model.Item; 
import il.ac.hit.model.ToDoListPlatformException; 
import il.ac.hit.model.User; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.List; 

import javax.servlet.RequestDispatcher; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

import antlr.StringUtils; 

/** 
* Servlet implementation class ToDoListPlatformContrller 
*/ 
@WebServlet("/controller/*") 
public class ToDoListPlatformController extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public ToDoListPlatformController() { 
     super(); 

    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    private boolean isAlphaNumeric(String word) 
    { 
     return word.matches("[a-zA-Z0-9]+"); 
    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     String path = request.getPathInfo(); 
     System.out.println(path); 
     RequestDispatcher dispatcher = null; 

     switch(path) 
     { 
     case "/login": 
     { 
      try { 
       String username = request.getParameter("username"); 
       String password = request.getParameter("password"); 
       if(username != null && password != null && isAlphaNumeric(username) && isAlphaNumeric(password)) 
       { 
        List<User> usersList = il.ac.hit.model.HibernateToDoListDAO.getInstance().getUsersList(); 
        for(User user : usersList) 
        { 
         if(user.getName().equals(username) && user.getPassword().equals(password)) 
         { 
          Cookie cookie = new Cookie("UserId", username); 
          cookie.setPath("/"); 
          response.addCookie(cookie); 
          request.getSession().setAttribute("UserID", username); 
          request.getSession().setAttribute("table", HibernateToDoListDAO.getInstance().getItemsList(username)); 
          dispatcher = getServletContext().getRequestDispatcher("/view.jsp"); 
          break; 
         } 
        } 
       } 
       else 
       { 
        dispatcher = getServletContext().getRequestDispatcher("/wronglogin.jsp"); 
       } 

      } catch (ToDoListPlatformException e) { 

       dispatcher = getServletContext().getRequestDispatcher("/wronglogin.jsp"); 
      } 

      break; 
     } 

     dispatcher.forward(request, response); 
    } 

    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
    } 
} 
當我輸入登錄JSP頁面上的用戶名和密碼,並點擊提交

,正在所示的兩種屬性在URL行中。

這就是我得到:

http://localhost:8080/ToDoListProj/controller/login?username=usernamesample&password=passwordasmple 

我該如何解決呢?

回答

2

您應該將method="get"更改爲method="post"

GET方法將數據添加到URL,如你的情況。處理身份驗證時切勿使用get方法。形式。

0

使用HTTP POST而不是GET:

在JSP

<form name="login" action="controller/login" method="post" 

,並在控制器:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) { 
    // read login data from request like you do now in doGet... 
+0

謝謝,它工作。 現在我有另一個概率。 登錄後,servlet將url轉發給view.jsp巫婆是項目的視圖。 有一個「添加項目」按鈕。 我添加一個項目後,它會返回到視圖頁面並顯示在列表中。 但如果我點擊'刷新'項目再次被添加。 – Dan

+0

@Dan刷新再次觸發「添加項目」動作。有一種模式可以解決這個問題,請參閱https://en.wikipedia.org/wiki/Post/Redirect/Get。 – wero

0

<form name="login" action="controller/login" method="get",該表單使用得到的,所以參數顯示的網址,使用method="post"。現在還記得使這個doPost()將被調用後,所以ou'll必須要麼在複製你的代碼doPost()或致電doGet()doPost()

0

你必須改變getpost方法

<form name="login" action="controller/login" method="post" onsubmit="return validateForm()" 

get通過在URL的末尾附加 將請求參數傳遞給服務器,而在發佈請求表單元素或 參數作爲HTTP主體的一部分傳遞並且不附加在 URL的結尾。因此,無論何時我們需要將一些敏感信息 發送到服務器,都會發送一個post請求。

0

HttpGet是「OK」,如果您使用https而不是http。 Https會加密你的獲取參數(但是整個請求URL可能會在服務器日誌中未加密保存,因此HttpPost將成爲首選方法)。

除了所有的答案說,你應該使用method=post,你可以把代碼doGet在自己的功能,可以說login,這樣你就可以在同一時間使用HttpPost和HTTPGET:

private RequestDispatcher login(HttpServletRequest request, HttpServletResponse response) { 
    String path = request.getPathInfo(); 
    System.out.println(path); 
    RequestDispatcher dispatcher = null; 

    switch(path) 
    { 
    case "/login": 
    { 
     try { 
      String username = request.getParameter("username"); 
      String password = request.getParameter("password"); 
      if(username != null && password != null && isAlphaNumeric(username) && isAlphaNumeric(password)) 
      { 
       List<User> usersList = il.ac.hit.model.HibernateToDoListDAO.getInstance().getUsersList(); 
       for(User user : usersList) 
       { 
        if(user.getName().equals(username) && user.getPassword().equals(password)) 
        { 
         Cookie cookie = new Cookie("UserId", username); 
         cookie.setPath("/"); 
         response.addCookie(cookie); 
         request.getSession().setAttribute("UserID", username); 
         request.getSession().setAttribute("table", HibernateToDoListDAO.getInstance().getItemsList(username)); 
         dispatcher = getServletContext().getRequestDispatcher("/view.jsp"); 
         break; 
        } 
       } 
      } 
      else 
      { 
       dispatcher = getServletContext().getRequestDispatcher("/wronglogin.jsp"); 
      } 

     } catch (ToDoListPlatformException e) { 

      dispatcher = getServletContext().getRequestDispatcher("/wronglogin.jsp"); 
     } 

     break; 
    } 

    return dispatcher; 
} 

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    login(request, response).forward(request, response); 
} 

/** 
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    login(request, response).forward(request, response); 
}