简体   繁体   English

从Java prgram在Servlet中执行doGet

[英]Execute doGet in Servlet from java prgram

I try execute method doGet (just show popup) in servlet from java program. 我尝试从Java程序的servlet中执行doGet方法(只是显示弹出窗口)。

Code in java program: Java程序中的代码:

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: 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 . 您在doGet中编写的代码会将您编写的标记写入到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 . 当从Web浏览器或等效的浏览器(例如,Web驱动程序或内存浏览器)中命中映射到servlet的URL时,它将呈现为html markup

Read the basics of J2EE servlet and JSP to get a clear understanding of the usage and use case. 阅读J2EE servlet和JSP的基础知识,以清楚地了解其用法和用例。 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 前提条件 :您的Servlet在http:// localhost:9999 / xxx的Servlet容器(Jetty,Tomcat)中运行

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: 如果路由正确(通过将浏览器指向http:// localhost:9999 / xxx / screen来访问Servlet中的doGet方法),则应该可以看到警报:

  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. 如果要从JavaSE程序执行GET,请查看Unirest等任何小型REST客户端库,以加快速度。

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

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