Hello everybody! I use glassfish local server and intellij IDEa for developing. i use jpa with eclipselink.
I'm already three days can't solve problems with my app. from the beginning i can't just add entity to the database. But now i can't even run servlet...
I have jsp page with button, when i press it servlet have to start.
here is my index.jsp:
and my servlet:<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Start page</title> </head> <body> <form action="<%=request.getContextPath()%>/insert" method="get"> <input type="submit" value="Start"/> </form> </body> </html>
package controller; import beans.SubscriberServiceBean; import javax.annotation.Resource; 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; import javax.transaction.*; import java.io.IOException; @WebServlet(name = "CS", urlPatterns = {"/insert"}) public class Controller extends HttpServlet { @EJB private SubscriberServiceBean subscriberBean; @Resource private UserTransaction ut; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { resp.getOutputStream().println("before transaction"); ut.begin(); subscriberBean.createSubscriber("some@mail.com", "some name"); resp.getOutputStream().println("inside transaction"); ut.commit(); resp.getOutputStream().println("after transaction"); } catch (NotSupportedException | SystemException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) { try { ut.rollback(); } catch (SystemException e1) { e1.printStackTrace(); } resp.getOutputStream().println(e.getMessage()); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } }
my service interface and ejb bean:
package beans.interfaces; import entities.Subscriber; import javax.ejb.Local; @Local public interface SubscriberService { public Subscriber createSubscriber(String email, String name); }
here is my entity:package beans; import beans.interfaces.SubscriberService; import entities.Subscriber; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateless public class SubscriberServiceBean implements SubscriberService{ @PersistenceContext(unitName="MS") protected EntityManager em; public EntityManager getEntityManager(){ return em; } @Override public Subscriber createSubscriber(String email, String name) { Subscriber subscriber = new Subscriber(); subscriber.setEmail(email); subscriber.setSubscriberName(name); getEntityManager().persist(subscriber); return subscriber; } }
my persistence.xmlpackage entities; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; @Entity @Table(name = "subscriber", schema = "mail_service") public class Subscriber implements Serializable{ private String email; private String subscriberName; @Id @Column(name = "EMAIL") public String getEmail() { return email; } @Column(name = "SUBSCRIBER_NAME") public String getSubscriberName() { return subscriberName; } public void setSubscriberName(String subscriberName) { this.subscriberName = subscriberName; } public void setEmail(String email) { this.email = email; } }
<?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> <persistence-unit name="MS" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>jdbc/_mail</jta-data-source> <class>entities.News</class> <class>entities.Subscriber</class> <properties> <!--<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>--> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> <property name="javax.persistence.jdbc.user" value="root"/> <property name="javax.persistence.jdbc.password" value="life"/> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mail_service"/> <property name="eclipselink.logging.level" value="FINE"/> </properties> </persistence-unit> </persistence>
and my pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>MailService</groupId> <artifactId>MailService</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <repositories> <repository> <id>EclipseLink</id> <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url> </repository> </repositories> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.23</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0-SNAPSHOT</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> </dependency> <!--<dependency>--> <!--<groupId>org.eclipse.persistence</groupId>--> <!--<artifactId>eclipselink</artifactId>--> <!--<version>2.5.0</version>--> <!--<scope>compile</scope>--> <!--</dependency>--> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>javax.persistence</artifactId> <version>2.1.0</version> </dependency> <!--<dependency>--> <!--<groupId>org.eclipse.persistence</groupId>--> <!--<artifactId>org.eclipse.persistence.core</artifactId>--> <!--<version>2.5.0</version>--> <!--</dependency>--> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0.2</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>org.eclipse.persistence.jpa</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.eclipse.persistence</groupId> <artifactId>eclipselink</artifactId> <version>2.5.0</version> <scope>compile</scope> </dependency> <!--<dependency>--> <!--<groupId>org.hibernate</groupId>--> <!--<artifactId>hibernate-core</artifactId>--> <!--<version>4.3.0.Beta3</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.hibernate</groupId>--> <!--<artifactId>hibernate-entitymanager</artifactId>--> <!--<version>4.3.0.Beta3</version>--> <!--</dependency>--> </dependencies> <build> <finalName>mail</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.3</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
now when i deploy application to the glassfish server (through the glassfish console) and enter to the brouser: http://localhost:8080/mail/insert
i receive errors:
what's wrong? any suggestions? i'm so tired with solving this problem. so be thankful for any help!type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: PWC1392: Error instantiating servlet class controller.Controller
root cause
com.sun.enterprise.container.common.spi.util.Injec tionException: Error creating managed object for class: class controller.Controller
root cause
com.sun.enterprise.container.common.spi.util.Injec tionException: Exception attempting to inject Remote ejb-ref name=controller.Controller/subscriberBean,Remote 3.x interface =beans.SubscriberServiceBean,ejb-link=null,lookup=,mappedName=,jndi-name=beans.SubscriberServiceBean,refType=Session into class controller.Controller: Lookup failed for 'java:comp/env/controller.Controller/subscriberBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming}
root cause
javax.naming.NamingException: Lookup failed for 'java:comp/env/controller.Controller/subscriberBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=controller.Controller/subscriberBean,Remote 3.x interface =beans.SubscriberServiceBean,ejb-link=null,lookup=,mappedName=,jndi-name=beans.SubscriberServiceBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' [Root exception is javax.naming.NamingException: Lookup failed for 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found]]]
root cause
javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=controller.Controller/subscriberBean,Remote 3.x interface =beans.SubscriberServiceBean,ejb-link=null,lookup=,mappedName=,jndi-name=beans.SubscriberServiceBean,refType=Session' . Actual (possibly internal) Remote JNDI name used for lookup is 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' [Root exception is javax.naming.NamingException: Lookup failed for 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found]]
root cause
javax.naming.NamingException: Lookup failed for 'beans.SubscriberServiceBean#beans.SubscriberServi ceBean' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterpr ise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.pr esentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.na ming} [Root exception is javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found]
root cause
javax.naming.NameNotFoundException: beans.SubscriberServiceBean#beans.SubscriberServic eBean not found
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.