1

我为在线 DVD 商店创建了一个 EJB 应用程序。我使用 Netbeans 7.2.1 和 Glassfish 3.1.2 当我运行 index.jsp 时,它启动成功的界面,如果我单击表单提交按钮,我收到此错误:

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
root cause

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

在 Netbeans 中,我收到此错误:

INFO: Couldn't create the bean Lookup failed for 'businessSessionBeans.UserSessionBeanRemote' 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}
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:76)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:722)

我创建了一个 Java EE 企业应用程序。我有 project-ejb 和 project-war。因为我无法直接从 ejb 创建 SessionBean,所以我创建了一个新项目/Java 库类,并在那里创建了实体。我向 ejb 和 war 介绍了该库,他们识别了实体,因此我能够创建会话 bean。但是我收到了这个错误,我不知道问题所在,请参阅一些来源:

索引.jsp

    <%@page import="businessSessionBeans.* , java.math.*, javax.naming.*" %>

<%!
    private UserSessionBeanRemote userB=null;
    public void jspInit()
    {
        try{
            InitialContext ic=new InitialContext();
            userB=(UserSessionBeanRemote)ic.lookup(UserSessionBeanRemote.class.getName());
        }catch(Exception e)
                {System.out.println("Couldn't create the bean "+e.getMessage());}

     }
   public void jspDestroy()
   {
       userB=null;
   }
%>

        <%  String username=(String)request.getParameter("username");
            String password=(String)request.getParameter("password");
            if((username!=null)&(password!=null)){
                String userType=userB.userLogin(username,password);
                if (userType!=null){
                    session.setAttribute("username",username);
                    session.setAttribute("password",password);
                    session.setAttribute("userId", userB.getUserId(username, password));
                    if (userType.equals("Admin"))
                       response.sendRedirect("admin.jsp");
                    else if (userType.equals("User"))
                         response.sendRedirect("user.jsp");
                }
                else{
        %> <script type="text/javascript">
                window.alert("User not registered");
        </script>
            <%}}%>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Login</title>
        </head>
        <body>
            <h1>Welcome to our DVD Store!</h1>
        <form method="post" action="index.jsp">
            <pre>
            Username: <input type="text" name="username"/><br/>
            Password: <input type="password" name="password"/><br/>
            <input type="submit" value="Submit"/>  <input type="reset" value="Reset"/><br/>
        <h3>Don't have an account? <a href="register.jsp">Sign up</a></h3>
            </pre>
        </form>
    </body>
</html>

UserSessionBeanRemote.java

public interface UserSessionBeanRemote {

public void create(java.lang.String username, java.lang.String password, java.lang.String name, java.lang.String type);

public void edit(Entities.UserEntityBean userBean);

public void destroy(java.lang.Integer id);

public Entities.UserEntityBean find(java.lang.Object pk);

public java.util.List findAll();

public java.lang.String userLogin(java.lang.String username, java.lang.String password);

public java.lang.Integer getUserId(java.lang.String username, java.lang.String password);

}

用户会话Bean.java

public class UserSessionBean implements UserSessionBeanRemote {

// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
@PersistenceContext
EntityManager em;

@Override
public void create(String username,String password,String name, String type)
{
    UserEntityBean userBean=new UserEntityBean();
    userBean.setName(username);
    userBean.setPassword(password);
    userBean.setName(name);
    userBean.setUserType(type);
    em.persist(userBean);
}

@Override
public void edit(UserEntityBean userBean)
{
    em.merge(userBean);
}

@Override
public void destroy(Integer id)
{
    UserEntityBean userBean=(UserEntityBean)em.find(UserEntityBean.class, id);
    em.remove(userBean);
}

@Override
public UserEntityBean find(Object pk)
{
    return (UserEntityBean)em.find(UserEntityBean.class, pk);
}

@Override
public List findAll()
{
    return em.createQuery("select OBJECT(o) from UserEntityBean o").getResultList();
}

@Override
public String userLogin(String username,String password)
{

    String fusername,fpassword;
    UserEntityBean userBean;
    List userList=findAll();
    Iterator it=userList.iterator();
    while(it.hasNext())
    {
        userBean=(UserEntityBean)it.next();
        fusername=userBean.getName();
        fpassword=userBean.getPassword();
        if(fusername.equals(username)&fpassword.equals(password))
        {
            return userBean.getUserType();
        }   
    }
    return null;
}

@Override
public Integer getUserId(String username,String password)
{

    String fusername,fpassword;
    UserEntityBean userBean;
    List userList=findAll();
    Iterator it=userList.iterator();
    while(it.hasNext())
    {
        userBean=(UserEntityBean)it.next();
        fusername=userBean.getName();
        fpassword=userBean.getPassword();
        if(fusername.equals(username)&fpassword.equals(password))
        {
            return userBean.getId();
        }
    }
    return null;
}

}

最后,UserEntityBean.java

public class UserEntityBean implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String password;
private String userType;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}

/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}

/**
 * @return the password
 */
public String getPassword() {
    return password;
}

/**
 * @param password the password to set
 */
public void setPassword(String password) {
    this.password = password;
}

/**
 * @return the userType
 */
public String getUserType() {
    return userType;
}

/**
 * @param userType the userType to set
 */
public void setUserType(String userType) {
    this.userType = userType;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof UserEntityBean)) {
        return false;
    }
    UserEntityBean other = (UserEntityBean) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "Entities.UserEntityBean[ id=" + id + " ]";
}

}

任何的想法?

4

1 回答 1

0

你应该像这样查找你的 EJB:

userB=(UserSessionBeanRemote)ic.lookup("UserSessionBean");

而且您的代码缺少@Stateless:

@Stateless(name="UserSessionBean") 
public class UserSessionBean implements UserSessionBeanRemote {
于 2012-11-25T20:51:35.990 回答