【问题标题】:I am not getting the reason why post method is not executed我不明白为什么不执行 post 方法的原因
【发布时间】:2014-07-22 04:26:51
【问题描述】:
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>

当我单击提交按钮时,url 模式发生了变化,并将其重定向到 Extractproduct.do 但“hii”没有打印到控制台。谁能帮助我。 我不明白为什么不执行 post 方法的原因。

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

【问题讨论】:

  • ""hii" 未打印到控制台" - 为什么您希望将任何内容打印到控制台?控制台如何与 Web 服务相关联?
  • 我的主要目的不是在控制台打印“hii”。我只是想确保执行doPost方法。
  • 不是尝试打印到控制台,而是尝试重定向到网页并查看重定向是否发生。例如:response.sendRedirect("http://www.google.com");
  • @user3773682 - 我检查了它是否已执行并且在控制台中可见。我制作了一个简单的 html 页面来“调用”servlet。我不知道为什么你没有得到相同的输出。您还可以添加 3-4 行代码来获取和发布以生成 html 响应以进行测试。
  • @BoratSagdiyev:完全同意你的看法。我也查过了。

标签: java jsp tomcat


【解决方案1】:

我不确定,但我相信你必须设置它们@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 获取默认方法。

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多