Download 1.1 Servlets Lab content

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1.1 Servlets Lab content
The aim of this lab is to write a secured website that can perform dynamic updates on any table in a given
database. The concerned database is the one called HOTEL. Hotel database connection parameters are:
DB
DB
IP
DB
DB
DB
Type:
Name:
Address:
Port:
User:
Pass:
Oracle 8.1.7
ORCL
10.202.10.2
1521
binome1
password
Users can update any table in the database. This implies that you can either Insert Delete or Update any row in the
database (BLOB management is not mandatory).
The whole web site is dynamic. That way the managed tables are dynamically listed as hyperlinks. And each table
can be administered through web forms. List & forms are dynamically generated using the information given by
the DatabaseMetadata Object.
Application logical diagram :
List of the tables for a given user under Oracle database is obtained with the getTables method. A sample is
given bellow:
I2S Consulting
Version :1.3.1
1
String [] allTables = { "TABLE"};
Connection conn
= DriverManager.getConnection(dbUrl,
dbUser,
dbPassword);
DatabaseMetaData dbm = conn.getMetaData();
ResultSet rs = dbm.getTables(null, dbUser.toUpperCase() ,"%",allTables);
Private Web ACCESS is handled through ACCESS_CONTROLS table (that table contains Logins & passwords).
Thus a logon from and logoff linked should be provided.
What should be done?




Every pages are password protected ;
Database connection parameters are passed as init parameters to a Servlet ;
No class nor any code should be specific to the Hotel Database ;
HTML Forms are dynamically generated by JSP pages that are called by a Servlet that acts as a web
controller
 Every single web form that is dynamically generated based upon
 Database Foreign keys should be handled as HTML list box ;
 Using LOG4J is not an option.
1.2 TOMCAT Installation
Deflate jakarta-tomcat-4.0.2.zip archive into the directory of your choice. Inmy example the chosen
directory is C:\opt.
Create a BAT file in which you'll set the following environment variables:
SET CATALINA_HOME=C:\opt\jakarta-tomcat-4.0.2.
SET JASPER_HOME=C:\opt\jakarta-tomcat-4.0.2.
SET JAVA_HOME=C:\opt\jdk1.3.1_02.
C:\opt\jakarta-tomcat-4.0\bin\startup.bat
Warning: JAVA_HOME environment variable can change depending on your system configuration.
Launch the previously written BAT File.
Choose your favorite Web Browser (Netscape or Mozilla), and connect yourself to the following URL :
http://localhost:8080/examples.
I2S Consulting
Version :1.3.1
2
A screen looking like the one above should appear:
Picture 1 :JSP & Servlets samples.
1.3 Tomcat Configuration
Your Servlet engine is now installed and runs fine. You now have to create your own working environment. Your
workspace is composed of a single WebApp called like you want.
To create a WebApp do the following operations:





In Tomcat's webapps directory add a directory (for it's odp) ;
In that directory create tow directories: WEB-INF et META-INF ;
In the directory WEB-INF add a web.xml file ;
In the directory WEB-INF add a directory called classes ;
In the classes directory add the HelloWorld.java and compile it
I2S Consulting
Version :1.3.1
3
Restart Tomcat engine and then go to the following URL:
http://led:8080/odp/test
webapp.xml.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Picture 2 : web.xml file
I2S Consulting
Version :1.3.1
4
Servlet compilation
Servlet API is part of the J2EE API thus you'll need to add the additional jar files to our CLASSPATH to be able
to compile our Serlvet.
Tomcat Servlet Engine provides these Jar files. Thus add every single Jar file located into the directory
%CATALINA_HOME%/Common/lib o your compilation CLASSPATH.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>Hello</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
Figure 3 : fichier HelloWorld.java
I2S Consulting
Version :1.3.1
5