2011-05-29 48 views
3

我想確定哪個鏈接正在點擊,並相應地設置請求屬性;在控制器中進行處理。控制器是通用的。它將根據JSP中設置的屬性分派到另一個頁面。我怎樣才能做到這一點?根據點擊的鏈接設置請求屬性

編輯:

我按照BalusC的建議,其中

<a href="RandomController/Register">Register</a> 
<a href="RandomController/Login">Login</a> 

RandomController : 



@WebServlet(name = "RandomController", urlPatterns = {"/RandomController/*"}) 
public class RandomController extends HttpServlet { 

protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
    int theRandom = 0; 
    Random myRandom = new Random(); 
    RequestDispatcher dispatcher = null; 

    String pathInfo = request.getPathInfo(); 
    String contextPath = request.getContextPath(); 
    String dispatchesPath = contextPath + pathInfo; 

    System.out.println(pathInfo); 
    System.out.println(contextPath); 
    System.out.println(dispatchesPath); 

    theRandom = myRandom.nextInt(MAX_RANDOM); 
    request.setAttribute("random", theRandom); 

    if(pathInfo.equals("/Login")) { 
     dispatcher = request.getRequestDispatcher("Login.jsp"); 
     dispatcher.forward(request, response); 
    } 
    else { 
     dispatcher = request.getRequestDispatcher("Register.jsp"); 
     dispatcher.forward(request, response); 
    } 
    } 
} 

我已經試過這種方法,但有關嵌套請求分發最大深度異常:20是拋出。 通用控制器是RandomController,其中服務器2生成隨機數並將其設置爲請求對象中的屬性並將其發送到登錄頁面或註冊頁面。

的Login.jsp:

<%-- 
    Document : Register 
    Created on : May 28, 2011, 5:49:35 PM 
    Author  : nicholas_tse 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 

<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <title>Online Store Register Page</title> 

    <link rel="stylesheet" href="style.css" type="text/css" media="screen" /> 
    </head> 
    <body> 
    <jsp:useBean id="randomBean" class="Helper.RandomInt" 
       scope="request"> 
    </jsp:useBean> 

    <h1>Online Store</h1> 

    <p></p> 
    <div align="right"> 
     <h3> 
     <c:if test="${not empty sessionScope.username}"> 
      ! Welcome ${sessionScope["username"]} ! 
      <form action="LogoutController" method="POST"> 
      <input type="submit" name="submit" value="Logout"/> 
      </form> 
     </c:if> 
     </h3> 
    </div> 

    <ul id="nav_list"> 
     <li><a href="http://localhost:8080/OnlineStore">Home</a></li> 
     <li><a href="Product.jsp">Product</a></li> 
     <li><a href="">Add Stock</a></li> 

     <li><a href="">Shopping Cart</a></li> 
     <li><a href="">Order Status</a></li> 
     <li><a href="RandomController/Register">Register</a></li> 
     <li><a href="RandomController/Login">Login</a></li> 
    </ul>  

    <br></br> 
    <p></p> 

    <c:if test="${!not empty sessionScope.username}"> 
     <div id="registerForm" align="center" 
      style="border:1px solid black; width:760px" > 

     <form name="RegisterForm" action="RegisterController" 
       method="POST"> 
      <p> 
      Username : <input type="text" id="username" name="username"> 
      <c:if test="${message.msg} != null" > 
      ${message.msg} 
      </c:if> 
      </p>   

      <p> 
      Password : <input type="password" id="password" name="password" 
      </p> 

      <p> 
      Name : <input type="text" id="name" name="name"> 
      <c:if test="${message.msg} != null" > 
      ${message.msg} 
      </c:if> 
      </p> 

      <p> 
      Address : <input type="text" id="address" name="address"> 
      <c:if test="${message.msg} != null" > 
      ${message.msg} 
      </c:if> 
      </p> 

      <p> 
      State : 
      <select> 
       <option>Selangor</option> 
       <option>Kuala Lumpur</option> 
       <option>Cyberjaya</option> 
       <option>Putrajaya</option> 
      </select> 
      </p> 

      <p></p> 

      <input type="submit" name="submit" value="Register"> 
      <input type="reset" name="clear" value="Clear"> 

      <p></p> 

     </form> 

     </div> 
    </c:if> 
    </body> 
</html> 
+0

此異常是因爲您將請求轉發到與servlet自身的URL模式相匹配的URL。轉發網址需要以'/ /開頭。 – BalusC 2011-05-29 04:52:41

回答

2

如果沒有進一步的信息我能想到的唯一的建議是一個查詢字符串添加到您的網址。就像這樣:

<a href="mycontroller.do?action=page1">Page 1</a> 

這樣你可以看看actiuon參數來確定要在您的控制器的要求去做。

+0

這種方法是有效的,但你有更好的方法。 – peterwkc 2011-05-29 04:36:02

0

您可以添加請求參數。這可以通過兩種方式完成,具體取決於您的表單是否具有POST或GET方法。

+0

我沒有兩種形式,它是在index.jsp的jsp頁面中的兩個鏈接。 – peterwkc 2011-05-29 04:31:13

0

您已經使用屬性submit和不同value屬性的表單中實現了提交按鈕。這應該表示請求將在查詢字符串或POST表單數據中包含submit參數。在服務器端,您可以使用它來確定哪個提交按鈕被點擊。