简体   繁体   English

JAVA servlet中没有内容的放置/发布请求

[英]put/post request without content in JAVA servlet

I want to send a PUT/POST request to another JAVA servlet informing that something has completed.I dont want to send any content. 我想向另一个JAVA servlet发送一个PUT/POST请求,通知已完成某些操作。我不想发送任何内容。 Say the URL is; 说URL是;

putURL = "http://"+recorderIp+":"+recorderPort +"/vodingest/rec/"+recId+"/completed"

I dont have any content(file content) to sent.The servlet once see this request in his PUT/POST methods , he will know that something has completed and does the needful. 我没有任何要发送的内容(文件内容)。servlet一旦在他的PUT/POST方法中看到了这个请求,他就会知道事情已经完成并且有需要。

How can I do this in JAVA servlet. 如何在JAVA Servlet中执行此操作。 Please help me. 请帮我。

we have many methods to POST request to another servlet. 我们有很多方法可以将请求POST到另一个servlet。

1) Send Redirect 1)发送重定向

request.sendRedirect("myServletName.java")

2) LInk 2)行

out.println("<a href=\"http://localhost:8080/HelloWorld/myServlet\"> Hello World Servlet </a>");

3) Request Dispatcher 3)请求调度员

 request.getRequestDispatcher("/myServletName").forward(request, response);

and in request object you can pass values also to another servlet. 在请求对象中,您还可以将值传递给另一个servlet。

request.setParams("recId" , "completed")

I guess the third option is better for you. 我猜第三个选择对您来说更好。 Hope that will solve your problem. 希望能解决您的问题。

First, PUT is by default not enabled on web servers (including Tomcat), so before you try this, make sure that PUT is enabled or stick to POST. 首先,默认情况下,Web服务器(包括Tomcat)上未启用PUT,因此在尝试此操作之前,请确保已启用PUT或坚持使用POST。 As mentioned above dispatching is possible if your two servlets (assuming there are 2 servlets) are under the same container. 如上所述,如果您的两个servlet(假设有2个servlet)在同一个容器中,则可以进行分派。 Otherwise if you are doing it from a stand alone client, the approach would be different. 否则,如果您是从独立客户端执行此操作,则方法将有所不同。 The below should work in all environments: 以下内容适用于所有环境:

Use HttpURLConnection ( https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html ) Use setRequestMethod to POST Use doInput/doOutput if you need to write and read from the stream (in your case I don't think either is necessary). 使用HttpURLConnection( https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html )使用setRequestMethod进行POST如果您需要在流中进行读写操作,请使用doInput / doOutput情况下,我认为两者都不是必须的)。

This is relatively straight forward method and would work. 这是相对简单的方法,并且可以工作。 If you need more flexibility of the client, you could use Apache Commons HTTP Client. 如果需要更大的客户端灵活性,则可以使用Apache Commons HTTP Client。

One point of note, if you have nothing to write and nothing really to read, why are you thinking of using PUT? 需要注意的一点是,如果您没有什么可写的,也没有真正可阅读的,那么您为什么要考虑使用PUT?

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

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