1

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&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));

}

Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
m13124
  • 51
  • 1
  • 2
  • 3
  • Have you identified the bit of code that ouputs the existing HTML? If so can you post it here? – Jaydee Mar 23 '11 at 13:37

5 Answers5

4

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();
Alex Ureche
  • 123
  • 6
3

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&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>")
MarcoS
  • 12,788
  • 5
  • 36
  • 61
  • Then I past that in the code, a red line comes up under servletexception, and when I try to run it nothing is different. Also, in the servlet code there is a "protected void" section that seems to contain the chunk where I want the google earth aplet to go. How would I insert your snippet into that? – m13124 Mar 23 '11 at 09:12
  • OK: the `protected void ...` is probably a method of your servlet class. If you think that's the point where you need to add your snippet, then it may be useful if you copy the `protected void ...` method in the question: this would allow us to better assist you. – MarcoS Mar 23 '11 at 09:43
  • Thanks marco, I posted it, what do you think? – m13124 Mar 27 '11 at 05:18
3

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

See Also

Community
  • 1
  • 1
jmj
  • 225,392
  • 41
  • 383
  • 426
  • This is a good idea. However I'm afraid our friend m13124 is not interested at the moment in a clean separation of model-view-controller (he says he's not a coder). It looks like he just wants to modify an existing servlet to quickly do what he needs. – MarcoS Mar 23 '11 at 09:46
0

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>");
Edison
  • 1
0

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

sharpner
  • 3,601
  • 3
  • 17
  • 27