Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

output a jsp page from a servlet

Posted on 2006-03-20
3
Medium Priority
?
1,579 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>");
0
Comment
Question by:paddyfitz2005
3 Comments
 
LVL 4

Accepted Solution

by:
evnafets earned 1200 total points
ID: 16241526
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
 
LVL 23

Assisted Solution

by:Siva Prasanna Kumar
Siva Prasanna Kumar earned 400 total points
ID: 16243673
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
 
LVL 29

Assisted Solution

by:bloodredsun
bloodredsun earned 400 total points
ID: 16244969
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Suggested Courses

580 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