output a jsp page from a servlet


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>");
paddyfitz2005Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evnafetsCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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.
0
bloodredsunCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.