tags:

views:

46

answers:

1

Hello,

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 Servlet2 getting a request? Where i'm going wrong or what i'm missing?

Kind regards, Adriana

+2  A: 

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.

BalusC
Yes exactlly that was! I know how to send some parameters and to retrieve the output of the servlet but the display in the browser was my problem. Silly me!! Many thanks, got it, finished it! Have a nice weekend.
Adriana
Cheers! You're welcome. Don't forget to mark the answer accepted if it helped in solving the problem :) See also http://stackoverflow.com/faq.
BalusC
@Adriana - if the answer worked for you, mark it as accepted (tick below the vote counter)
Bozho