Results 1 to 3 of 3
  1. #1
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Angry need help regarding java servlet.

    HI all..

    I have a html page which contain four text box in which user have 2 fill four integer value.These values will be taken by a servlet and pass to a second servlet which contain code of jfree chart for creation of graph according to user input.The problen is that I am getting only a croos mark instead of getting graph.I do not know where I am doing mistake?:confused:
    I have made a folder dynamicpage in webaaps folder of tomcat4.1 which contain webinf folder and my html page.In web-inf 2 folder classes and lib is there and a web-xml file is there.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Let's brake it down.

    Can you easily send some piece of info from servlet1 to servlet2?

    There are few approaches in serlvet1-to-servlet2 communication but
    for security reasons direct call of one servelt2 instance in servlet1
    is not recommended.

    One approach is to use redirect:

    Java Code:
       HttpServletResponse  response;
       response.sendRedirect(URL+"servlet-name");

    But better approach is to use RequestDispatcher for this.
    Maybe put some Attribute in request of servlet1 and use RequestDispatcher
    to send it to servlet2

    Java Code:
    request.setAttribute("AttributeName","This is the Attribute value.");
    RequestDispatcher dispatcher =  request.getRequestDispatcher(someUrl);
    dispather.forward(request, response); (or redirect instead of forward)
    So can you test this - just simulate all and write in console test values
    from request object in both servlets?

    Here is some help:

    Forwarding and Including Response from other Servlets

    regards

  3. #3
    ras_pari is offline Member
    Join Date
    Oct 2009
    Posts
    40
    Rep Power
    0

    Default

    Big thanx for your reply!!here is my 2 servlet page.

    code of java servlet ChartSetup.java

    Java Code:
    
    package com.karthik.sample;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ChartSetup extends HttpServlet {
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws IOException, ServletException {
    		String line = request.getParameter("t1");
    		String line1 = request.getParameter("t2");
    		String line2 = request.getParameter("t3");
    		String line3 = request.getParameter("t4");
    		double x1 = Double.parseDouble(line);
    		double y1 = Double.parseDouble(line1);
    		double x2 = Double.parseDouble(line2);
    		double y2 = Double.parseDouble(line3);
    		request.setAttribute("x1", x1);
    		request.setAttribute("y1", y1);
    		request.setAttribute("x2", x2);
    		request.setAttribute("y2", y2);
    		getServletContext().getRequestDispatcher("/ChartGenerator").forward(
    				request, response);
    	}
    }
    code of chartgenrator
    Java Code:
    package com.karthik.sample;
    
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.data.xy.XYSeries;
    import org.jfree.data.xy.XYSeriesCollection;
    
    public class ChartGenerator extends HttpServlet {
    	private static final long serialVersionUID = 1L;
    
    	protected void doGet(HttpServletRequest request,
    			HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("image/gif");
    		XYSeries series = new XYSeries("Average Weight");
    		Double x1 = (Double) request.getAttribute("x1");
    		Double y1 = (Double) request.getAttribute("y1");
    		Double x2 = (Double) request.getAttribute("x2");
    		Double y2 = (Double) request.getAttribute("y2");
    		series.add(x1, y1);
    		series.add(x2, y2);
    		XYDataset xyDataset = new XYSeriesCollection(series);
    		JFreeChart chart = ChartFactory.createXYLineChart(
    				"XYLine Chart using JFreeChart", "Age", "Weight", xyDataset,
    				PlotOrientation.VERTICAL, true, true, false);
    		BufferedImage bufferedImage = chart.createBufferedImage(500, 500);
    		ImageIO.write(bufferedImage, "gif", response.getOutputStream());
    	}
    }

Similar Threads

  1. how can i link html+servlet+xml to form servlet
    By balachandarr in forum Java Servlet
    Replies: 0
    Last Post: 04-15-2009, 04:06 PM
  2. Replies: 1
    Last Post: 12-02-2008, 12:23 AM
  3. Replies: 0
    Last Post: 08-30-2008, 11:52 AM
  4. Help with my java servlet homework
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-21-2007, 05:41 PM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •