简体   繁体   English

执行servlet程序时出错

[英]error while executing servlet programs

while practising servlet programs i encountered this problem. 在练习servlet程序时,我遇到了这个问题。 and now i am facing this same problem in whichever project i create . 现在无论我创建哪个项目,我都面临着同样的问题。 dont know what i am doing wrong. 不知道我在做什么错。 i am using netbeans 8.1 and glass fish server . 我正在使用netbeans 8.1和玻璃鱼服务器。 the error is :- 错误是:-

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 code:- 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 code:- 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 code:- 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>

Your serlvet class must extend the javax.servlet.http.HttpServlet class, 您的serlvet类必须扩展javax.servlet.http.HttpServlet类,

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

For your second error, I,m not sure why are you getting this error could be another config for inject objects, but to solve this problem you must create a public constructor with no arguments. 对于第二个错误,我不确定为什么会出现此错误,这可能是注入对象的另一个配置,但是要解决此问题,必须创建不带参数的公共构造函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM