Professional Documents
Culture Documents
1. New Project
4. Add a css stylesheet, defining some enhanced presentation formats for use by the
servlets etc.
5. In Source Packages, add a package mystuff
6. Add a servlet class, FormServlet. This first version of the servlet simply displays
a page with a list of string values fetched from the database via JPA. The servlet
has just a doGet method (so remove doPost and the auto-generated
processRequest function)
This servlet is going to have a lot of code generating HTML. This wouldnt be
desirable in a real application (JSPs would be preferable) but will suffice for this
simple demonstration.
It is going to have to get a list of the schools names; so it will have some JPA
code. It is going to need to use Entity classes School, Pupil, Teacher. (It only
uses School, but since School references Pupil and Teacher it will need all three.)
Will need to get definitions of classes generated from existing entries in database.
Before we can do that, we need to set up a Persistence Unit describing the
database.
7. Define a persistence unit and a database connection; dialogs
NetBeans should add three classes to project with definitions of these entities, all
marked up with persistence annotations.
NetBeans will not be able to resolve these datatypes! We must add the TopLink
libraries to the Libraries section of the project. We should also add the Oracle
drivers at the same time.
em = emf.createEntityManager();
Query q = em.createQuery(
"Select s.schoolname from School s");
List<String> names = q.getResultList();
em.close();
results = names.toArray(new String[0]);
return results;
}
9. This limited application can then be deployed and run to check all connections to
databases etc. The code will be refined later.
Start AppServer (You may find it necessary to start the appserver separately rather
than use the menu in NetBeans servers-window; an attempt to start the server
from within NetBeans will sometimes cause the system to hang; on launch, it tries
to connect to Suns web site, if this fails it hangs.)
Build main project, select project in projects window, Undeploy and Deploy
Web-browser to localhost:8080/WebSchoolDemo2, thence to servlet