这是我的servlet,当我尝试在我的eclipse中运行动态Web项目时,它会抛出404错误,我的web.xml配置有问题。 web.xml中的servlet类导致各自的servlet(crtl +鼠标单击)。我的web.xml配置可能出错了,请帮忙!
package java_barchartservlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
//import java_barchart.ImageDemo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/**
* Servlet implementation class BarChartExample
*/
public class BarChartExample extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init()
{
//initialize( or add a log statement to debug)
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
/* Step - 1: Define the data for the bar chart */
DefaultCategoryDataset my_bar_chart_dataset = new DefaultCategoryDataset();
my_bar_chart_dataset.addValue(34, "WMOS", "2013");
my_bar_chart_dataset.addValue(45, "DOM", "2013");
my_bar_chart_dataset.addValue(45, "TLM", "2013");
my_bar_chart_dataset.addValue(22, "WMOS", "2014");
my_bar_chart_dataset.addValue(12, "DOM", "2014");
my_bar_chart_dataset.addValue(12, "TLM", "2014");
my_bar_chart_dataset.addValue(56, "WMOS", "2015");
my_bar_chart_dataset.addValue(98, "DOM", "2015");
my_bar_chart_dataset.addValue(98, "TLM", "2015");
/* Step -2:Define the JFreeChart object to create bar chart */
JFreeChart BarChartObject=ChartFactory.createBarChart("productVsYear - Bar Chart","Year","Modifications",my_bar_chart_dataset,PlotOrientation.VERTICAL,true,true,false);
/* Step -3: Write the output as PNG file with bar chart information */
int width=640; /* Width of the image */
int height=480; /* Height of the image */
File BarChart=new File("C:/Users/seramaiah/Pictures/output_chart.png");
ChartUtilities.saveChartAsPNG(BarChart,BarChartObject,width,height);
FileInputStream inputStream = new FileInputStream("C:/Users/seramaiah/Pictures/output_chart.png"); //read the file
response.setHeader("Content-Disposition","attachment; filename=test.txt");
//new ImageDemo("C:/Users/seramaiah/Pictures/output_chart.png");
}
catch (Exception i)
{
System.out.println(i);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>java_barchartservlet.BarChartExample</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
</web-app>
答案 0 :(得分:0)
当我尝试在我的日食中运行动态Web项目时,它会抛出 404错误,我的web.xml配置有问题。
运行网络应用时,您的默认网页是什么?您甚至没有指定欢迎文件。您在运行应用时尝试访问的是什么?没有servlet映射,没有欢迎文件。不是404显而易见的。
更新您的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>java_barchartservlet.BarChartExample</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/fireServlet</servlet-class>
<servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
您可以从index.jsp
发送servlet请求OR
直接将servlet映射到上下文路径
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>java_barchartservlet.BarChartExample</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/</servlet-class>
<servlet-mapping>
</web-app>