Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes EJB and other Java EE Technologies and the fly likes Ejb 3.0 Unable to run the program in Weblogic or Glassfish server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Ejb 3.0 Unable to run the program in Weblogic or Glassfish server" Watch "Ejb 3.0 Unable to run the program in Weblogic or Glassfish server" New topic
Author

Ejb 3.0 Unable to run the program in Weblogic or Glassfish server

Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
My pojo files is as follows:
Customer.java
package com.example.entity;

import java.util.List;
import javax.persistence.*;

@Entity
public class Customer implements java.io.Serializable
{
private Integer CustomerId;
private String firstName;
private String lastName;
private String company;
private String address1;
private String address2;
private String city;
private String state;
private String zip;
private String emailAddress;
private String phoneNumber;

public Customer()
{
firstName="";
lastName="";
company="";
address1="";
address2="";
city="";
state="";
zip="";
emailAddress="";
phoneNumber="";
}


@Id
@Column(name="custId",insertable=false,updatable=false)
public Integer getCustomerId() {
return CustomerId;
}

public void setCustomerId(Integer customerId) {
CustomerId = customerId;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

@Column(name="address_1")
public String getAddress1() {
return address1;
}

public void setAddress1(String address1) {
this.address1 = address1;
}
@Column(name="address_2")
public String getAddress2() {
return address2;
}

public void setAddress2(String address2) {
this.address2 = address2;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

public String getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public static List<Customer> findAllCustomers(EntityManager em)
{
Query query=em.createQuery("Select OBJECT(cust) FROM Customer cust");
List<Customer> customers=query.getResultList();
return customers;
}

public static Customer findCustomerById(EntityManager em,int custId)
{
Query query=em.createQuery("Select OBJECT(cust) FROM Customer cust"
+ "WHERE cust.customerId= : custId");
System.out.println("customerId in customer" + custId);
query.setParameter("custId",custId);
Customer customer=(Customer)query.getSingleResult();
return customer;

}

}


ControllerServlet.java
package com.example.web.servlet;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.example.entity.Customer;
import static com.example.web.servlet.FormConstants.*;

/**
* Servlet implementation class ControllerServlet
*/
public class ControllerServlet extends HttpServlet {
@PersistenceContext EntityManager myEntityManager;

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String action= request.getParameter(FIELD_ACTION);
if(ACTION_QUERY_FORM.equals(action))
{
displayQueryForm(request,response);
}
else if(ACTION_VIEW_FORM.equals(action))
{
displayViewForm(request,response);
}
else if(ACTION_QUERY.equals(action))
{
doQueryAll(request,response);
}
else
{
displayQueryForm(request,response);
}
}

private void displayQueryForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

getRequestDispatcher("/index.jsp").forward(request,response);
}


private void displayViewForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int custId=Integer.parseInt(request.getParameter(FIELD_CUSTOMER_ID));
System.out.println("customerid"+custId);
Customer customer=Customer.findCustomerById(myEntityManager, custId);
request.setAttribute(ATTRIBUTE_CUSTOMER ,customer);
getRequestDispatcher("/View.jsp").forward(request,response);

}

private void doQueryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ List<Customer> customers =Customer.findAllCustomers(myEntityManager);
request.setAttribute(ATTRIBUTE_CUSTOMER_LIST,customers);
getRequestDispatcher("/list.jsp").forward(request,response);
}

private RequestDispatcher getRequestDispatcher(String resource) throws IOException
{
return getServletConfig().getServletContext().getRequestDispatcher(resource);

}



/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request,response);
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
processRequest(request,response);
}

public String getServletInfo()
{
return "Sample Customer Database Controller";
}
}


Formconstants.java

package com.example.web.servlet;

public class FormConstants {

//FORM ACTIONS
public static final String ACTION_QUERY_FORM="queryForm";
public static final String ACTION_VIEW_FORM="viewForm";
public static final String ACTION_QUERY ="query";

//FORM FIELDS

public static final String FIELD_ACTION ="action";
public static final String FIELD_COMPANY ="company";
public static final String FIELD_FNAME="fname";
public static final String FIELD_LNAME="lname";
public static final String FIELD_CUSTOMER_ID="customer_id";
public static final String FIELD_EMAIL="email";
public static final String FIELD_PHONE="phone";
public static final String FIELD_ADDRESS1= "address1";
public static final String FIELD_ADDRESS2 ="address2";
public static final String FIELD_CITY="city";
public static final String FIELD_STATE ="state";
public static final String FIELD_ZIP ="zip";

//REQUEST ATTRIBUTES

public static final String ATTRIBUTE_CUSTOMER="customer";
public static final String ATTRIBUTE_CUSTOMER_LIST ="customer_list";

//Controller Servlet
public static final String CONTROLLER= "controller";
}


URLuTILS.JAVA
package com.example.web.servlet;

import static com.example.web.servlet.FormConstants.*;

public class UrlUtils {

private static String makeActionUrl(String action)
{
return CONTROLLER + "?"+ FIELD_ACTION + "=" +action;
}

public static String makeCustomerViewUrl(String customerId)
{
return makeActionUrl(ACTION_VIEW_FORM) + "&" + FIELD_CUSTOMER_ID + "=" +customerId;
}
}

INDEX.JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@page import ="com.example.web.servlet.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customer Database</title>

</head>
<body>
<center><h1>Sample Customer Database</h1></center>
<p>this application highlights java persistence API and its usage from a java servlet to build a sample customer database</p>
<p>You can start the application and go to the list of customers by clicking the "go" button below.</p>
<hr/>
<form action="<jsp:expression>FormConstants.CONTROLLER</jsp:expression>" method="post">
<input> name="<jsp:expression>FormConstants.FIELD_ACTION</jsp:expression>" type="hidden"
value="<jsp:expression>FormConstants.ACTION_QUERY</jsp:expression>"
</input>
<input name="Submit" type="submit" value="Go"/>
</form>
<hr/>
</body>
</html>


LIST.JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import ="java.util.List" %>
<%@page import ="com.example.entity.Customer" %>
<%@page import ="com.example.web.servlet.UrlUtils" %>
<%@page import="com.example.web.servlet.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customer List</title>
</head>
<body>
<center><h1>Simple Customer List</h1></center>
<p>Here is the list of customers. You can view more details on a customer by clicking the customer name.
<hr/>
<table>
<tr bgcolor="#99cccc">
<td><strong>Company Name</strong></td>
<td><strong>Customer Name</strong></td>
</tr>
<%
List<Customer> customers=(List<Customer>)request.getAttribuTE(FormConstants.ATTRIBUTE_CUSTOMER_LIST);
if(customers== null || customers.size()==0)
{
%>
<tr>
<td colspan="2"><i>No customers</i></td>
</tr>
<%
}else{
for(Customer cust: customers)
{
String actionUrl= UrlUtils.makeCustomerViewUrl(cust.getCustomerId().toString());
%>
<tr>
<td><jsp:expression>cust.getCompany()</jsp:expression>
<td><a href="<jsp:expression>actionUrl</jsp:expression>" ><jsp:expression>cust.getFirstName()+" "+
cust.getLastName()</jsp:expression>
</a>
</td>
</tr>
<%
}
}
%>
</table>
<hr/>
</body>
</html>


VIEW.JSP
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="com.example.entity.Customer" %>
<%@page import="com.example.web.servlet.UrlUtils" %>
<%@page import="com.example.web.servlet.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>View Customer</title>
</head>
<body>
<center><h1>View Customer</h1></center>
<p>Here is the detailed information on the customer you selected</p>
<hr/>
<%
Customer customer=(Customer)request.getAttribute(FormConstants.ATTRIBUTE_CUSTOMER);
%>

<form method="post" action="<JSP:EXPRESSION> FormConstants.CONTROLLER </JSP:EXPRESSION>">
<%@include file="customer.jsp" %>
<input type="hidden" name="<JSP:EXPRESSION>FormConstants.FIELD_ACTION</JSP:EXPRESSION>"
value="<JSP:EXPRESSION>FormConstants.ACTION_QUERY</JSP:EXPRESSION>"/>


<input type="submit" name="submit" value="Back"/>
</form>
<hr/>
<p><a href="<JSP:EXPRESSION>FormConstants.CONTROLLER</JSP:EXPRESSION>"> Return to the application top page"
</a>
</p>
<hr/>
</body>
</html>

Customer.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<body>
<table>
<tr>
<td colspan="1" align="right">Company name: </td>
<td colspan="5">
<input type="text" name="<jsp:expression>FormConstants.FIELD_COMPANY</jsp:expression>"
value="<jsp:expression>customer.getCompany()</jsp:expression>" size="60"/>
</td>
</tr>
<tr>
<td align="right">First name: </td>
<td colspan="2">
<input type="text" name="<jsp:expression>FormConstants.FIELD_FNAME</jsp:expression>"
value="<jsp:expression>customer.getFirstName()</jsp:expression>" size="20"/>
</td>
</tr>
<tr>
<td align="right">Last name: </td>
<td colspan="2">
<input type="text" name="<jsp:expression>FormConstants.FIELD_LNAME</jsp:expression>"
value="<jsp:expression>customer.getLastName()</jsp:expression>" size="20"/>
</td>
</tr>
<tr>
<td align="right">Email: </td>
<td colspan="2">
<input type="text" name="<jsp:expression>FormConstants.FIELD_EMAIL</jsp:expression>"
value="<jsp:expression>customer.getEmailAddress()</jsp:expression>" size="20"/>
</td>
</tr>
<tr>
<td align="right">Phone: </td>
<td colspan="2">
<input type="text" name="<jsp:expression>FormConstants.FIELD_PHONE</jsp:expression>"
value="<jsp:expression>customer.getPhoneNumber()</jsp:expression>" size="20"/>
</td>
</tr>
<tr>
<td colspan="1" align="right">Address Line1: </td>
<td colspan="5">
<input type="text" name="<jsp:expression>FormConstants.FIELD_ADDRESS1</jsp:expression>"
value="<jsp:expression>customer.getAddress1()</jsp:expression>" size="60"/>
</td>
</tr>
<tr>
<td colspan="1" align="right">Address Line2: </td>
<td colspan="5">
<input type="text" name="<jsp:expression>FormConstants.FIELD_ADDRESS2</jsp:expression>"
value="<jsp:expression>customer.getAddress2()</jsp:expression>" size="60"/>
</td>
</tr>
<tr>
<td align="right">City: </td>
<td>
<input type="text" name="<jsp:expression>FormConstants.FIELD_CITY</jsp:expression>"
value="<jsp:expression>customer.getCity()</jsp:expression>" size="25"/>
</td>

<td align="right">State: </td>
<td>
<input type="text" name="<jsp:expression>FormConstants.FIELD_STATE</jsp:expression>"
value="<jsp:expression>customer.getState()</jsp:expression>" size="15"/>
</td>
<td align="right">Zip: </td>
<td>
<input type="text" name="<jsp:expression>FormConstants.FIELD_ZIP</jsp:expression>"
value="<jsp:expression>customer.getZip()</jsp:expression>" size="10"/>
</td>
</tr>
</table>
</body>
</html>

Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="persistence" transaction-type="JTA">
<jta-data-source>jdbc/customer</jta-data-source>
<class>com.example.entity.Customer</class>
</persistence-unit>
</persistence>

Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Customer</display-name>

<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.example.web.servlet.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

I gave the command jar -cvf customer.war *.*

When i deploy this in weblogic server, i get the error
No persistence unit named 'myEntityManager' is available in scope customer. Available persistence units: []

The same program when i run on glassfish server, no error is thrown , but no output is shown

What is the error .Can you please solve this program for me? thanks in advance
Nikhitha
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

First of all I think it will be better to use named query in your entity 'Customer'. That is instead of method

you can simply write the following annotation on class:
@NamedQueries({
@NamedQuery(name = "findAllCustomers", query = "SELECT c FROM Customer c ORDER BY c.CustomerId"),
@NamedQuery(name = "findCustomerById", query = "SELECT c FROM Customer c WHERE c.CustomerId=:customerID")
})
@Entity
public class Customer implements java.io.Serializable
{ .................... }

Now it is very simple to call both queries by EntityManager instance:
1. List<Customer> clist=em.createNamedQuery("findAllCustomers").getResultList();
2. Customer customer = (Customer) em.createNamedQuery("findCustomerById", Customer.class).setParameter("customerID", 2).getSingleResult();

Frankly I read that using named(static) queries is more efficient than dynamic queries which you use when call - em.createQuery(...);
Consider my way to declare primary key in entity:

This way key is incremented automatically when you insert new row and is not updatable or insertable by programmer. You must not insert primary key yourself if it is
annotated this way.

Next I know very little about jsp and servlets as I study Java Server Faces recommended by javaee6 tutorial, but I see that your persistence.xml is really poor and apparently it lacks <provider> element. Look at my persistence.xml:

Here 'demo00' is my name of database which is created automatically or when you ping your own connection-pool. I used Derby.
Of course this persistence provider should be set in classpath. I use maven and to include this provider in classpath I have in my pom.xml this:


Next I don't understand your url-patern in web.xml:

It seems to me wrong. As for me it simpler to write this -

It means that each page that ends with .jsp is processed by ControllerServlet. So welcome file will be like this:


True person is moral, false is right!
Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Thank you very much for your quick response. I have modified my persistence.xml as below:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="persistence" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/customer</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>com.example.entity.Customer</class>

<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/nikdb"/>
<property name="javax.persistence.jdbc.password" value="aptech"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
</properties>

</persistence-unit>
</persistence>


Other changes are not applied as it did not seem necessary. I get the following warning in glassfish server

Command succeeded with Warning
"http://localhost:4848/management/domain/applications/application/customer" created successfully. PER01003: Deployment encountered SQL Exceptions: PER01000: Got SQLException executing statement "CREATE TABLE CUSTOMER (custId INTEGER NOT NULL, address_1 VARCHAR(255), address_2 VARCHAR(255), CITY VARCHAR(255), COMPANY VARCHAR(255), EMAILADDRESS VARCHAR(255), FIRSTNAME VARCHAR(255), LASTNAME VARCHAR(255), PHONENUMBER VARCHAR(255), STATE VARCHAR(255), ZIP VARCHAR(255), PRIMARY KEY (custId))": java.sql.SQLException: No database selected PER01003: Deployment encountered SQL Exceptions: PER01000: Got SQLException executing statement "CREATE TABLE CUSTOMER (custId INTEGER NOT NULL, address_1 VARCHAR(255), address_2 VARCHAR(255), CITY VARCHAR(255), COMPANY VARCHAR(255), EMAILADDRESS VARCHAR(255), FIRSTNAME VARCHAR(255), LASTNAME VARCHAR(255), PHONENUMBER VARCHAR(255), STATE VARCHAR(255), ZIP VARCHAR(255), PRIMARY KEY (custId))": java.sql.SQLException: No database selected


If i remove the line
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> from the persistence .xml i am able to deploy the application without any messages . But when i try to open the url

http://localhost :8080/customer

It shows requested resource not found.
Please help me in this.

Also i have seen an article about 'No persistence unit found ' message in weblogic that it is a bug ans there is a workaround for that by deploying exploded archives. How to do that?
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

First of all you should modify the following line in your persistence.xml:
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/nikdb;create=true"/>

This insures that your database is created when you deploy your application.

You should check if you created jdbc-resource described in persistence.xml --> <jta-data-source>jdbc/customer</jta-data-source>
If you didn't follow these steps:
1. create jdbc-connection-pool. For that type in command-line after you started glassfish server

asadmin create-jdbc-connection-pool --datasourceclassname=com.mysql.jdbc.jdbc2.optional.MysqlDataSource --restype=javax.sql.DataSource --property
portNumber=3306:password=aptech:user=root:serverName=localhost:databaseName=nikdb:connectionAttributes=;create\=true nikdbPool

After --property should be one space. There must not be space when you type
portNumber=3306:password=aptech:user=root:serverName=localhost:databaseName=nikdb:connectionAttributes=;create\=true

2. create your datasource using created connection-pool
asadmin create-jdbc-resource --connectionpoolid nikdbPool jdbc/customer
To list all jdbc resources type in command line this asadmin list-jdbc-resources. To delete - asadmin delete-jdbc-resource jdbc/customer

At the end please make sure that you provided correctly path to welcome file in web.xml. About that I wrote in first reply.
Also your start or welcome page should be at the same level as WEB-INF folder in webapp folder,
otherwise your path to welcome page changes to <welcome-file><folder-name>/index.jsp</welcome-file>
Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hello there,

I have followed your steps and created the jdbc-resource and jdbc-connection-pool.Everything is fine and it even creates a table .
The other changes i made are as follows :
POJO

package com.example.entity;

import java.util.List;
import javax.persistence.*;

@Entity
public class Customer implements java.io.Serializable
{
/*@PersistenceContext(unitName="persistence.xml")
private EntityManager em;
*/

private Integer CustomerId;
private String firstName;
private String lastName;
private String company;
private String address1;
private String address2;
private String city;
private String state;
private String zip;
private String emailAddress;
private String phoneNumber;

public Customer()
{
firstName="";
lastName="";
company="";
address1="";
address2="";
city="";
state="";
zip="";
emailAddress="";
phoneNumber="";
}


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="custId",insertable=false)
public Integer getCustomerId() {
return CustomerId;
}

public void setCustomerId(Integer customerId) {
CustomerId = customerId;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getCompany() {
return company;
}

public void setCompany(String company) {
this.company = company;
}

@Column(name="address_1")
public String getAddress1() {
return address1;
}

public void setAddress1(String address1) {
this.address1 = address1;
}
@Column(name="address_2")
public String getAddress2() {
return address2;
}

public void setAddress2(String address2) {
this.address2 = address2;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getZip() {
return zip;
}

public void setZip(String zip) {
this.zip = zip;
}

public String getEmailAddress() {
return emailAddress;
}

public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public static List<Customer> findAllCustomers(EntityManager em)
{
Query query=em.createQuery("Select OBJECT(cust) FROM Customer cust");
List<Customer> customers=query.getResultList();
return customers;
}

public static Customer findCustomerById(EntityManager em,int custId)
{
Query query=em.createQuery("Select OBJECT(cust) FROM Customer cust"
+ "WHERE cust.customerId= : custId");
System.out.println("customerId in customer" + custId);
query.setParameter("custId",custId);
Customer customer=(Customer)query.getSingleResult();
return customer;

}

}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="persistence" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/customer</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<class>com.example.entity.Customer</class>

<properties>

<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/nikdb;create=true"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<!-- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/> -->
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource"/>
<property name="javax.persistence.jdbc.password" value="aptech"/>

</properties>

</persistence-unit>
</persistence>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Customer</display-name>

<servlet>
<servlet-name>ControllerServlet</servlet-name>
<servlet-class>com.example.web.servlet.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ControllerServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


Then i deploy the war file with jar -cvf customer.war *.*
It gets successfully deployed , but when i run the url
http://localhost :8080/customer

It shows requested resource not found.
I have also attached a screen shot of my directory structure.

What else should i do?
I should get the index page but that is not happening. Any other go?

Thank you in advance.


[Thumbnail for PersistenceProbImage.JPG]

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1089
    
  10

Hi Nikhitha,
Welcome to the Ranch!

Please UseCodeTags when posting code. Click on that link to understand how that is done. It makes it hard to read the code otherwise.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

I use NetBeans and for me correct project structure is.. Please tell me at first how you did that screenshot of your project in eclipse and then I shall give you screenshot of
my netbeans project

Thank you!

Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
hello,
I have taken a printscreen (PrtScrn button on the keyboard) of the eclipse directory structure and pasted it in Ms Paint. In the forum i have included it as an attachment. I guess that answers how the screen shot was taken.
Coming back to the program, i am just curious to know if you have run it succesfully in Netbeans. Hope you would'nt mind.
I am really desperate to know what and where things have gone wrong in the deployed file. I copied this code and the directory structure from a well known book, so i am sure it must have been successfully tried out by the authors.

Also i went and checked at Mysql prompt the table is getting created when the deployment takes place

I think there is no error in the jdbc-resource and jdbc-connection-pool.Everything is fine . But i could not figure out why and how the controller servlet is not working as expected.

Thank You.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello Nikhitha Pai!

Thank you for your advise to take screenshot but button 'PrintScreenSysRq' on my old primitive keyboard doesn't work.

First of all in WEB-INF you are able to keep such files( at least what I have seen so far):
- faces-config.xml
- web.xml
- beans.xml to enable CDI
- glassfish-web.xml

I don't think it is a good idea to store something else in WEB-INF!. Your start page and all others pages should be
in the same folder that WEB-INF is! For instance you have:

It means that your index.jsp should be in :
WebContent/index.jsp
WebContent/WEB-INF/........
If you have this:

Your index.jsp should be in :
WebContent/rest/index.jsp
WebContent/WEB-INF/........

But instead of folder WebContent your IDE could generate folder with name 'webapp' or name 'web'.
When you create project in Eclipse you should take correct type of project.
It is good idea to take book's examples and try them at first. Use source code which is usually accompanying the book.
Please tell me does your book use Maven or not? Sorry for English.
Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hello,

Thank you for all that explanation. I guess my application satisfies the requirements. My book is not using maven. Manually deploying at command prompt. IT uses eclipse, Weblogic 10 and MySql. I have no acccompanied cd's with the book.

Could you please tell me if you have got the output for the program?
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

Probably I didn't use book you use so I didn't try those examples. The best way for you is to have source code for that book. At the beginning of the book you must meet
information where to get source code! Usually source code is on author's site. I always download the book and its source code absolutely for free.

Great if your program runs correctly!
Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hello there,
could you please post me 1 or 2 good examples with Entity beans as I need it urgently.Hope I am not bothering you too much.
Thnak you.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Where can I post you examples? On this site it will be too much and you will not understand anything.

Give me your email and I send a few pretty examples I have on my computer.

Later you can delete your email so that nobody else sees them.

I can send you really cool examples on JSF with NetBeans and glassfish server. Meanwhile there are many entities you can look through.

It is very good idea for you to cover many examples first of all!!!
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

If you don't want to post email you can simply visit this site - http://www.java2s.com/Code/Java/JPA/CatalogJPA.htm
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

I have even better idea. As you use eclipse look on eclipse site for examples on JPA. They are simple and very important they contain a lot of tips to work
effeciently on Eclipse.
Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hello there,
i would be glad to get more examples on the same topic. Please email to me to nikhithapai@gmail.com

Also the last 2 days i had been working on the example. Well i am glad to say that 2 errors have been rectified. Now i am able to view my index page.

Error no 1
My welcome file index.jsp was wrongly placed inside the WEB-INF folder. You had indeed pointed that out. Then i put all my jsp files in the same level as WEB-INF folder. So i am not getting the Resource not found error for that anymore.



Error no:2
When i deployed in weblogic i had wrongly put the META-INF FILE outside the WEB-INF folder when i tried in another computer. So this was giving the error No persistence unit named xxx available in the scope customer.war.
When i put the META-INF FOLDER AND as a result the persistence.xml file inside the WEB-INF that got rectified.
So iam happy i could reach the next stage.

Now the problem is once i hit the GO button from the index page it should invoke the Controller Servlet.Which at present is not happening. It is showing 404 error. Got to rectify that.

I am pasting below in the attachment the correct directory structure ,as how i follow it at present.

Thank you for all the help you provided.




[Thumbnail for EJBCustomerCorrect.png]

Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello again!

Have you received my emails? Feel free to ask if you don't understand something there!
I found this in your code:

attribute 'unitName' should be exactly the same as your unit name set in persistence.xml here:


Probably servlet is not called because of wrong path in action attribute if your button has such. Anyway try to set path servlet can be accessed:

Look at form's 'action' that invokes above servlet when is submitted:
Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hi,
Thank you for the mails . I have received 2 mails with programs from you. I think , i will take some time to go through all those codes.
Meanwhile let me try out solving my sample program.

Regards,Nikhitha.
Volodymyr Levytskyi
Ranch Hand

Joined: Mar 29, 2012
Posts: 505
    
    1

Hello!

I have sent to you three emails with three examples. Write names of those archives which you received!
Nikhitha Pai
Greenhorn

Joined: Sep 17, 2012
Posts: 19
Hello,
I have got all the three archives you have sent.
Also i am happy that i could finally get the application deployed successfully and running.
The last mistake was i had placed the Web.xml inside classes folder of Web-INF.When I put it outside the classes directory, but inside the WEB-INF, things started working.
So the application is running smooth in glasssfish +eclipse+Mysql combination.
But not in Weblogic server.

Thank you for the archives.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Ejb 3.0 Unable to run the program in Weblogic or Glassfish server