从另一个 servlet 调用 servlet
我有两个 servlet,它们运行在不同的 tomcat 服务器上。
我尝试通过以下方式从 servlet2 调用 servlet1,并希望将一个对象写入输出流。
URL url=new URL("http://msyserver/abc/servlet1");
URLConnection con=url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os=con.getOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(pushEmailDTO);
oos.flush();
oos.close();
问题是我无法访问 servlet? 我不知道我错过了什么。
I have two servlets which are running on different tomcat servers.
I and trying to call a servlet1 from servlet2 in the following way and wanted to write an object to output stream.
URL url=new URL("http://msyserver/abc/servlet1");
URLConnection con=url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os=con.getOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(pushEmailDTO);
oos.flush();
oos.close();
The problem is that i am unable to hit the servlet? I cannot figure out what i am missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须先通过 url.connect() 创建连接,然后才能读取/发送数据。 这是违反直觉的,因为名称
openConnection()
表明它已经这样做了,但文档说:这就是
getExpiration()
让它发挥作用的原因:它为您调用connect()
。You must create a connection via
url.connect()
before you can read/send data. This is counter-intuitive since the nameopenConnection()
suggests that it does that already but the docs say:This is why
getExpiration()
makes it work: It callsconnect()
for you.您遇到的错误是什么? 检查地址是否正确。 如果远程服务器在 80 以外的端口上运行,则在构建 URL 时要考虑到这一点。
我还可以建议使用 HttpClient 而不是 URLConnection 。
What is the error you are getting? Check that the address is correct. If the remote server is running in a port other than 80, then take this into consideration when building the URL.
May also I suggest to use HttpClient instead of URLConnection.
我无法理解,但它通过在代码中添加以下行来起作用。
像这样
I cannot unnderstand but it worked by adding the following line in the code.
like this