简体   繁体   中英

Execute doGet in Servlet from java prgram

I try execute method doGet (just show popup) in servlet from java program.

Code in java program:

URL url = new URL( "http://localhost:9999/xxx/screen?msg=VVU6" ); 
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); 
line = in.readLine(); 
System.out.println( line ); 
in.close(); 

Code in doGet:

PrintWriter out = response.getWriter(); 
out.println("<script type='text/javascript'>");
out.println("alert('peek-a-boo');");
out.println("</script>");

But, when run it not show popup alert, it just print data in console.

May you help me in this case ?

Thanks all!

您可以使用Selenium Web驱动程序与来自Java代码的URL进行交互: http : //www.seleniumhq.org/docs/03_webdriver.jsp

The code you have written in doGet writes the markup you have written to the outputstream . When the URL mapped to the servlet is hit from a web browser or a browser equivalent(web drivers or in-memory browsers for example), it will be rendered as a html markup .

Read the basics of J2EE servlet and JSP to get a clear understanding of the usage and use case. A console cannot be used to generate popups.

pre-condition : your Servlet is running in a Servlet Container (Jetty, Tomcat) at http://localhost:9999/xxx

If your routing is correct, (you access the doGet method in your Servlet by pointing your browser at http://localhost:9999/xxx/screen ) you should be able to see the alert with:

  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<html><head></head><body>");
  out.println("<script>alert('peek-a-boo');</script>");
  out.println("</body></html>");

If you wanna perform the GET from a JavaSE program please give a look to any small REST Client library like Unirest to be up to speed quickly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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