开发者

Starting a Servlet from a java class like from HTML code

What I am trying to do is to run a simple Servlet from a java class.

Actually I have a Servlet1 that is started from an html code through a post action. That servlet outputs some other html page but also calls a Java class. What I try to do now is to start a Servlet2 that outputs html code from that java class. I did:

URL url = new URL("http://localhost:8080/WebApplication/Servlet2");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);

but the servlet2 is not opened in the browser even though i output in the proccessRequest method some html code.

Isn't the Serv开发者_Python百科let2 getting a request? Where i'm going wrong or what i'm missing?

Kind regards, Adriana


Your question is vague and ambiguous and the functional requirement is unclear, so I can't give a detailed answer how to achieve the particular functional requirement.

But at least, the following phrase is a bit odd:

but the servlet2 is not opened in the browser

That particular code will in no way open the mentioned URL in some webbrowser. That Java code does in no way interact with any of the installed webbrowsers at the local machine. If your sole intent is to open the particular URL in the local machine's default webbrowser (e.g. Firefox, MSIE, etc), then you rather need Desktop#browse(). E.g.

Desktop.getDesktop().browse(new URI("http://google.com"));

If that's also not what you're looking for, then I don't know. All I can suggest is to get yourself through this topic to learn how to fire and handle HTTP requests programmatically using Java code. Probably you're monitoring the webserver's log files to see if the servlet is been requested. This will only happen when URLConnection is actually firing the request. Only calling openConnection() won't do that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜