无法从HttpClient向Servlet doPost方法发送请求

时间:2013-07-02 01:59:53

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

我在新的SampleServlet中创建了一个名为dynamic web project的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类中放置的断点。而且我不确定为什么会这样,并且没有例外被抛出。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

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

请参阅https://stackoverflow.com/a/1068132/305116以查找与您类似的问题,并http://evgeny-goldin.com/blog/uploading-files-multipart-post-apache/查看一些小教程。

所以在你的主要功能中你需要这样的东西才能起作用:

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);
}