如何将 html 块插入到 java servlet 中

发布于 2024-10-25 04:51:11 字数 2235 浏览 2 评论 0原文

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

我有一个可以在 html 文件中运行的小片段,但我不知道如何让 servlet 将其放入它生成的 html 中。

我并不是真正的编码员,所以我需要一些关于如何使用 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));

}

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.

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.

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

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绻影浮沉 2024-11-01 04:51:12

另一种方法是使用请求调度程序

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

Yet another method would be using 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();
唯憾梦倾城 2024-11-01 04:51:12

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

另请参阅

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

See Also

他是夢罘是命 2024-11-01 04:51:12

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

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

本质上:

  • 在您的 servlet 中,您需要从 response 中获取 PrintWriter Servlet 中的doGet() 方法

    中作为参数接收的对象,

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

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


看到您添加到问题中的代码后,我认为您可以

out.write(HtmlConsts.BODY_OPEN);

通过添加在该行后面添加您的代码片段

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

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

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 PrintWriter from the response object that you receive as parameter in the doGet() method

  • everything you print on that PrintWriter will be sent to the browser

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


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&up_kml_url=https%3A%2F%2Fwebfiles.colorado.edu%2Fwww%2FLSF%2520DB%2520Placemarks5.kmz&up_tour_index=1&up_tour_autoplay=1&up_show_navcontrols=1&up_show_buildings=1&up_show_terrain=1&up_show_roads=0&up_show_borders=1&up_sphere=earth&synd=open&w=700&h=600&title=Embedded+Tour+Player&border=http%3A%2F%2Fwww.gmodules.com%2Fig%2Fimages%2F&output=js"></script>")
心在旅行 2024-11-01 04:51:12

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

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

应该是这样

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

and after that open the scripting tag again
<%

that should be it

彩虹直至黑白 2024-11-01 04:51:12

我不确定这是否是您要找的。我在 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>");

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.

    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>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文