2012-05-05 32 views
0

我有一個index.jsp文件有兩種不同類型的表單多形式不同的Servlet

<form action="searchpath" name="searchForm" method="get"> 
<p>BedType</p> 
    <select name="bedType"> 
    <jsp:include page="/WEB-INF/embeds/bedType.jsp"/> 
    </select> 
    <p>Max Price</p> 
    <input size="10" maxlength="10" name="mPrice"/> 
    <br/><br/> 
    <input name="Reset" type="reset" value="RESET" class="input"/> <input type="submit" class="input" value="SUBMIT"/> 
</form> 

<form action="loginController" method="post" id="loginForm"> 
     Please Login :<input name="username" size="30" maxlength="30"/> 
     Password : <input name="pass" type="password" size="30" maxlength="30"/>  
     <input type="submit" value="SUBMIT" class="input"/> 
    </form> 

我認爲action字段類型指向交互web.xml<url-pattern>/searchpath</url-pattern>將允許我將這些表單中的任何輸入信息定向到該路徑的servlet。也就是說,第一種形式將與我的searchpath servlet進行交互。當我嘗試提交表格並打印出信息時,似乎沒有任何工作,我一直收到http 404 error。有人能幫我解決這個問題嗎?

的web.xml:

<servlet-mapping> 
    <servlet-name>Search</servlet-name> 
    <url-pattern>/searchpath</url-pattern> 
    </servlet-mapping> 

我search.java Servlet中:

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

    response.setContentType("text/html"); 
    PrintWriter out = response.getWriter(); 
    out.println("<HTML>"); 
    out.println("<HEAD>"); 
    out.println("<TITLE>Static Servlet</TITLE>"); 
    out.println("</HEAD>"); 
    out.println("<BODY>"); 
    out.println("<h1>WTF</h1>"); 
     out.println("</BODY>"); 
    out.println("</HTML>"); 
+2

在'form'的'action'屬性中添加上下文路徑 –

回答

1

檢查瀏覽器URL。你錯過了上下文。

讓說吧,您正在運行http://localhost:8080/test/index.jsp其中test是您的上下文路徑。

所以,當調用Servlet它應該像http://localhost:8080/test/searchpath

就你而言,它不是那樣的。

所以,添加cotextpath將解決您的問題。

例如action="<%=request.getContextPath()%>/searchpath"