【问题标题】:Not able to send request to Servlet doPost method from HttpClient无法从 HttpClient 向 Servlet doPost 方法发送请求
【发布时间】:2013-07-02 01:59:53
【问题描述】:

我在新的dynamic web project 中创建了一个名为SampleServlet 的Servlet 类。我已经在调试模式下启动了服务器。下面是我的 Servlet 中的代码-

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

    BufferedReader reader = request.getReader();
    System.out.println(reader.readLine());

}

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

    BufferedReader b = new BufferedReader(request.getReader());  
    System.out.println(reader.readLine());

}

而我的web.xml文件是这样的-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>ServletExample</display-name>
  <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>
  <servlet>
    <description></description>
    <display-name>SampleServlet</display-name>
    <servlet-name>SampleServlet</servlet-name>
    <servlet-class>com.servlet.example.SampleServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SampleServlet</servlet-name>
    <url-pattern>/SampleServlet</url-pattern>
  </servlet-mapping>
</web-app>

我在上述两种方法中都设置了断点。一旦我从浏览器中点击此网址-

http://localhost:8080/ServletExample/SampleServlet

我的断点总是在 doGet 方法中被命中。

现在我在 eclipse 中创建了一个新的 Java 项目,它是我的客户端,它将调用 servlet doPost 方法,因为我需要将 XML 文件作为请求传递给我的 servlet。

下面是我的代码-

public static void main(String[] args) {

    HttpPost post = new HttpPost("http://localhost:8080/ServletExample/SampleServlet");
    post.setHeader("Content-Type", "application/xml");
    post.setEntity(new StringEntity(generateNewXML()));
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
}

但不知何故,一旦我将上面的主程序作为 Java 应用程序运行,它就不会命中我在 servlet 类中放置的断点。而且我不确定它为什么会发生,并且没有抛出异常。知道为什么会这样吗?

【问题讨论】:

  • @AVD,我已经添加了这些,但它仍然没有达到我的断点。肯定还有别的东西。
  • 您使用的是什么应用服务器?访问/错误/其他服务器日志中的任何内容。一种可能性是应用程序服务器由于最大帖子大小设置而拒绝请求。尝试用“hello”替换 generateNewXML() 看看会发生什么。
  • 你的主程序在什么包里? doPost 确实受到保护,所以您不应该调用服务方法吗?
  • @happybuddha,我的主程序都在不同的Java项目中。
  • Aaah.. 这就是我得到的java.net.SocketException: Software caused connection abort: recv failed 知道为什么会这样吗?

标签: java servlets http-post apache-httpclient-4.x


【解决方案1】:

您的 contentType 错误,要将文件上传到 Web 服务器,您需要指定多部分格式。

请参阅https://stackoverflow.com/a/1068132/305116 了解类似您的问题,http://evgeny-goldin.com/blog/uploading-files-multipart-post-apache/ 了解小教程。

所以在你的 main 函数中,你需要这样的东西才能让它工作:

public static void main(String[] args) {

    HttpPost post = new HttpPost("http://localhost:8080/ServletExample/SampleServlet");
    MultipartEntity entity = new MultipartEntity();
    entity.addPart( "someXMLfile", new StringBody(generateNewXML(), "application/xml",
        Charset.forName( "UTF-8" )));
    post.setEntity(entity);

    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(post);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-06-14
    • 2021-03-30
    相关资源
    最近更新 更多