java - 如何使用 HttpClient 将 XML 文件作为请求传递给我的 Servlet 之一?

标签 java xml servlets request

下面是我的代码,我试图在其中生成一个 XML 文件,然后一旦我生成一个 XML,我就需要将此 XML 文件发送到我自己的一个 Servlet,它在我的机器上本地运行。我能够生成一个 XML 文件,但我不确定如何将该 XML 文件发送到我的 servlet 之一,以便在 doGet 方法中,我可以解析该 XML 文件。

public static void main(String[] args) throws SAXException, XPathExpressionException, ParserConfigurationException, IOException,
    TransformerException {

String xml = generateXML();
send("http://localhost:8080/ServletExample/SampleServlet", xml);
}


/**
 * A simple method to generate an XML file
 *
 */  
public static String generateXML(String conn, String funcAddr) throws ParserConfigurationException, SAXException, IOException,
    XPathExpressionException, TransformerException {

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

// Some code here to make an XML file

String xmlString = sw.toString();

// print xml
System.out.println("Here's the xml:\n" + xmlString);

return xmlString;
}


/**
 * A simple method to send the XML to servlet class
 *
 */
public static void send(String urladdress, String file) throws MalformedURLException, IOException {
String charset = "UTF-8";
String s = URLEncoder.encode(file, charset);

// I am not sure what should I do here so that I can pass the 
//  above XML file that I made to my servlet class.

}

我的 servlet 在 8080 上本地运行。下面是我的 servlet 类的片段-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    BufferedReader reader = request.getReader();

    //Parse the XML file here?

    System.out.println(reader.readLine());

}

更新代码:-

我创建了一个名为 SampleServlet 的 Servlet 类在新 dynamic web project .我已经在 Debug模式下启动了服务器。以下是我的 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 类中放置的断点。而且我不确定为什么会发生这种情况并且没有抛出异常。知道为什么会这样吗?

最佳答案

一切正常,我只是将您的代码复制到一个新项目中

public class SampleServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doPost is called");
    }
}

并运行客户端:
public class PostClient {

    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpPost post = new HttpPost("http://localhost:8080/ServletExample/SampleServlet");
        post.setHeader("Content-Type", "application/xml");
        post.setEntity(new StringEntity("<xml></xml>"));
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(post);
    }

}

消息“doPost被调用”打印在cosole中,一切都按预期工作

关于java - 如何使用 HttpClient 将 XML 文件作为请求传递给我的 Servlet 之一?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17415698/

相关文章:

Java,如何停用夏令时?

java - 从不同的jsp调用相同的servlet以达到不同的目的

forms - 成功登录Tomcat表单后重定向

java - 从Notepad++复制java/xml代码到Ms word

java - 在一维游戏中实现第二艘战舰 "River Battleship"

java - 使用jsoup解析XML——防止jsoup来自 "cleaning"<link>标签

java - 忽略 JAXB 中的根元素

java - 为什么我不能在 SAXParser 中打开这个 XML?

java - 调试与日历日期 GMT 相关的简单 java 代码

java - 连接到需要用户/密码的网络