【问题标题】:ejb class is not found in glassfish?在 glassfish 中找不到 ejb 类?
【发布时间】:2013-11-02 15:21:34
【问题描述】:

我有 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>

}

【问题讨论】:

    标签: java jakarta-ee servlets glassfish ejb


    【解决方案1】:

    来自 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 ) 部分,不要忘记重新部署您的应用。

    【讨论】:

    • 仍然有 SEVERE: Class [ Lejbexercises/StatlessFundManagerBean; ] 未找到。加载 [class controllers.TestStatelessEJB] 时出错
    • 您能否重新部署您的应用程序并发布初始化 EJB 的服务器日志行。这可能是部署问题。您是如何尝试访问网页的?
    • 我刚刚复制了你的例子。一切正常(没有 name="sfm")。仔细观察,我注意到您的第一个问题是您根本无法访问 servlet。确保在浏览器中输入正确的地址。在我的情况下是localhost:8080/Lejbexercises/TestStatelessEJB
    • 只是我想问一下?您的项目是否位于我的 doc\netbeansProjects 中??
    • 不,我使用的是 Eclipse
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-16
    相关资源
    最近更新 更多