2015-05-07 154 views
1

試圖將jsp提交給servlet。在web.xml映射期間出錯。將JSP映射到servlet

的index.jsp

<form method="POST" action="Validate"> 
    <input type="submit" value="Submit" /> 
</form> 

的web.xml

<servlet> 
    <servlet-name>validate</servlet-name> 
    <servlet-class>com.test.Validate</servlet-class> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>validate</servlet-name> 
    <url-pattern>/Validate</url-pattern> 
    </servlet-mapping> 

文件夾結構

enter image description here

然而,當,我嘗試在服務器運行index.jsp,我得到"server cannot be started"錯誤時servlet映射從web.xml中刪除

錯誤去

Validate.java

package com.test; 

import java.io.IOException; 
import java.io.PrintWriter; 

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

/** 
* Servlet implementation class Validate 
*/ 
@WebServlet(description="validation", urlPatterns={"/Validate"}) 
public class Validate extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public Validate() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     PrintWriter out = response.getWriter(); 
     response.setContentType("text/html"); 
     System.out.println("validate called"); 
    } 

當我作爲服務器上運行,它的工作原理Validate.java

但是,當我作爲服務器在index.jsp上運行,然後單擊提交。它將 重定向到http://localhost:8080/TestApp/Validate,並且控制檯上沒有任何內容。

+1

請分享一下'Validate.java'的代碼嗎? –

+0

已添加Validate.java – user544079

+0

當您點擊提交時,您正在執行HTTP POST,但您沒有在您的servlet中實施doPost方法。 – Crx

回答

1

問題是您使用的都是註釋映射以及web.xml,所以您最好刪除其中的一個以使代碼正常工作。我相信註釋更好。

@WebServlet(description="validation", urlPatterns={"/Validate"})