0

我有 JAVA EE 应用程序,我试图从 servelate 调用 EJB,但每次我遇到这个错误

HTTP Status 404 - Not Found

type Status report

messageNot Found

descriptionThe requested resource is not available.

我检查 glassfish 日志我看到了这个

  SEVERE:   Class [ Lejbexercises/StatlessFundManagerBean; ] not found. Error while loading [ class controllers.TestStatelessEJB ]

    ejb class
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */

    package ejbexercises;

    import javax.ejb.Stateless;
    import javax.ejb.LocalBean;

    /**
     *
     * @author
     */
    @Stateless
    @LocalBean
    public class StatlessFundManagerBean {

       public double addFunds(double balance, double amount) {
            balance += amount;
            return balance;
        }

        public double withdrawFunds(double balance, double amount) {
            if (balance < 0) {
                return 0.0;
            } else {
                balance -= amount;
                return balance;
            }
        }
    }


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controllers;

import ejbexercises.StatlessFundManagerBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author
 */

@WebServlet(name = "TestStatelessEJB", urlPatterns = {"/TestStatelessEJB"})
public class TestStatelessEJB extends HttpServlet {

   @EJB(name = "sfm")
 private StatlessFundManagerBean sfm;

    /**
     * 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
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {
                double balance = 1200;
                balance = (double) sfm.addFunds(balance, Double.parseDouble("1200"));
                out.println("1st balance=" + balance );
        } finally {            
            out.close();
        }
    }

    // <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 {
        processRequest(request, response);
    }

    /**
     * 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 {
        processRequest(request, response);
    }

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

}
4

1 回答 1

0

来自 EJB 文档(http://docs.oracle.com/cd/E13222_01/wls/docs100/ejb30/annotations.html#wp1416481):

name - 指定在环境中查找引用的 EJB 时使用的名称。

您正在使用 (name = "sfm") ,这意味着容器将查找已知为“sfm”的 bean,除非您在 beans.xml 中定义了 bean 名称(但您没有提到它,否则在您的情况下这是错误的,所以我假设,你没有)。

只需删除 (name="sfm ) 部分,不要忘记重新部署您的应用程序。

于 2013-10-24T05:04:00.193 回答