如何将 html 块插入 java servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5402763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:57:24  来源:igfitidea点击:

How to insert a block of html into a java servlet

javahtmljspservletsjakarta-ee

提问by m13124

I'm messing around with some java source in eclipse for an existing appengine site. I want to get one of the existing pages to show a google earth plugin applet.

我正在为现有的 appengine 站点在 eclipse 中处理一些 java 源代码。我想获取现有页面之一来显示谷歌地球插件小程序。

I have this little snippet that works in an html file but I cant figure out how to get the servlet to put this into the html that it generates.

我有这个在 html 文件中工作的小片段,但我不知道如何让 servlet 将它放入它生成的 html 中。

I am not really a coder so I need some pretty consice instructions on how to get java to make this work.

我不是一个真正的编码员,所以我需要一些关于如何让 Java 使其工作的非常好的说明。

<script src="http://www.gmodules.com/ig/ifr?url=http://code.google.com/apis/kml/embed/tourgadget.xml&amp;up_kml_url=https%3A%2F%2Fwebfiles.colorado.edu%2Fwww%2FLSF%2520DB%2520Placemarks5.kmz&amp;up_tour_index=1&amp;up_tour_autoplay=1&amp;up_show_navcontrols=1&amp;up_show_buildings=1&amp;up_show_terrain=1&amp;up_show_roads=0&amp;up_show_borders=1&amp;up_sphere=earth&amp;synd=open&amp;w=700&amp;h=600&amp;title=Embedded+Tour+Player&amp;border=http%3A%2F%2Fwww.gmodules.com%2Fig%2Fimages%2F&amp;output=js"></script>

===

===

protected void beginBasicHtmlResponse(String pageName, String headContent, HttpServletResponse resp,
      HttpServletRequest req, boolean displayLinks) throws IOException {
resp.addHeader(HOST_HEADER, getServerURL(req));
resp.setContentType(ServletConsts.RESP_TYPE_HTML);
resp.setCharacterEncoding(ServletConsts.ENCODE_SCHEME);
PrintWriter out = resp.getWriter();
out.write(HtmlConsts.HTML_OPEN);
out.write("<link rel=\"icon\" type=\"image/png\" href=\"/odk_color.png\">");
out.write(HtmlUtil.wrapWithHtmlTags(HtmlConsts.HEAD, headContent + HtmlUtil.wrapWithHtmlTags(
    HtmlConsts.TITLE, BasicConsts.APPLICATION_NAME)));
out.write(HtmlConsts.BODY_OPEN);
out.write(HtmlUtil.wrapWithHtmlTags(HtmlConsts.H2, "<FONT COLOR=330066 size=0><img src='/odk_color.png'/>" + HtmlConsts.SPACE + BasicConsts.APPLICATION_NAME) + "</FONT>");
if (displayLinks) {
  UserService userService = UserServiceFactory.getUserService();
  out.write(generateNavigationInfo());
  out.write(HtmlConsts.TAB + HtmlConsts.TAB);
  out.write(HtmlUtil.createHref(userService.createLogoutURL("/"), "Log Out from "
      + userService.getCurrentUser().getNickname()));
  out.write(HtmlConsts.TAB + "<FONT SIZE=1>" + ServletConsts.VERSION + "</FONT>");
}
out.write(HtmlConsts.LINE_BREAK + HtmlConsts.LINE_BREAK);
out.write(HtmlUtil.wrapWithHtmlTags(HtmlConsts.H1, pageName));

}

}

回答by MarcoS

If you have a servlet, then the easiest thing that comes to my mind is the following:

如果您有一个 servlet,那么我想到的最简单的事情如下:

public void doGet(HttpServletRequest request,
                  HttpServletResponse response)
       throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("put your snippet here");
}

Essentially:

本质上:

  • in your servlet you need to get a PrintWriterfrom the responseobject that you receive as parameter in the doGet()method

  • everything you print on that PrintWriterwill be sent to the browser

  • 在您的 servlet 中,您需要PrintWriterresponse作为doGet()方法中的参数接收的对象中获取

  • 您打印的所有内容都PrintWriter将发送到浏览器

Warning: be careful at not messing up what your servlet is already sending to the browser.

警告:小心不要弄乱您的 servlet 已经发送到浏览器的内容。



After seeing the code you added to your question, I think you can add your snippet after the line

看到您添加到问题中的代码后,我认为您可以在该行之后添加您的代码段

out.write(HtmlConsts.BODY_OPEN);

by adding

通过添加

out.write("<script src="http://www.gmodules.com/ig/ifr?url=http://code.google.com/apis/kml/embed/tourgadget.xml&amp;up_kml_url=https%3A%2F%2Fwebfiles.colorado.edu%2Fwww%2FLSF%2520DB%2520Placemarks5.kmz&amp;up_tour_index=1&amp;up_tour_autoplay=1&amp;up_show_navcontrols=1&amp;up_show_buildings=1&amp;up_show_terrain=1&amp;up_show_roads=0&amp;up_show_borders=1&amp;up_sphere=earth&amp;synd=open&amp;w=700&amp;h=600&amp;title=Embedded+Tour+Player&amp;border=http%3A%2F%2Fwww.gmodules.com%2Fig%2Fimages%2F&amp;output=js"></script>")

回答by Alex Ureche

Yet another method would be using Request Dispatcher:

另一种方法是使用Request Dispatcher

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("Included HTML block:");
    request.getRequestDispatcher("/pathToFile/block.html").include(request, response); 
    out.close();

回答by Jigar Joshi

I would recommend you to use jsp for view. Use servlet as just controller

我建议您使用jsp进行查看。使用 servlet 作为控制器

See Also

也可以看看

回答by Edison

I'm not sure if this is what your looking for. i use this in java EE doPost block. or perhaps u can use this as reference for the equivalent syntax to what your looking for.

我不确定这是否是您要找的。我在 java EE doPost 块中使用它。或者也许您可以将其用作与您要查找的等效语法的参考。

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.print("<html>");
    out.print("<head>");
    out.print("<title>");
    out.print("</title>");
    out.print("<h1>view accounts</h1>");
    out.print("</head>");

回答by sharpner

close the scripting code... with %> then you can write anything you want in plain html

关闭脚本代码...使用 %> 然后你可以用纯 html 编写任何你想要的东西

and after that open the scripting tag again <%

然后再次打开脚本标签 <%

that should be it

应该是这样