2014-07-22 52 views
1
public class ExtractProduct extends HttpServlet 
{ 
    private static final long serialVersionUID = 1L; 


    public ExtractProduct() { 
     super(); 
    } 


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     System.out.println("bye"); 
    } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     System.out.println("hiiiii"); 
    } 

} 

JSP頁面中的原因:我沒有得到,爲什麼不執行POST方法

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<!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=ISO-8859-1"> 
    <title>Insert title here</title> 
    </head> 
    <body> 
    <form name="displayProduct" method='post' action='ExtractProduct.do' > 
     <input type="submit" value="submit"></input> 
    </form> 
    </body> 
</html> 

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> 
<web-app id="WebApp_ID"> 
    <display-name>adsfa</display-name> 

<servlet> 
    <servlet-name>ExtractProduct</servlet-name> 
    <display-name>ExtractProduct</display-name> 
    <description></description> 
    <servlet-class> 
    controller.ExtractProduct</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>ExtractProduct</servlet-name> 
    <url-pattern>/ExtractProduct.do</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
</welcome-file-list> 

當我點擊提交按鈕的url模式被改變,它將其重定向到Extractproduct.do,但「hii」不會打印到控制檯。任何人都可以幫助我。 我沒有得到爲什麼不執行post方法的原因。

然後我試圖創建新的servlet,當我試圖運行servelet時,它向我顯示一個錯誤: HTTP狀態500 內部服務器錯誤。 雖然我所有的舊的servelet程序運行正常。

+0

「hii」未打印到控制檯「 - 您爲什麼期望將任何內容打印到控制檯?控制檯如何與web服務相關? – alfasin

+1

我的主要目的不是在控制檯中打印「hii」,我只是想確保執行doPost方法。 – Prasenjit

+0

而不是試圖打印到控制檯,嘗試重定向到一個網頁,看看是否發生重定向。例如:'response.sendRedirect(「http://www.google.com」);' – alfasin

回答

0

我不知道,但我相信,你必須將他們@Override

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    System.out.println("bye"); 
} 

@Override 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    System.out.println("hiiiii"); 
} 

即使你不這樣做從HttpServlet的獲取方法中使用這種默認的方法。

相關問題