Wednesday, 10 December 2008

A Dynamic Web Project (part I)

The object of this exercise is to create a Dynamic Web Project in Eclipse and deploy it to your local Apache Tomcat server and then to an Apache Tomcat server running in a VirtualBox Ubuntu guest.

The application will contain a simple index.html file, a simple index.jsp file and two servlets DBConnect and JNDIConnectDB. The first servlet will create and connect to a database directly using sql commands. The second servlet will connect to a database using a configured JNDI entry in Apache Tomcat.

Step 1 - Create the Dynamic Web Project


Open Eclipse and create a new Dynamic Web Project

From the toolbar: new Project -> Web -> Dynamic Web Project

Enter a name for your project:

Tomcat1

select your locally installed Apache Tomcat server

apache-tomcat-6.0.18

as your Target Runtime and click

finish

Create index.html


Navigate to your project in the project explorer panel - panel to the left - or from the menu select:

window -> show view -> navigator

Right-click on your WebContent folder and select:

WebContent (right click)
new -> other -> web -> html
click next

Type index as the filename. The suffix of .html will be added automatically. click the finish button

index
finish

Enter the following into the main panel:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>index.html</h1>
</body>
</html>

save the file.

Create index.jsp


Navigate to your project in the project explorer panel - panel to the left - or from the menu select:

window -> show view -> navigator

Right-click on your WebContent folder and select:

WebContent (right click)
new -> other -> web -> jsp
click next

Type index as the filename. The suffix of .jsp will be added automatically. click the finish button

index
finish

Enter the following into the main panel:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>index.jsp</h1>
</body>
</html>

save the file.

Launch the Server


Right-click on index.html and select and launch the page on the server as follows:

index.html (right click)
run as -> run on server

Select your tomcat server and click finish

apache-tomcat-6.0.18
finish

Your web page will launch in your browser.

To view your jsp file, change index.html in the browser to index.jsp

No comments:

Post a Comment