output a jsp page from a servlet

Posted on 2006-03-20
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

out.println("<%@ taglib prefix='c' uri='' %>");
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' ''>");

out.println("<html xmlns=''>");
out.println("<TITLE>JFreeChart Servlet Demo 2</TITLE>");
out.println("<%@ include file='/chart1.jspf'%>");

out.println("<H2>JFreeChart Servlet Demo</H2>");
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("<INPUT TYPE=\"submit\" VALUE=\"Generate Chart\">");
out.println("<IMG SRC=\"ServletDemo2ChartGenerator?type=" + param
+ "\" BORDER=1 WIDTH=400 HEIGHT=300/>");
out.println("<%@ include file='/chart2.jspf'%>");
Question by:paddyfitz2005
    LVL 4

    Accepted Solution

    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'%>");

    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.
    LVL 23

    Assisted Solution

    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.
    LVL 29

    Assisted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now