2014-10-29 75 views
1

我是J2EE新手,使用tomcat和eclipse構建示例項目。雖然index.html的負荷正常,但是當我提交頁面(或者撥打電話與Servlet),我在下面的異常收到從tomcat調用servlet拋出HTTP狀態500 java.lang.NullPointerException

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

也讓我知道,如果需要別的。請幫助,因爲這可以讓我工作:(

回答

2

在你webb.xml存在servlet類定義,它應該錯字是<servlet-class> </servlet-class>不是<servet-class> </servet-class>可能在servlet從未註冊,這就是爲什麼你得到一個NullpointerExcepion

0

是否內置到類文件夾中的class CoffeeSelect?在錯誤的提示,它可能是未找到類。

0

檢查與request.getParameterValues("taste"); 和可能的話移除從servlet-name

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

這沒有區別,你可以在url-pattern的使用您的自定義名稱 – 2014-10-29 05:43:23

0

HTTP狀態500告訴有在停止履行的請求的服務器端軟件錯誤。 瞭解關於HTTP Status codes here的更多信息當調試您的web應用程序時,這些功能可以讓您的工作變得簡單。

檢查您的部署描述符(web.xml)註冊servlet類 時出現錯誤,因此您收到空指針異常。
這個替換你的web.xml現在它將definetly工作

<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> 
相關問題