オンライン 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 を作成できました。しかし、このエラーが発生しましたが、問題がわかりません。いくつかのソースを参照してください。
index.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);
}
UserSessionBean.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 + " ]";
}
}
何か案が?