HTTP 500错误 - Web服务的Servlet

时间:2015-10-26 04:08:22

标签: java web-services servlets

所以我正在创建一个servlet来处理Calculator Web Service的功能。服务代码如下:

package org.me.calculator;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.ejb.Stateless;
import javax.xml.soap.SOAPException;

/**
 *
 * @author rory
 */
@WebService(serviceName = "CalculatorWS")
@Stateless()
public class CalculatorWS {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "add")
    public double add(@WebParam(name = "a") double a, @WebParam(name = "b") double b) {
        double c = a + b;
        return c;
    }

    @WebMethod(operationName = "subtract")
    public double subtract(@WebParam(name = "a") double a, @WebParam(name = "b") double b){
        double c = a - b;
        return c;
    }

    @WebMethod(operationName = "multiply")
    public double multiple(@WebParam(name = "a") double a, @WebParam(name = "b") double b){
        double c = a * b;
        return c;
    }

    @WebMethod(operationName = "divide")
    public double divide(@WebParam(name = "a") double a, @WebParam(name = "b") double b) throws SOAPException{

        double c = Double.POSITIVE_INFINITY;

        if (b == 0){
            throw new SOAPException ("Cannot divide by 0");
        }

        c = a / b;

        return c;

    }
}

然后我有一个Servlet试图使用通过Glassfish和Netbeans IDE部署的服务。这有两个元素,一个HTML页面和一个java文件来处理来自html页面的请求。两者如下: HTML页面:

<html>
    <head>
        <title>Calculator</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div onload=""></div>
        <h1>Basic Calculator</h1>
        <p>Enter your operands. Be aware that Operand 1 will be the first part of the equation at any time. 
            Meaning for subtraction and division that it is the number being subtracted from or divided.</p>

        <form name="Test" method="post" action="ClientServlet">
            <p>Operand 1: <input name="Operand1" /></p>
            <p>Operand 2: <input name="Operand2" /></p>
            <input type="radio" name="opType" value="add" /> +<br />
            <input type="radio" name="opType" value="subtract" /> -<br />
            <input type="radio" name="opType" value="multiply" /> x<br />
            <input type="radio" name="opType" value="divide" /> &divide;<br />
            <input type="submit" value="Calculate">
        </form>
    </body>
</html>

Servlet的Java文件:

package org.me.calculator.client;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
import org.me.calculator.CalculatorWS_Service;
import org.me.calculator.SOAPException_Exception;

/**
 *
 * @author rory
 */
@WebServlet(name = "ClientServlet", urlPatterns = {"/ClientServlet"})
public class ClientServlet extends HttpServlet {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/CalculatorWS/CalculatorWS.wsdl")
    private CalculatorWS_Service service;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     * @throws org.me.calculator.SOAPException_Exception
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SOAPException_Exception {

        // Handle Data from Form
        String operand1 = request.getParameter("Operand1");
        String operand2 = request.getParameter("Operand2");
        String opType = request.getParameter("opType");

        // Transform Strings to Doubles
        java.lang.Double op1 = Double.parseDouble(operand1);
        java.lang.Double op2 = Double.parseDouble(operand2);

        // Declare Variables
        double result = 0;
        String opSym = "";

        // Determine the Calculation
        switch (opType) {
            case "add":
                result = add(op1, op2);
                opSym = "+";
                break;
            case "subtract":
                result = subtract(op1, op2);
                opSym = "-";
                break;
            case "multiply":
                result = multiply(op1, op2);
                opSym = "x";
                break;
            case "divide":
                result = divide(op1, op2);
                opSym = "&divide;";
                break;
        }

        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Calculator Servlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Calculator  " + request.getContextPath() + "</h1>");
            out.printf("<h2>Result of %s %s %s = %s", op1, opSym, op2, result);
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SOAPException_Exception ex) {
            Logger.getLogger(ClientServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SOAPException_Exception ex) {
            Logger.getLogger(ClientServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private double add(double a, double b) {
        // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
        // If the calling of port operations may lead to race condition some synchronization is required.
        org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();
        return port.add(a, b);
    }

    private double divide(double a, double b) throws SOAPException_Exception {
        // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
        // If the calling of port operations may lead to race condition some synchronization is required.
        org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();
        return port.divide(a, b);
    }

    private double multiply(double a, double b) {
        // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
        // If the calling of port operations may lead to race condition some synchronization is required.
        org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();
        return port.multiply(a, b);
    }

    private double subtract(double a, double b) {
        // Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
        // If the calling of port operations may lead to race condition some synchronization is required.
        org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();
        return port.subtract(a, b);
    }

}

我的问题是我在尝试处理请求时遇到服务器500错误,如下所示:

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 org.me.calculator.client.ClientServlet
root cause

com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class org.me.calculator.client.ClientServlet
root cause

com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Env-Prop: org.me.calculator.client.ClientServlet/service@Field-Injectable Resource. Class name = org.me.calculator.client.ClientServlet Field name=service@javax.jws.WebServiceRef@@@ into class org.me.calculator.client.ClientServlet: Lookup failed for 'java:comp/env/org.me.calculator.client.ClientServlet/service' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
root cause

javax.naming.NamingException: Lookup failed for 'java:comp/env/org.me.calculator.client.ClientServlet/service' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException [Root exception is java.lang.reflect.InvocationTargetException]]
root cause

javax.naming.NamingException [Root exception is java.lang.reflect.InvocationTargetException]
root cause

java.lang.reflect.InvocationTargetException
root cause

javax.xml.ws.WebServiceException: Failed to access the WSDL at: file:/Users/rory/Documents/Uni%202015/WS/CalaculatorApp/CalculatorWSServlet/build/web/WEB-INF/wsdl/localhost_8080/CalculatorWS/CalculatorWS.wsdl. It failed with: 
    /Users/rory/Documents/Uni 2015/WS/CalaculatorApp/CalculatorWSServlet/build/web/WEB-INF/wsdl/localhost_8080/CalculatorWS/CalculatorWS.wsdl (No such file or directory).
root cause

java.io.FileNotFoundException: /Users/rory/Documents/Uni 2015/WS/CalaculatorApp/CalculatorWSServlet/build/web/WEB-INF/wsdl/localhost_8080/CalculatorWS/CalculatorWS.wsdl (No such file or directory)
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

有人能告诉我什么出错了吗?我很茫然。

0 个答案:

没有答案