This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
//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";
}
<%@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>
<!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);
%>
<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">
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
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:
Nikhitha Pai
Greenhorn
Joined: Sep 17, 2012
Posts: 13
posted
0
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">
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
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?
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
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: 13
posted
0
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
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: 13
posted
0
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 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: 13
posted
0
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?
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: 13
posted
0
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.
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: 13
posted
0
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.
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: 13
posted
0
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.
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: 13
posted
0
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: Ejb 3.0 Unable to run the program in Weblogic or Glassfish server