我没有得到post方法没有执行的原因

时间:2014-07-22 04:26:51

标签: java jsp tomcat

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页面:

<%@ 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>

当我点击提交按钮时,网址格式会被更改,并将其重定向到Extractproduct.do但是&#34; hii&#34;没有打印到console.can任何人都帮助我。 我没有得到post方法没有执行的原因。

然后我尝试创建新的servlet,当我尝试运行servelet时,它显示错误: HTTP状态500 内部服务器错误。 虽然我的所有旧的servlet程序运行正常。

1 个答案:

答案 0 :(得分: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的默认方法也会被使用。