Given the application that you have to develop, you need to first define the schema, i.e. the tables that you want to create in order to store the required data. In this assignment you will be using PostgreSQL. You do not need to implement the creation of tables in JDBC. You can use the psql command line client to create tables and test the SQL commands.
Then, for each of the required
tasks in your application you should write a
function (or more than one if you want) that uses at least one SQL statement and
implements the required task.You can test the task using a Java
program and JDBC. We strongly recommend that you have separate functions
for each query/task as you will be using the same to implement a web frontend
with JSP. Don't forget to put a semicolon(;) at the end of each SQL statement.
Make sure to use VARCHAR
instead of CHAR
. Otherwise
equality will be difficult to check in your Java code. We next give the steps to
get started. We have an example that accesses the database via JDBC
using a standalone Java application in Lab 6. We describe a similar database access
example using JSP in Lab 8.
All instructions given below are designed to only work on "csa1.bu.edu", "csa2.bu.edu" and "csa3.bu.edu". Using "csa3.bu.edu" is recommended. Make sure you are logged into one of these machines before you proceed.
Create a directory for this project and cd to
that directory. Hereafter, this directory (the full path of this directory)
shall be referred to as $PA1DIR
Execute: tar xvf /cs/coursedata/cs460/pa1.tar (if you get an error try /usr/bin/tar instead of tar). You can also download the file from here: pa1.tar, copy the file to the directory and run: tar xvf pa1.tar
You should have received an email with your postgres username and password.
Shell configuration: If you use
bash shell (this is the default for your cs account), add the
following lines in the .bashrc
file in
your home directory.
export ANT_HOME=$PA1DIR/apache-ant-1.6.5
#
Make sure that
you
replace $PA1DIR with the actual
directory!
export JAVA_HOME=/usr
export PATH=$ANT_HOME/bin:$PATH
export PGPORT=5432
export PGUSER=<postgres user name:
this should be changed>
export PGPASSWORD=<postgres password
assigned to
you: this should be
changed>
Close and reopen your terminal.
Check your postgres account by running psql
. Enter some SQL
commands to ensure everything is ok. (You just need to run
psql and you should get to the database with the same name as your account
name.)
For this project assignment we have a created a skeleton application which demonstrates:
how to upload images from a JSP page
how to store upload images to the database
how to create image thumbnails
how to retrieve and display stored images
Getting this application to run should be your first priority. Here is how to do it:
Configure Tomcat
Tomcat is installed under the
apache-tomcat-5.5.15
directory.
cd to apache-tomcat-5.5.15/conf
.
Open the file server.xml
using an editor and update the port
numbers. You will find two port numbers assigned to your account in the files on Piazza.
If you don't find your email in the file, send an email to me and an account will be assigned to you. You have to
change only the following two lines that have the strings
yourport1
and yourport2
and replace them with
your port numbers.
line 13: <Server port="yourport1"
shutdown="SHUTDOWN">
and
line 77: <Connector
port="
yourport2
"
maxHttpHeaderSize="8192"
Create the appropriate database tables
The file $PA1DIR/photoshare/docs/photoshare_schema.sql
contains the table definitions for the skeleton applications. Start
psql
and execute these commands. Verify that no error messages
are produced in the process or the following steps will fail. Typing
\d
at the psql prompt should produce something like that:
List of relations Schema | Name | Type | Owner | Description --------+-------------------------+----------+-------+------------- public | Pictures | table | XYZ | public | Pictures_picture_id_seq | sequence | XYZ | public | Users | table | XYZ | public | Users_user_id_seq | sequence | XYZ |
Configure the application
Edit the file
$PA1DIR/photoshare/META-INF/context.xml
and substitute the
username
and password
entries with the ones that
have been assigned to you. Make sure that you make all the changes in this file.
Compile the application
Run the following two commands
cd $PA1DIR
ant -f photoshare.xml all
For this project we have provided an ANT build file. You shouldn't have to modify it. After running these commands a new file
$PA1DIR/photoshare/photoshare.war
should have been created. Check it is there.
Note: this step will have to be repeated whenever you make changes to your application.
Deploy the application Now that the web application archive has been created you should copy it to the appropriate Tomcat directory using
cp photoshare/photoshare.war
apache-tomcat-5.5.15/webapps
Note: this step will have to be repeated whenever you make changes to your application.
Starting and testing tomcat:
cd apache-tomcat-5.5.15/bin
To start the Tomcat Server use:
./catalina.sh start
You can stop the server with:
./catalina.sh stop
Any output on stdout or stderr (ie, stuff
printed using System.out.println
or
System.err.println
) is written to
apache-tomcat-5.5.15/logs/catalina.out
.
Other error messages are written to
apache-tomcat-5.5.15/logs/localhost_log*
You should now check that your tomcat server is running by using the
URL:
http://csa3.bu.edu:yourport2/photoshare (If you are using csa3.bu.edu)
If you got everything right you should be able to upload some JPEG (that's
the only image format we will consider) images and see the created thumbnails
once you authenticate yourself. The system comes with a preconfigured user
test@bu.edu
whose password is test
. Use these
credentials to log in. If something went wrong, verify that you followed all of
the above steps properly and no error messages were produced in the process.
To authenticate users and authorize access to protected pages on the website
which you are going to build you will be using Tomcat's embedded security
mechanisms. As the project is mainly concerned with the database interaction we
have implemented this functionality for you in the skeleton application. Despite
the fact the implementation is provided you will have to understand how it works
so that you can tweak it appropriately to match your system's requirements. The
first thing to check is that your photoshare/META-INF/context.xml
file contains the following XML snippet
<Realm className="org.apache.catalina.realm.JDBCRealm" driverName="org.postgresql.Driver" connectionURL="jdbc:postgresql://cs460.bu.edu:5432/YOUR_POSTGRES_USERNAME" connectionName="YOUR_POSTGRES_USERNAME" connectionPassword="YOUR_POSTGRES_PASSWORD" userTable="Users" userNameCol="email" userCredCol="password" userRoleTable="Users" roleNameCol="role_name" />
What the above configuration directive does is to tell Tomcat that protected
resources should be authenticated and authorized against the Users
table in your database. Here is the minimal contents of the Users
table.
CREATE TABLE Users ( user_id int4 NOT NULL DEFAULT nextval('Users_user_id_seq'), email varchar(255) NOT NULL, password varchar(255) NOT NULL, role_name varchar(255) NOT NULL DEFAULT 'RegisteredUser', CONSTRAINT users_pk PRIMARY KEY (user_id) );
This is just and example and is not the exact same
Users
table in which your system's users will be stored, i.e., you
will have to add extra columns to this table to match the requirements of the
application as they have been documented elsewhere. Next, what you have to do is
to restrict access to some pages in your Web
application. Again, a minimal configuration is defined in the file
photoshare/WEB-INF/web.xml
which looks like the following:
<security-constraint> <web-resource-collection> <web-resource-name>PhotoshareRealm</web-resource-name> <description>Pages accessible by registered users</description> <!-- PAGES ACCESIBLE ONLY BY REGISTERED USERS SHOULD BE ADDED HERE --> <url-pattern>/index.jsp</url-pattern> <url-pattern>/someOtherProtectedPage.jsp</url-pattern> <!-- THIS IS AN EXAMPLE OF A PATTERN TO MATCH MANY PAGES <url-pattern>/protected/*.jsp</url-pattern> --> <!-- ============================================================= --> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>RegisteredUser</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/loginerror.jsp</form-error-page> </form-login-config> </login-config>
The comments above direct you as to where to add other pages that are
protected - it should be pretty straightforward. The final lines simply tell
Tomcat that users whose credentials we do not know yet should be directed to the
page login.jsp
and in case of a login error to
loginerror.jsp
. These pages are also provided for you. Once a user
is authenticated they are redirected to their original destination. Another
useful function an example of which is provided is getting the currently
authenticated user from Tomcat. Inside index.jsp
you will find the
lines:
Hello <b><code><%= request.getUserPrincipal().getName() %></code></b>, click here to <a href="/photoshare/logout.jsp">log out</a>
which indicates how to retrieve the username (in our case the email) of the visitor performing the current request. Knowing of and understanding the above functionality should be all you need in terms of authentication and authorization to complete this project.
Image storage and thumbnail creation is provided for you is the skeleton
application. You should reuse this code. When a new image is uploaded to the
database its thumbnail is also automatically created and stored (have a look at
Picture.createThumbnail()
). Both the original picture and its
thumbnail can be retrieved using the ImageServlet
which is also
provided. To access a picture just point your browser at the following url http://csa3.bu.edu:yourport2/photoshare/img?picture_id=XXX
Replacing XXX
with the actual numeric picture id. Similarly, for a
thumbnail the url is http://csa3.bu.edu:yourport2/photoshare/img?picture_id=XXX&t=1
Now, that you have everything up and running it's time to start adding your own files. JSP pages should be placed under:
+---resources
whereas Java source files under
+---src
Remember, whenever you make changes to your application and you want to re-deploy it, you will need to rerun the two steps highlighted above. They are:
ant -f photoshare.xml all
cp photoshare/photoshare.war apache-tomcat-5.5.15/webapps
and if everything worked as expected and Tomcat picked up the new deployment you should see something like that
XX
-
Oct
-20
15
XX:XX:XX
org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web
application archive photoshare.war
in the logfile catalina.2015-XX-XX.log
Good luck!