We help IT Professionals succeed at work.

output a jsp page from a servlet

paddyfitz2005
on
Medium Priority
1,621 Views
Last Modified: 2013-11-24

i'm trying to output a jsp page from a servlet but it won't display properly, anyone any ideas how i can display it as a jsp:
This is the code in the servlet


response.setContentType("text/html");
out.println("<%@ taglib prefix='c' uri='http://java.sun.com/jstl/core_rt' %>");
out.println("<%@ page contentType='text/html; charset=iso-8859-1' language='java' import='java.sql.*' errorPage='' %>");
out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");


out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
out.println("<HEAD>");
out.println("<TITLE>JFreeChart Servlet Demo 2</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<%@ include file='/chart1.jspf'%>");

out.println("<H2>JFreeChart Servlet Demo</H2>");
out.println("<P>");
out.println("Please choose a chart type:");
out.println("<FORM ACTION=\"ServletDemo2\" METHOD=POST>");
String pieChecked = (param.equals("pie") ? " CHECKED" : "");
String barChecked = (param.equals("bar") ? " CHECKED" : "");
String timeChecked = (param.equals("time") ? " CHECKED" : "");
out.println("<INPUT TYPE=\"radio\" NAME=\"chart\" VALUE=\"pie\"" + pieChecked
+ "> Pie Chart");
out.println("<INPUT TYPE=\"radio\" NAME=\"chart\" VALUE=\"bar\"" + barChecked
+ "> Bar Chart");
out.println("<INPUT TYPE=\"radio\" NAME=\"chart\" VALUE=\"time\"" + timeChecked
+ "> Time Series Chart");
out.println("<P>");
out.println("<INPUT TYPE=\"submit\" VALUE=\"Generate Chart\">");
out.println("</FORM>");
out.println("<P>");
out.println("<IMG SRC=\"ServletDemo2ChartGenerator?type=" + param
+ "\" BORDER=1 WIDTH=400 HEIGHT=300/>");
out.println("<%@ include file='/chart2.jspf'%>");
out.println("</BODY>");
out.println("</HTML>");
Comment
Watch Question

Commented:
You can't output JSP code from a servlet.  You have to output HTML.
If you want to invoke another JSP/Servlet as part of a servlet, use the request dispatcher and either forward or include.

ie replace
out.println("<%@ include file='/chart1.jspf'%>");

with
RequestDispatcher rd = request.getRequestDispatcher("/chart1.jsp")'
rd.include(request, response);

This page looks like it would work better as a JSP as it is 90% HTML you are outputting.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Siva Prasanna KumarPrincipal Solutions Architect
CERTIFIED EXPERT
Top Expert 2006
Commented:
yes thats correct you just need to diaplay the html.
and in the HTML instead of includeing jsp file just use request dispatcher.
RequestDispatcher reqDisp = request.getRequestDispatcher("/chart1.jsp")'
reqDisp.include(request, response);

and keep all tht Jsp code in chart1.jsp.
CERTIFIED EXPERT
Top Expert 2006
Commented:
To run a JSP page inside a Servlet you would need to call the Jasper compiler which is pretty much impossible.

Also, from an architectural point of view, what you are doing amounts to using a dynamic page (servlet) to create a dynamic page (jsp) to create a static page (html). You can see that there is an extra redundant step as you are calling 2 dynamic steps when you only need one

The above suggestions are useful but the best one is just to replace this servlet with a JSP. There's only a couple of pieces of code anyway which can be easily replaced with scriptlets or preferably JSTL.

Also, you should use a <c:import> not a static @include as mixing standards can lead to complications later.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.