简体   繁体   English

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

[英]Not able to send request to Servlet doPost method from HttpClient

I have created a Servlet class named SampleServlet in a new dynamic web project . 我在一个新的dynamic web project创建了一个名为SampleServlet的Servlet类。 I have started the server in debug mode. 我已以调试模式启动服务器。 Below is the code in my 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());

}

And my web.xml file is like this- 我的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>

I have put the breakpoint in both the methods above. 我已经在以上两种方法中都设置了断点。 As soon as I hit this url from the browser- 当我从浏览器中点击该网址时,

http://localhost:8080/ServletExample/SampleServlet

my breakpoint always gets hit in doGet method. 我的断点总是在doGet方法中被击中。

Now I have created a new Java Project in the eclipse which is my client and which will call the servlet doPost method as I need to pass an XML file to my servlet as a request. 现在,我在eclipse中创建了一个新的Java项目,它是我的客户端,它将调用servlet doPost方法,因为我需要将XML文件作为请求传递给servlet。

Below is my code- 以下是我的代码-

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

But somehow as soon as I run my above main program as a Java Application, it doesn't hit the breakpoint I have put in my servlet class. 但是以某种方式,只要我将上述主程序作为Java应用程序运行,它就不会达到我在servlet类中设置的断点。 And I am not sure why it is happening and no exceptions is getting thrown. 而且我不确定为什么会发生这种情况,并且不会引发任何异常。 Any idea why it is happening? 知道为什么会这样吗?

Your contentType is wrong, to upload a file to a web server you need to specify the multipart format. 您的contentType错误,要将文件上传到Web服务器,您需要指定分段格式。

See https://stackoverflow.com/a/1068132/305116 for a problem like yours, and http://evgeny-goldin.com/blog/uploading-files-multipart-post-apache/ for a little tutorial. 有关类似您的问题的信息,请参见https://stackoverflow.com/a/1068132/305116 ;有关本教程的信息,请参见http://evgeny-goldin.com/blog/uploading-files-multipart-post-apache/

So in your main function you need something like this for it to work: 因此,在您的主要功能中,您需要执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM