2015-06-14 79 views
0

我設計了3個jsp頁面。基於url參數的重定向/轉發

index.jsp,login.jsp和newUser。 JSP

的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    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>Ho there!</title> 
</head> 
<body> 

This is authorization page. 
<br> 
<a href="/AgileScrumBoard/index.jsp?action=new">Create a new account</a> 
<br> 
<a href="/AgileScrumBoard/index.jsp?action=login">Login with the existing</a> 
</body> 
</html> 

NEWUSER和登錄都只是普通的香草jsp頁面。

這裏的servlet方法的doGet:

String action=request.getParameter("action"); 
     if(action.equals("new")){ 
      response.sendRedirect("/newUser.jsp"); 
     }else if(action.equals("login")){ 
      request.getRequestDispatcher("/login.jsp").forward(request, response); 
     }else{ 
      request.getRequestDispatcher("/login.jsp").forward(request, response); 
      }  
    } 

的問題是:爲什麼當我按下請求調度員沒有按鏈接;噸轉發我/我重定向到指定的jsp頁面?

PS:web.xml中

<servlet> 
     <servlet-name>Index</servlet-name> 
     <jsp-file>/index.jsp</jsp-file> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Index</servlet-name> 
     <url-pattern>/index.jsp</url-pattern> 
    </servlet-mapping> 

    <servlet> 
     <servlet-name>MainController</servlet-name> 
     <servlet-class>controller.MainController</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>MainController</servlet-name> 
     <url-pattern>/index</url-pattern> 
    </servlet-mapping> 

當我在Tomcat的運行我的應用程序: At_tomcat

+0

請求是否在點擊瀏覽器中的鏈接後到達您的'doGet'方法? – hagrawal

+0

是的,請求達到doGet方法。 –

+0

您可以分享您訪問您的index.jsp的URL嗎? – hagrawal

回答

0

兩個鏈接重定向到其中的index.jsp是你已進入頁面,而不是/ AgileScrumBoard/index.jsp添加您的servlet名稱。鏈接應該重定向到servlet然後servlet將重定向到的login.jsp或newUser.jsp

+0

nope,這些鏈接重定向到/login.jsp和/newUser.jsp。 –

0

這是你的index.jsp代碼這樣說:

<a href="/AgileScrumBoard/index.jsp?action=new">Create a new account</a> 
<br> 
<a href="/AgileScrumBoard/index.jsp?action=login">Login with the existing</a> 
</body> 

兩個鏈接重定向到/ AgileScrumBoard /指數。 jsp 我認爲代碼應該是:

<a href="yourservleturl?action=new">Create a new account</a> 
<br> 
<a href="yourservleturl?action=login">Login with the existing</a> 
</body>