从 tomcat 调用 servlet 会抛出 HTTP 状态 500 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26623311/
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
Calling servlet from tomcat throws HTTP Status 500 java.lang.NullPointerException
提问by bugcode09
I am new to J2EE and build a sample project using tomcat and eclipse. While the index.html load properly but when i submit the page (or make call to servlet), i received below exception
我是 J2EE 的新手,并使用 tomcat 和 eclipse 构建了一个示例项目。虽然 index.html 加载正确,但是当我提交页面(或调用 servlet)时,我收到以下异常
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
java.util.concurrent.ConcurrentHashMap.putVal(Unknown Source)
java.util.concurrent.ConcurrentHashMap.putIfAbsent(Unknown Source)
java.lang.ClassLoader.getClassLoadingLock(Unknown Source)
java.lang.ClassLoader.loadClass(Unknown Source)
java.lang.ClassLoader.loadClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1641)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
Following are the files i have created
以下是我创建的文件
1) index.html
1) index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="SelectCoffee.do">
select coffee
<select name="taste" size="1">
<option value="dark">dark</option>
<option value="latte">latte</option>
<option value="cold">cold</option>
<option value="chocolate">chocolate</option>
</select>
<input type="submit">
</form>
</body>
2)web.xml
2)web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>For Coffee</servlet-name>
<sevlet-class>com.example.web.CoffeeSelect</sevlet-class>
</servlet>
<servlet-mapping>
<servlet-name>For Coffee</servlet-name>
<url-pattern>/SelectCoffee.do</url-pattern>
</servlet-mapping>
</web-app>
3) CoffeeSelect.java
3) CoffeeSelect.java
package com.example.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CoffeeSelect extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Cofee Advice");
String c= "mine : " + request.getParameter("taste");
out.println("<br>Got taste = "+c);
}}
Also let me know if anything else is required. Please help as this may get me a job :(
如果还需要其他任何东西,请告诉我。请帮忙,因为这可能会让我找到一份工作:(
采纳答案by Hari
In your webb.xml there is a typo in servlet class definition it should be <servlet-class> </servlet-class>
not <servet-class> </servet-class>
probably the servlet is never registered, that is why you are getting a NullpointerExcepion
在您的 webb.xml 中,servlet 类定义中有一个拼写错误,应该<servlet-class> </servlet-class>
不是<servet-class> </servet-class>
servlet 从未注册过,这就是为什么您得到NullpointerExcepion
回答by Feng Lin
Does the class CoffeeSelect
built into the class folder? In the wrong hint, it may be the class is not found.
是否class CoffeeSelect
内置到类文件夹中?在错误提示中,可能是未找到该类。
回答by sailor
check with request.getParameterValues("taste");
and If possible remove the space from the servlet-name
检查request.getParameterValues("taste");
并如果可能的话从servlet-name
回答by greenhorn
This would perfectly work. Don't write the logic in doPost() do it in processRequest()
这将完美地工作。不要在 doPost() 里写逻辑在 processRequest() 里写
index.html
索引.html
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="/SelectCoffee">
select coffee
<select name="taste" size="1">
<option value="dark">dark</option>
<option value="latte">latte</option>
<option value="cold">cold</option>
<option value="chocolate">chocolate</option>
</select>
<input type="submit">
</form>
</body>
</html>
web.xml
网页.xml
<servlet>
<servlet-name>CoffeeSelect</servlet-name>
<servlet-class>CoffeeSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CoffeeSelect</servlet-name>
<url-pattern>/SelectCoffee</url-pattern>
</servlet-mapping>
CoffeeSelect.java
咖啡选择程序
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("Cofee Advice");
String c= "mine : " + request.getParameter("taste");
out.println("<br>Got taste = "+c);
} finally {
out.close();
}
}
回答by Sagar Pudi
HTTP status 500 tells there is an error in server side software that stops fulfilling a request. Learn more about HTTP Status codes herethose will make your job easy while debugging your webapplication.
HTTP 状态 500 表示服务器端软件出现错误,导致停止满足请求。在此处了解有关HTTP 状态代码的更多信息,这些代码将使您在调试 Web 应用程序时的工作变得轻松。
Check your Deployment descriptor (web.xml) there is an error while registering servlet class
so you are getting null pointer exception.
Replace your web.xml with this now it will definetly works
检查您的部署描述符 ( web.xml) 注册 servlet 类时出现错误,因此您收到空指针异常。
用这个替换你的 web.xml 现在它肯定会工作
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>For Coffee</servlet-name>
<servlet-class>com.example.web.CoffeeSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>For Coffee</servlet-name>
<url-pattern>/SelectCoffee.do</url-pattern>
</servlet-mapping>
</web-app>