Week 1 Tasks
Java Development Kit (JDK) installation and set up
- Make sure you have JDK (Java SE) installed in your system.
Go to java.sun.com or get an
installer from the DISCS technical staff (F225).
- Set some environment variables so that you could
compile and execute Java programs on the command line.
Refer to the following screenshots:
JDKSetup.doc .
Take note of the JDK version and use the appropriate path
when setting the environment variables; e.g.,
you may need to indicate "jdk1.6.0_04" instead of "jdk1.6.0_01".
- In addition, for tomcat-servlet functionality
to be enabled, you will need to arrange it so that
your CLASSPATH environment variable includes
"C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;."
- Through the CMD prompt, you may compile a Java program by issuing
the command: javac [ProgramName].java .
You will need to be in the folder where the program resides.
The following command tool script will be useful
in this case (after the script is installed; right-click on the folder
and issue the command prompt accordingly).
JDBC Overview: Connecting to a database through a Java program
- Create a Microsoft Access Database called phonebook.mdb
with one database table called PHONENUM.
Arrange it so that this table has columns named
NAME and MOBILENUM (both of type text).
Add a few rows to this table
(e.g., name JOHN with mobile number 09185551234 and
name MARY with mobile number 09175550000).
- Execute the following queries on the database:
- SELECT MOBILENUM FROM PHONENUM WHERE NAME="JOHN";
- INSERT INTO PHONENUM VALUES("GEORGE","09209999999");
- Register this database as an ODBC data source
(Control Panel -> Admin Tools -> Data Sources (ODBC)).
Important: Register it a System DSN, not a User DSN.
- Create a Java program that performs a query on the database.
You could use the following sample code
for a Java program called Select.java.
Servlet Overview: Web pages and Java servlets
- Install apache-tomcat
in your machine.
If your installation was correct, the following link:
http://localhost:8080
should display the default tomcat webpage.
- Create a simple html web page.
For example, you may create a file called sample.html with
the following contents.
Place the html file inside the webapps/ROOT
folder and try opening the following URL:
http://localhost:8080/sample.html.
- Create a web page with a form.
For example, you may create a file called myform.html with
the following contents.
Place the html file inside the webapps/ROOT folder and try
opening the following URL:
http://localhost:8080/myform.html.
When you click on the form button,
it will open the sample.html form you made earlier.
- (Optional) Redirecting to google.
In myform.html ,
replace "/sample.html" with "http://www.google.com/search",
and replace "firstname" with "q".
This will enable google searches from your page.
- Create a Java servlet.
Here is a Sample Servlet ;
It should be defined in a file named WelcomeServlet.java.
- Now here's the most complex part:
you will assemble a web application.
First, create another form called myotherform.html with
the following contents.
- Create a folder called mywebapp .
That folder should contain myotherform.html
and a folder called WEB-INF .
- Inside the WEB-INF folder,
create a classes folder.
Inside the classes folder,
create a servlets folder.
Inside the servlets folder,
place the compiled Java program WelcomeServlet.java.
(Actually, what needs to be in that folder is the WelcomeServlet.class file).
- Create a web.xml file with
the following contents.
Place the web.xml file inside the WEB-INF folder.
- Place the entire mywebapp
directory structure inside the tomcat webapps folder.
If you carried out each step properly, the following link:
http://localhost:8080/mywebapp/myotherform.html
should demonstrate a working web application.
Here's a zip file containing the entire
directory structure, if you want a short cut :-).