2014-09-30 39 views
0

在執行servlet程序時我遇到了這個問題。現在我無論在哪個項目中都面臨同樣的問題。不知道我做錯了什麼。我正在使用netbeans 8.1和玻璃魚服務器。錯誤是: -在執行servlet程序時發生錯誤

HTTP Status 500 - Internal Server Error 
type Exception report 
messageInternal Server Error 
descriptionThe server encountered an internal error that prevented it from  
fulfilling this request. 
exception javax.servlet.ServletException: Error instantiating servlet class calc 
root cause 
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class calc 

root cause 

java.lang.NoSuchMethodException: calc.<init>() 

note The full stack traces of the exception and its root causes are available in the 
GlassFish Server Open Source Edition 4.1 logs. 
GlassFish Server Open Source Edition 4.1 

的html代碼: -

<html> 
    <head> 
     <title>TODO supply a title</title> 
     <meta charset="UTF-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    </head> 
    <body> 
     <form method="get" action="http://localhost:8080/calculator/calc"> 
      <label for="no1">Enter the number one</label> 
      <input type="text" id="no1" name="no1"/><br><br> 
      <label for="no2">Enter the number two</label> 
      <input type="text" id="no2" name="no2"/><br><br> 
      <select name="opr"> 
       <option>addition</option> 
       <option>subtraction</option> 
       <option>multiplication</option> 
       <option>Division</option> 
      </select> 
      <input type="submit" value="submit"/> 
      <input type="reset" value="reset"/> 

     </form> 
    </body> 
</html> 

servlet代碼: -

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
class calc extends HttpServlet 
{ 
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException 
    { 
     PrintWriter pw=res.getWriter(); 
     float result=0; 
     float a=Float.parseFloat(req.getParameter("no1")); 
     float b=Float.parseFloat(req.getParameter("no2")); 
     String opr=req.getParameter("opr"); 
     if(opr.equals("addition")) 
     { 
      result=a+b; 
     } 
     if(opr.equals("subtraction")) 
     { 
      result=a-b; 
     } 
     if(opr.equals("multiplication")) 
     { 
      result=a*b; 
     } 
     if(opr.equals("division")) 
     { 
      result=a/b; 
     } 
     pw.println("the answer is "+result); 
    } 
} 

XML代碼: -

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <servlet> 
     <servlet-name>calc</servlet-name> 
     <servlet-class>calc</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>calc</servlet-name> 
     <url-pattern>/calc</url-pattern> 
    </servlet-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 
</web-app> 
+0

什麼是您嘗試擊中的URL? – Renjith 2014-09-30 17:18:15

+0

http:// localhost:8080/calculator/for index.html運行完美 – 2014-09-30 17:31:25

+0

http:// localhost:8080/calculator/calc?no1 = 1&no2 = 1&opr = addition參數根據action =「http :// localhost:8080/calculator/calc「 – 2014-09-30 17:32:29

回答

1

你的serlvet類必須擴展javax.servlet.http.HttpServlet類,

public class calc extends the javax.servlet.http.HttpServlet 
{ 
    public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException 
    { 
    ..... 

對於你的第二個錯誤,我不能確定你爲什麼要得到這個錯誤可能是注入的對象另一個配置,但要解決這個問題,你必須創建一個無參數的公共構造。

public class calc extends the javax.servlet.http.HttpServlet 
    { 
     public calc() {} 
     public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException 
     { 
    ..... 
+0

是的愚蠢的錯誤。我糾正了它,但現在它完全不同的錯誤。我更新了這篇文章 – 2014-09-30 18:36:48

+0

@augustushill您可以發佈錯誤日誌部分錯誤 – 2014-09-30 18:55:13

+0

的日誌部分嗎?我是這個網站的新手,這是一種錯誤發佈約定? – 2014-10-01 04:08:07

相關問題