从tomcat调用servlet会抛出HTTP Status 500 java.lang.NullPointerException

时间:2014-10-29 05:26:26

标签: java eclipse java-ee servlets tomcat7

我是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)

以下是我创建的文件

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

<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

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);
}}

如果需要其他任何内容,请告诉我。请帮助,因为这可能会让我找到一份工作:(

5 个答案:

答案 0 :(得分:2)

在你的webb.xml中,servlet类定义中有一个拼写错误它应该是<servlet-class> </servlet-class>而不是<servet-class> </servet-class>可能servlet从未注册过,这就是为什么你得到一个 NullpointerExcepion

答案 1 :(得分:1)

这完全有效。不要在doPost()中写入逻辑在processRequest()

中执行
  

的index.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

<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();
    }
}

答案 2 :(得分:0)

class CoffeeSelect是否内置在类文件夹中?在错误的提示中,可能是找不到类。

答案 3 :(得分:0)

查看request.getParameterValues("taste"); 如果可能,请从servlet-name

中删除空格

答案 4 :(得分:0)

HTTP状态500表示服务器端软件中有一个错误停止履行请求。 了解有关HTTP Status codes here的更多信息,这些将使您在调试Web应用程序时轻松完成工作。

检查部署描述符( web.xml )注册servlet类时出错 所以你得到空指针异常 现在用你的web.xml替换你的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>