[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

Inclusion of servlet in a jsp

I have a servlet named "chartServlet.java"

       It contains the fiollowing lines of code

public class chartServlet extends HttpServlet {

    /**
     * Process a GET request.
     *
     * @param request  the request.
     * @param response  the response.
     *
     * @throws ServletException if there is a servlet related problem.
     * @throws IOException if there is an I/O problem.
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
                GeneralUtility chartUtil =  GeneralUtility.getInstance();
             BufferedImage pieChart3D = chartUtil.createPieChart();                              
          response.setContentType("image/jpeg");
          OutputStream out = response.getOutputStream();
        ChartUtilities.writeBufferedImageAsPNG(out,pieChart3D);
              }
}

I need to include this chartServlet in my jsp inorder to display the piechart to the user.
Could anyone pls help me out in this context?
0
ramsharma23
Asked:
ramsharma23
2 Solutions
 
objectsCommented:
specify the path to your servlet as the src attribute for you img tag
eg.

<img src="\servlet\ChartServet">
0
 
bloodredsunCommented:
Objects is correct that you should just use the image tag to generate the jpeg via the servlet.

A couple of points though. Make sure that your servlet is in a package as some servlet containers will cause you unnecessary problems if you don't have a package declaration.

e.g.
package  ramsharma23;
public class chartServlet extends HttpServlet {
.....

This means that the class file for this example would be found in <webapp>WEB-INF/classes/ramsharma23/chartServlet .class

Also, never use the invoker servlet to access your servlets (for lots of reasons) but make sure that they are mapped in the web.xml file. When you do this, it's also common practise to add the suffix ".do" to signify that it's a servlet in the url you map to.

     <servlet>
        <servlet-name>Charter</servlet-name>
        <servlet-class>ramsharma23.chartServlet </servlet-class>
    </servlet>

    <servlet-mapping>
          <servlet-name>Charter</servlet-name>
          <url-pattern>/Charter.do</url-pattern>
    </servlet-mapping>

So objects img tag becomes <img src="/Charter.do"> as specified in the url-pattern tag above.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now