Java - 为什么我会收到“HTTP 状态 500 - 实例化 servlet 类时出错”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29494248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Why am I getting "HTTP Status 500 - Error instantiating servlet class"?
提问by NeedsHelp
I created a Dynamic Web Project. I have Tomcat 7.0 installed to run it. This is my first time making a Website with java so I'm completely clueless as to what the problem might be. Here is my code:
我创建了一个动态 Web 项目。我安装了 Tomcat 7.0 来运行它。这是我第一次用 java 制作网站,所以我完全不知道问题是什么。这是我的代码:
web.xml
网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>SimpleTest</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
Hello.java
你好.java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Hello
*/
@WebServlet("/Hello")
public class Hello extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Hello() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello");
}
}
index.jsp
索引.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Hello" method="post">
Name: <input type="text" name="name" value="" />
<input type="submit" name="name" value="Submit" />
</form>
</body>
</html>
The app just prints "Hello" when submit is clicked. I made it like this because I just wanted to make sure the servlet was working first.
单击提交时,该应用程序仅打印“Hello”。我这样做是因为我只是想确保 servlet 首先工作。
采纳答案by Darshan Patel
Servlet can be mapped using web.xml
or annotation
. You are using both of them.
可以使用web.xml
或映射 Servlet annotation
。你正在使用它们。
Remove below portion from web.xml then clean and build your project then you are good to go.
从 web.xml 中删除以下部分,然后清理并构建您的项目,然后您就可以开始了。
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
回答by Annamalai Thangaraj
Remove @WebServlet("/Hello")
from Servlet or servlet mapping from web.xml.Both doing same job, so remove one of the configuration.
@WebServlet("/Hello")
从 web.xml 中删除Servlet 或 servlet 映射。两者都做相同的工作,因此删除其中一个配置。
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>Hello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
回答by SG001
You are doing the same thing with web.xml and annotations , and with 3.0 or greater mostly it makes the decision engine to open both channel.For smaller projects like above it is better to change it with annotations only.With bigger projects or deploying servlet manually go for web.xml.
您正在使用 web.xml 和 annotations 做同样的事情,并且在 3.0 或更高版本中,它主要使决策引擎打开两个通道。对于像上面这样的较小项目,最好仅使用 annotations 更改它。对于较大的项目或部署 servlet手动去 web.xml。
Also on servlet mapping use proper file representations.
同样在 servlet 映射上使用正确的文件表示。
回答by Surya
You have to give the class name as packagename.classname in the Hello. You must have a path instead of using the classname directly
您必须在 Hello 中将类名指定为 packagename.classname。你必须有一个路径而不是直接使用类名