csci399labinfo
TRANSCRIPT
-
7/29/2019 CSCI399labinfo
1/76
CSCI399LaboratoryManual
LabsetupThe NetBeans IDE will be used for both PHP and Java development in CSCI399. You
should have previous experience with NetBeans from CSCI110, and maybe also CSCI222 or
CSCI213. NetBeans should be in your Applications/Programming menu on Ubuntu. The
version for Session 1 2012 is 7.1.
The Glassfish appserver has been installed along with NetBeans. This will be used to run
servlets in the Java component of the subject.
Most of the assignments require database access you are free to choose MySQL or Oracle
(or even Derby or Postgres; if you install all the software on Windows, you can useSQLServer but not Microsoft Access). You will have accounts on the version of Oracle that
runs on the wraith computer, and accounts for using MySQL. The arrangements for using
MySQL are the same as in CSCI110. Each laboratory computer is running its own instance
of MySQL; the MySQL engine creates databases on the local hard disk. This means that if
you create tables on one computer, they will not be available to you if you move to another
computer. Remember also that there is no backup; if the software on a computer is refreshed,
your data tables will vanish. Student accounts on Oracle are also not backed up. So
whichever database you choose, you should keep the scripts that create your tables and the
scripts that populate tables with initial data.
MySQLs Workbench application should be listed in the Applications/Programming menu
(there are tutorials for Workbench at the MySQL site). You can use Workbench to connect
to the local MySQL engine and create tables. Alternatively, you can work from the
Services/Databases tab in NetBeans where you can run SQL scripts or individual SQL
commands. Administration of your Oracle database tables is probably most easily done
through the Services/Databases tab in NetBeans.
There is a standard Apache running at port 80 on each machine. This has been configured to
allow user directories. It runs in single thread mode because it includes the PHP
interpreter. The PHP interpreter has been configured with modules for MySQL (mysqli) and
Oracle (oci) access, and has the GD graphics libraries installed.
The arrangements for user directories are the same as in CSCI110. You should create a
public_html directory in your home directory and allocate execute permission to
others. Your home directory on Linux usually has permissions drwx------; these
permissions prevent other students from looking at your work, but also prevent the Apache
web server from accessing your public_html directory. As in CSCI110, you should change
the permissions on your home directory to maybe drwx-----x.
-
7/29/2019 CSCI399labinfo
2/76
The PHP environment in NetBeans should include the Zend framework (1.10) and the Smarty
template engine. The installed PHP libraries include the ADOdb ORM package as well as
GD graphics.
When you first use NetBeans (or maybe every time you start NetBeans) you may get
challenged for a Keyring password. This is a feature of the Linux system and its gnome
window manger; it depends on how your account has been set up whether or not you get this
challenge. If you cannot remember your Keyring password, you should remove all files from
the directory .gnome2/keyrings and then try again.
If you have used NetBeans previously, you may have a set of directories such as .netbeans,
.netbeans-derby, .netbeans-registration, mydomain and others. (You will also probably
have a NetBeansProjects directory.) Data in these directories may relate to older versions of
the NetBeans system and can under some circumstances prevent you from starting NetBeans.
If you have problems, delete any old NetBeans directories to give it a clean start.
Reasonably current versions of Firefox, Chrome, Opera etc have been installed on both Linux
and Windows OSs. These should support fairly substantial subsets of HTML5.
The Apache logs are in /var/logs/apache2 but you cannot access these directly. There
should be two locally defined commands apacheerrortail, and apacheaccesstail that let
you see recent entries in these logs. The data in the logs can be useful for debugging or for
providing evidence for your reports, i.e. evidence showing that your version runs.
StartinginthelaboratoryIt will probably be best if in the first week of session you set up your laboratory environment both the PHP parts and the Java parts. You should also reprise work that you have done
previously on creating and using tables in both Oracle and MySQL data base systems.
1DirectorysetupCreate a public_html directory in your home directory. Assign appropriate access
permissions to both your home directory and the public_html subdirectory. If you cannot
remember how to do that, go and look at the CSCI110 exercises. Create a text file
helloworld.html containing the text:
Directory test
Hello!
Helloyour user identifier here
Make sure that this file has read permission for others.
-
7/29/2019 CSCI399labinfo
3/76
Aim a browser at http://localhost/~youruserid/helloworld.html.
You should get a greeting delivered via the local Apache web server.
(Dont open the file by double clicking on it! That has it delivered by the local file system
not the web server.)
2NetBeansStart NetBeans.
If you are challenged for a Keyring password get that sorted out and try again.
NetBeans will typically display a start page (you can turn off this feature). If you configure
your NetBeans (in Tools/Options/General) you can set your name and password so allowing
NetBeans to access the Internet via the Universitys proxy; if Internet access is available, the
start page will include news from netbeans.org.
Close the start page and get to the standard tab pane view.
-
7/29/2019 CSCI399labinfo
4/76
2.1NewPHPprojectCreate a new PHP project (right-click in Projects tab and select new).
The first time you create a project of any particular kind (Java, Java Web, PHP, ) NetBeans
will have to run some activation code to initialize configuration data.
This first project is a basic PHP project (PHP version 5.3 with namespace support):
-
7/29/2019 CSCI399labinfo
5/76
No frameworks needed yet:
Write the code:
-
7/29/2019 CSCI399labinfo
6/76
What do you mean I cant read that?
OK, here is the code again
It should run:
2.2ActivateJavaNext create a Java project (nothing to do with the web, just checking that all parts are
working!). Since this is the first Java action, the Java components must be activated
-
7/29/2019 CSCI399labinfo
7/76
Activation can take a little time
Fill in all those dialogs:
Implement your Java Standard Edition hello-world program:
-
7/29/2019 CSCI399labinfo
8/76
What do you mean I cant read that?
OK, here is the code again
Now run it:
2.3ActivateWebandEnterpriseJavaCreate another project this time the type is Java Web. There is a lot more activation work
this is the point where NetBeans must create a lot of extra configuration data for the
Glassfish appserver.
Note that PHP projects (that will be served via the Apache/PHP system) go in your
public_html directory but Java web applications that run on Glassfish go in subdirectories of
your NetBeansProjects directory.
-
7/29/2019 CSCI399labinfo
9/76
It will create your domain and pick ports for your server (your port numbers may differ):
You have to identify the server during this process.
-
7/29/2019 CSCI399labinfo
10/76
NetBeans may be able to sort this out for itself or may ask for your help.
You shouldnt have to download anything (one of the options that it seems to favour); there is
a server installation (belonging to root) in /usr/local/glassfish.
-
7/29/2019 CSCI399labinfo
11/76
You cannot use that (it belongs to root) but you should be able to select it. If you select
roots glassfish, the install script will copy information from it and create appropriate
configuration data in your own domain files.
(Sometimes you are left with references both to your version of the server and to roots
version. This is annoying as NetBeans keeps popping up reminders that you cannot use
roots version. You should go to the services/servers tab and remove the reference to roots
version.)
-
7/29/2019 CSCI399labinfo
12/76
Continue setting up the new Java web project
Our Java web projects do not use the MVC frameworks:
It is easier for all the subsequent Java web projects because the appserver will have been
configured.
You should eventually get an empty web app with just a JSP page (index.jsp).
-
7/29/2019 CSCI399labinfo
13/76
What do you mean I cant read that?
OK, here is the code again
Just select the WebApplication1 project in the projects tab, and right-click and select Run.
It gets exciting. (?)
NetBeans has to
1. Compile your JSP page (converting it into a servlet that prints output).2. Prepare it for deployment.3.
Start servers (Derby database and glassfish).
-
7/29/2019 CSCI399labinfo
14/76
4. Deploy the application onto the server. Start a browser.5. Aim the browser at the new Java Web application.
(If you have followed instructions from elsewhere like some of the lecture notes and you
have already set an administrator password on your glassfish, you will be prompted to enterthis password at some point during the deployment operation. You really should set an
administrator password see later.)
Derby database starts, and glassfish starts reports appear in the output logs shown in
NetBeans:
It does actually have quite a lot of work to do to get the appserver up and running:
Eventually we get there:
-
7/29/2019 CSCI399labinfo
15/76
2.4CheckoutdatabaseconnectionsDo this once you have got your database passwords and identifiers.
The lecturer will send mail with the identifier and password for MySQL it will be
something like student13/V1hJ4kuio. Yuan, the database administrator will send you mail
with your password for Oracle (on Oracle, you use your unixid as your user-name).
You may not get passwords until the second week of session.
Check the Services/databases tab, it should show provision for connections to a variety of
databases, and will probably also have an entry for administering the local MySQL (which
you cannot use as Im not telling you the administrator password) and for using Derby (which
you dont particularly want to use I suppose you could use it if you have some deeply held
conviction that neither Oracle nor MySQL was right for you).
-
7/29/2019 CSCI399labinfo
16/76
Try creating a connection to Oracle it will ask where the Oracle driver files are, they should
be in /usr/local/instantclient_10_2.
(A little bit dated but it should still work.)
Connect to Oracle it is on wraith at port 1521, and is called csci. Use your own user-id
and Oracle password. You can try Test Connection
-
7/29/2019 CSCI399labinfo
17/76
Finish setting up the connection.
And view what you can get to. For some reason it shows all of the schemas (data bases) that
are defined on the database servers. Of course, you can only use your schema (hopefully).
-
7/29/2019 CSCI399labinfo
18/76
You can view your tables and run SQL statements
It should work in much the same way for MySQL:
-
7/29/2019 CSCI399labinfo
19/76
It sets the wrong schema as the default find your own schema and right click to set it as
your default. (You should change that JDBC URL to jdbc:mysql://localhost:3306/idwhere id
is your MySQL identifier e.g. stud0000, student13.)
2.5CreatesomedatatablesTo keep things simple, these tests just use a variant of the babynames example that was
used in CSCI110. The table that you define will need a surrogate primary key (required for
some of the ORM packages). You should create both MySQL and Oracle versions:
MySQL (enter these statements in a statement execution tab in NetBeans), of course you
should use your schema, not that of stud0000:
-
7/29/2019 CSCI399labinfo
20/76
Populate the table:
View the data to check:
-
7/29/2019 CSCI399labinfo
21/76
Repeat for Oracle (its wonderful how standardized SQL is these days):
And again view the data to check:
-
7/29/2019 CSCI399labinfo
22/76
2.5UsingthedatabasesfromPHPCreate a new PHP web application that simply displays the contents of the babynames tables
in the two databases:
/home/undergrad/s/stud0000/public_html/Babynames1.php/index.php
Loading babynames from databases
Checking connections to databases
Oracle
Connect to oracle, list all in babynames table
-
7/29/2019 CSCI399labinfo
23/76
MySQLConnect to local mysql, list all in babynames table
-
7/29/2019 CSCI399labinfo
24/76
2.6GettingsmartYou can leave this part of the exercise until you are about to start Assignment 3.
Smarty templates provide a useful basis for separating coding issues (business logic) from
display issues (presentation).
Smarty does require a standard directory configuration with subdirectories for templates
etc. Two of these directories need to be writable by the web-process. It is best to place these
directories in a different part of the filespace, separate from you web-files area. But for
simplicity in these exercises and assignments, you can create the directories as sub-directories
of a NetBeans project directory in your public_html area.
Start a new PHP project, add the four directories cache, configs, templates, and templates_cand, at the command line, adjust the permissions:
-
7/29/2019 CSCI399labinfo
25/76
Your PHP code now handles control and model, while a Smarty template handles view.
Code that does not contain lots of output statements to generate HTML is usually easier to
read.
Your project should have the code file, and the associated Smarty template view file in a
templates directory:
The code gets the babynames from the MySQL file, and stores the retrieved records in a PHParray. This array is passed to the Smarty object. The code has to include the Smarty library
code (location may vary but currently it is in /usr/share/php/smarty directory), and instantiate
and initialize a Smarty object.
-
7/29/2019 CSCI399labinfo
26/76
The Smarty template file is largely standard HTML text; it has some Smarty scriptinglanguage statements that create dynamic content. (Smarty is introduced in the Still More
PHP segment of the subject. It is documented at www.smarty.net.)
-
7/29/2019 CSCI399labinfo
27/76
The Smarty version of your babynames application should work:
2.7SettingupZendYou can leave this part of the exercise until you are about to start Assignment 4. The Zend
framework is introduced in the Still More PHP segment of the subject.
First, you should add the Zend library to the global include path used by NetBeans for PHP
(this is under the Tools/Options menu):
-
7/29/2019 CSCI399labinfo
28/76
(The library is currently in /usr/share/php/libzend-framework-php; I think that screen shot
might be in error as it references the Zend subdirectory rather that the framework directory. )
Adding this directory allows NetBeans to check calls to methods of Zend classes.
The Zend libraries must also be added to your Apache configuration (this has been done inthe laboratory, but it is something that you would have to do if trying to configure similar
software on your own computer).
You must also register a command supplier:
(Again if you are creating a version of the software on your own computer, you will have
additional work to do. There is a file NetBeansCommandSupplier.php (or something like
that) among your NetBeans IDE files; this has to be copied to the directory with the Zend
PHP files.)
-
7/29/2019 CSCI399labinfo
29/76
You should now create a new PHP project, this time specifying the use of the Zend
framework. NetBeans will run Zends script and create a project with the elaborate structure
required by Zend, along with a few files. This project directory is created in your
public_html directory.
-
7/29/2019 CSCI399labinfo
30/76
The Zend install script actually gives you a running application one that displays an
advertisement for the framework:
(Note the URL - ~youruserid/Projectname/public. It runs the index.php script there. This
odd URL relates to the simple way of setting up Zend. The proper way to setup Zend
involves many additional steps creating virtual hosts in your Apache, modifying DNS
records, reconfiguring browsers and proxies.)
You edit the files of the script generated application to create your own application.
Initially, just change the default output. This is defined in the file application/views/
scripts/index/index.phtml. It has the same role as a Smarty template. As initially generated it
has a CSS style section and the HTML with the links back to Zend that produce the output
shown above.
Change it to something like:
-
7/29/2019 CSCI399labinfo
31/76
This should result in an advert free starting page.
Another thing that you should do at this point is add extra information to the .htaccess file
that Zend generated (the one with the rewrite rules for Apache). This change relates to the
simple way of configuring Zend. There has to be a line specifying where the Zend projects
public directory is relative to your public_html directory:
Add a RewriteBase entry:
-
7/29/2019 CSCI399labinfo
32/76
2.8ZendhandlesbabynamesYour Zend installation needs a few more tests to check that all is working correctly, so create
another version of the babynames application.
The default output from your updated babynames Zend application should be a HTML page
showing the babynames records from your MySQL database.
This will involve:
1. Adding a database adapter to your project. Details of your database, password etcshould be added to the application/configs/application.ini file. This can be done using
a Zend command script (in NetBeans, this works with dialogs). Most commands are
best done using the scripts and dialogs but it is actually easier to just edit the
application.ini file when adding an adapter.
2. Create a model with a data table class related to the babynames table in thedatabase. (Should definitely be done using the Zend script).
3. Modifying the code of the indexAction method in the indexController to use adata table object to retrieve the data. (Does this sound confusing? Just magical
incantations? Of course. But all will be explained in lectures.)
4. Changing the index.phtml view template to output the data.In NetBeans, you can run Zend commands by right-clicking the project and selecting
Zend/Run Command.
-
7/29/2019 CSCI399labinfo
33/76
The configure db-adapter command can be used; you must specify host, adapter, port,
username, etc. It is easier to copy and paste the information directly into the application.ini
file!
This specifies use of the PDO_MYSQL adapter (you will be using either this adapter or the
ORACLE adapter dont know what you would use for Postgres, Derby, SQLServer). The
host is localhost etc. (Create appropriate resource.db.* entries).
-
7/29/2019 CSCI399labinfo
34/76
Next create a Zend db-table class (and the rest of the model). Here, you should use the
Zend command and its NetBeans dialog:
You pick a name for the class BabynamesTable and identify the table babynames.
The Zend script creates a set of directories for the model and a file with the class definition:
This is the class used to access the table:
-
7/29/2019 CSCI399labinfo
35/76
-
7/29/2019 CSCI399labinfo
36/76
We also have to edit the view object (that index.phtml file again). Zend uses standard PHP
for scripting (it doesnt have a special view scripting language like Smarty):
It should run:
2.9ConfiguringadminandusersforGlassfishYou can leave this part of the exercise until you are about to start Assignment 5.
Connect to the administrator console for your Glassfish server (this is a right-click option on
your server in the Service/servers tab in NetBeans):
-
7/29/2019 CSCI399labinfo
37/76
-
7/29/2019 CSCI399labinfo
38/76
You will need the Manage Users option in this section:
There should be an existing entry in the users for anonymous create a new entry with a
password for admin and maybe another with your userid (just in case you forget a
password):
-
7/29/2019 CSCI399labinfo
39/76
Logout from the admin console, then log back in using an account with a password:
Remove the anonymous entry.
While logged in as an administrator, create some users in the file-realm.
With an application server like glassfish, user authentication and authorization is handled by
the server not by the individual applications.
-
7/29/2019 CSCI399labinfo
40/76
With GlassFish, you have users and groups (for authentication), and roles (for authorization).
The authentication data are configured using the admin console of the server. Roles tend to
be application specific. When creating an application you specify the roles, and provide a
mapping between roles and users/groups.
(The free version of the Glassfish server is ok if you dont have large numbers of users. I
havent seen how this is handled in the enterprise edition.)
Group names should be chosen to be meaningful but that is kind of organization dependent
so here Im just going to use group1 etc.
Select the security/file realm option, then pick manage users and create users, making them
members of various groups:
-
7/29/2019 CSCI399labinfo
41/76
You should create a few users assigned to various combinations of groups. (In a real world
context, the groups would be things like developer, manager, tech-support, privileged
user, user).
Those users and groups will get referenced in a later example where restrictions are placed onthe use of web applications.
2.10CheckingdatabaseaccessfromJavaYou can probably omit this task and get straight on with the web-application in the next
section. But it could help identify configuration problems if there are any.
The task is to use database drivers from a Java Standard Edition application (command line
Java program). Create a new project in your NetBeans projects directory a Java SE project.
Add the jar files for the library (right-click on the Libraries entry in the project and pick add
jar file). As illustrated earlier when setting up NetBeans, the Oracle library is in
/usr/local/instantclient while the MySQL library is in the NetBeans ide subdirectory.
-
7/29/2019 CSCI399labinfo
42/76
The two check functions open appropriate database connections and read back the data from
the babynames table:
-
7/29/2019 CSCI399labinfo
43/76
-
7/29/2019 CSCI399labinfo
44/76
Then, add a servlet to the project, making its code part of the new package.
Servlets require deployment data that are used by the application engine Glassfish, Tomcat,
Oracle Application Server etc. Traditionally, all deployment data were held in a web.xml
file. Since Servlets 3, an alternative scheme has existed where deployment data are defined
in annotations in the source code. The annotations approach is supposedly easier for the
developer (I havent found it that helpful) while the web.xml approach is better for
production environments. When you create a servlet, you have the option of adding its
details to web.xml select this for now (you can try the annotations approach some other
time).
-
7/29/2019 CSCI399labinfo
45/76
Just as it generated a (rather useless) index.jsp page for your Java Web project, NetBeans
generates an outline for your servlet.
This generated outline has definitions for doGet() and doPost() (the methods that you are
most likely to need for the typical servlet). The code supplied for both methods invokes aNetBeans defined processRequest() function that prints some predefined text when either Get
or Post is invoked. This processRequest function exists simply to help you test whether the
servlet system has been set up correctly (its code shows how to open and write to the servlet
output stream a useful model that you can copy into your own functions). Once you have
tested the setup, you should remove processRequest() and define effective methods for doGet
(get a form) and doPost (process a submission).
-
7/29/2019 CSCI399labinfo
46/76
You should remove the editor fold, and define meaningful methods for doGet(), doPost(),
and getServletInfo().
-
7/29/2019 CSCI399labinfo
47/76
But before you do that, you might as well make use of the BabynamesServlets
processRequest() by running a quick test. You need to deploy the web application.
If you right-click on the project and select run, NetBeans will deploy the web app (if not
already deployed) and cause the default browser to open the index.jsp file in the project this
gets the correct URL in the browser (this helps because youve probably forgotten the port
number that your version of Glassfish is using).
The URL (here localhost:33644/Babynames/ - your port number will surely differ) is the
context the address for the overall web application. The BabynamesServlet is located in
this context at a relative URL defined when you created it earlier. View the servlets output
by entering its URL:
-
7/29/2019 CSCI399labinfo
48/76
Edit the servlet. Remove the doPost method entirely (an attempt to post to this servlet will
then fail, generating a standard error response). Copy the content of processRequest into
doGet and remove the processRequest function. Copy code from your database checking
exercise into the revised doGet function:
NetBeans will help sort out the includes just right-click in the editing window and pick
the fix up include files option. There could be several Java classes with the same name,
e.g. Statement NetBeans offers a list, it is your responsibility to pick the right one:
-
7/29/2019 CSCI399labinfo
49/76
-
7/29/2019 CSCI399labinfo
50/76
-
7/29/2019 CSCI399labinfo
51/76
There are several sections in the security tab.
First you must select the Login Configuration. We will normally use Form
Configuration but for this baby example we run with Basic. Basic simply uses the
realm prompt in the browser to get name and password. This is OK for a test, but shouldnt
be used in a real environment where all logins should be customised.
The Realm name has to be supplied it will appear in the login dialog presented by the
browser.
Next add a security role along with a description. Parents-to-be are the ones likely to be
interested in babynames so have a role newparent.
-
7/29/2019 CSCI399labinfo
52/76
-
7/29/2019 CSCI399labinfo
53/76
Its worth looking at the source of the web.xml file to see how these constraints are actuallyexpressed.
Once the generic controls have been defined, you must set up the implementation specific
controls. Select the project and pick New File/Other. The new file has to be a Glassfish
descriptor:
-
7/29/2019 CSCI399labinfo
54/76
Need to add a Security constraint to the glassfish-web.xml file:
This constraint essential says that persons in group1 can take the role of newparent and use
this servlet.
Redeploy the web application and restart your browser. When you attempt to access the
servlet you should see a standard browser challenge for name and password:
If you use the name and password of a person not in group1, you will get a rejection
response. (It seems that it leaves a logged in as cookie on your browser that prevents
you from trying again. You have to restart your browser to try with another user name.)
-
7/29/2019 CSCI399labinfo
55/76
Restart your browser and enter the name and password of a group1 member:
You should once again be able to see the names:
2.13CustomisedloginformBasic authorization, using the browser dialog for access to a realm, is only for
development tests. A real application should have at least form authorization with a
customized login page.
This page obviously should use site-wide styling from CSS stylesheets, and be customized to
the organisation. But it also has to follow a standard structure defined by the servlet
specifications. The name and password fields must have specific names and its action is topost to a built-in component within the application server.
-
7/29/2019 CSCI399labinfo
56/76
The security checked babynames application can be extended with a minimal version of the
required form and login pages.
Add HTML pages loginpage.html and errorpage.html to the project.
-
7/29/2019 CSCI399labinfo
57/76
-
7/29/2019 CSCI399labinfo
58/76
An attempt to access the servlet will now bring up the login page (which obviously needs
prettying:
If an invalid user-name/password combination is entered, the error page gets displayed:
But if the user-name and password correspond to a user who does not have the right to access
the resource the standard HTTP access denied response gets sent:
2.13UsingaDataSourceThe direct use of a JDBC driver to connect to a database was deprecated years ago. (Of
course, most of us still do things that way.) Instead you are supposed to use a DataSource.
-
7/29/2019 CSCI399labinfo
59/76
A DataSource is an initialized object that contains the information needed to connect to a
database which driver, what URL, which user, what password etc. Your program is
supposed to load in a DataSource object and ask it for the connection.
But this means that somehow you have to
1. create an instance of a DataSource object with all the right data;2. Store it somewhere in the filespace;3. Have your program load this object from filespace.
This is a little inconvenient if you are writing a simple stand-alone Java SE application. But
it isnt too difficult if you are using servlets deployed into an application server. In addition,
the current environments are likely to work with a connection pool so you get a reusable
connection. When you dutifully close a reusable connection, it goes back into the pool. This
can improve performance as database connections are not being repeatedly opened and
closed.
An environment like NetBeans can provide a wizard that helps you build a valid
DataSource object for your chosen database. The Glassfish server provides facilities for
storing such objects and retrieving them by name.
So the next improvement to the babynames project should be use of a DataSource.
Add a new Glassfish/JDBC Resource to the Babynames project:
You have to give the resource a name; something like jdbc/uniOracle, or jdbc/localmysql
-
7/29/2019 CSCI399labinfo
60/76
NetBeans can correctly initialize such a resource using data from the database connections
that you previously created.
-
7/29/2019 CSCI399labinfo
61/76
The details of the new DataSource object will be stored in Glassfish when you next deploy
the application.
(When I did this, NetBeans insisted that I use a more modern version of the Oracle driver.
Ill put a copy of it, ojdbc6.jar, in /share/cs/pub/399.)
You must then rewrite the code to use the DataSource rather than a JDBC driver when getting
a connection:
-
7/29/2019 CSCI399labinfo
62/76
The revised program should work and get the data in the Oracle database.
(It doesnt seem really worthwhile making this change just for a simple servlet. We do it
because we will be needing datasources subsequently in more complex situations.)
2.14UsingJavaPersistence(JPA)First, you should check the settings of the database connections that you created for
NetBeans. NetBeans will be using these when it creates entity classes from database records.
Make sure that the URL in your MySQL connection is referencing your database (and not the
default mysql database which holds data such as names of users):
The Oracle connection should reference your schema at csci.
Create a new Java Web Project Babynames2; add a Java package (mystuff).
This version of the project will need to use a library that implements the JPA specification.
There are now several versions available; the versions from the Hibernate project or the
Eclipse project seem to be those preferred for use with Glassfish 3.
You need to add a JPA implementation library to the libraries for the Babynames2 project
(this is Add Library rather than Add JAR/Folder):
-
7/29/2019 CSCI399labinfo
63/76
Then you will want NetBeans to generate an entity class based on the meta-data that the
database has describing its tables:
In the dialog, select the datasource use your MySQL data source.
-
7/29/2019 CSCI399labinfo
64/76
NetBeans will retrieve the meta-data on all your tables and list them as Available Tables (if
an entry is greyed out, it is a table without a primary key). Pick the table and add it to the
Selected Tables (NetBeans will use foreign key relations and identify an related tables
that would also have to be added obviously there are none for the babynames example).
NetBeans generates an EntityClass which is a simple Java class with fields for the different
columns in the database, get/set access/mutator methods for the fields, and a few extra
methods like toString(). The MySQL meta-data is easy to interpret and NetBeans has been
able to pick up the fact that the babynames table has an auto-increment primary key field.
-
7/29/2019 CSCI399labinfo
65/76
You usually change the generated implementation of toString to one that gives a string with
summary data about the record (more than just the primary key):
-
7/29/2019 CSCI399labinfo
66/76
NetBeans has also created some NamedQuery objects that embody simple queries (these
are expressed in the Java Persistence Query Language which is similar to but not identical to
SQL).
At run-time, the code is going to need the data that specify that this record is in your MySQL
database. These data are created in a persistence.xml file associated with the project. You
should create this next.
The default Table Generation Strategy is Create but you have already created the tablesand filled them with data. Change it to None. Pick the Persistence Provider and the data
source.
NetBeans generates the persistence.xml file in your project and given it a name:
-
7/29/2019 CSCI399labinfo
67/76
By default it will include the Entity Classes that are defined in the project. (In more complex
examples, the entity classes may well be part of a separate Java library project in that
case you have to add them manually.) You should note the Persistence Unit Name.
Use NetBeans to create a new BabynamesServlet as before remove doPost() and
processRequest() and edit doGet().
This BabynamesServlet will be using a connection to a database as defined in the
persistence.xml file. Here, code injection is being used. (Code injection is another form
of magic that will be explained in the lectures!). The servlet will need to use an
EntityManager to handle its connection to a database and the storage and retrieval of
entities. (EntityManagers are not thread safe so they are created as local variables of
functions therefore in thread storage). EntityManagers are created using an
EntityManagerFactory which interprets the persistence.xml file. An EntityManagerFactory is
thread safe and should be created as an instance member of the servlet. The
@PersistenceUnit annotation provides a mechanism for automatically initializing the
EntityManagerFactory with data defining a particular persistence unit the one whose nameyou remembered earlier in the persistence.xml file.
-
7/29/2019 CSCI399labinfo
68/76
-
7/29/2019 CSCI399labinfo
69/76
Next, try the Oracle version. You should again create an Entity class from the database; this
time picking the oracle data source:
This creates another class in your project:
Oracle tables are not as easily interpreted as MySQL tables, and an entity class that you
generate from an Oracle table usually requires some manual tidying up.
-
7/29/2019 CSCI399labinfo
70/76
Firstly, Oracles default numeric type has something like 38 decimal digits! The only way
this can be represented in Java is as a BigDecimal (or a BigInteger). You will usually want to
change these numeric types.
The primary key in a table must be an Object type but an Integer will suffice rather than a
BigDecimal. Edit the file making consistent changes throughout.
Other numeric data members, corresponding to columns other than the primary key, can be
changed to simple data types such as int, long, float, double.
The other common problem with Oracle tables relates to sequences used to create surrogate
primary keys. The necessary meta-data, relating the NEWBABYNAMESSEQ sequence to
the NEWBABYNAMES table, are just not there. (All Oracle names should be capitalized.)
You have to manually add the information identifying the key field as being auto-generated
from a named sequence (you must also introduce a local name for that sequence e.g.
MySeq):
Change the persistence.xml file so that it now references the Oracle data source:
-
7/29/2019 CSCI399labinfo
71/76
Adjust the code of the servlet (the Entity class for Oracle is Newbabynames, not Babynames
as was the case for MySQL):
It should run:
(Note that the projects libraries now do not include the Oracle .jar file and the MySQL .jar
file. The DataSources and library code have now been made part of the glassfish server
environment and so dont have to be in individual projects.)
2.15UsingJavaServerPages(JSPs)todisplayresultsThose out.println() statements in servlets are so clumsy they are like echo statements
scattered though a PHP script. JSPs were invented long ago (~1999) to provide a better
approach to dynamic content presentation.
It is possible to do the actual work of an application in a JSP, possible but really not that
convenient. Servlets and helper classes provide a better environment for coding issues.
Although JSPs are intended to be more general, it is easiest if you simply use them in the
same way as you used Smarty templates with PHP. (JSPs are a lot older and so may seem alittle clumsier.)
The overall approach is very similar to PHP+Smarty. Work is done in the servlet. Results
are collected. The servlet passes the collections of results to a JSP (just as a PHP script
passed its results to a Smarty object). The JSP has standard markup along with sections that
extract required data from the result objects and slot them into HTML constructs.
Originally, JSPs had HTML markup and static text with the dynamic content created by little
fragments of embedded Java code (scriptlets). The style was similar to the contemporary
approaches like ASP and PHP. Subsequently, embedded Java was deprecated. Instead,dynamic data were displayed using XML like action tags. These action tags are really a bit
-
7/29/2019 CSCI399labinfo
72/76
like macros small parameterized templates that get expanded to much more code. Tag
libraries exist with different sets of macros that permit different ways of selecting result data
and incorporating them into the HTML page. We will be using only the Java Standard Tag
Library JSTL.
You will need a new Java Web Project Babynames3. This will have a servlet that retrieves
all names from one of the databases (using JPA). The servlet will forward the collection of
names to a JSP. The JSP will create the report page using JSTL action tags to format the
dynamic parts of the response page.
Babynames3 project:
1. Create the project2. Add the EclipseLink persistence library3. Add a persistence.xml file specifying use of a MySQL connection.4. Create a Babynames entity class from the table in your MySQL database.5. Add a Babynames servlet; take the definitions from the last example as the starting
point (a doGet method using an EntityManagerFactory to create an EntityManager
etc)
Add the JSTL library to the project:
Add a JSP:
-
7/29/2019 CSCI399labinfo
73/76
NetBeans generates the JSP with the standard Hello World content:
-
7/29/2019 CSCI399labinfo
74/76
Edit the servlet. Its code can be simplified. It just retrieves the records, adds the record
collection to the request, and forwards everything to the DisplayResults.jsp.
Define the JSP:
-
7/29/2019 CSCI399labinfo
75/76
Note:
1. Its using the JSTL tag library (actually, just the core module of JSTL) so add thetaglib annotation.
2. Its generating a list; the list delimiters and can be put into the text.3. It is to loop through the collection of results outputting each as an item in the HTML
list; so it uses the forEach action from the JSTL core module (c:forEach).
4. This forEach loop needs a working variable name it record.5. The forEach loop is working with the forwarded collection of name records; it is
going to find them attached as the extra attribute babynamelist on the request object
this is specified by the expression ${requestScope.babynamelist}. (The expression
language used is derived from Javascript.)
6. Each record will have a name member and a gender member. These should beoutput using the out action defined in the JSTL core.
It should work:
-
7/29/2019 CSCI399labinfo
76/76
EndWhen you have completed all those steps you should have experienced most of thetechnologies that will be used in this years version of CSCI399.