0% found this document useful (0 votes)
20 views11 pages

Lab 4 Servlets

The document provides instructions for creating a dynamic web project in Eclipse, adding a servlet API dependency, creating a Tomcat server, adding an HTML page with a link, and creating a servlet to read and output parameter values from the link.

Uploaded by

n02019697m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views11 pages

Lab 4 Servlets

The document provides instructions for creating a dynamic web project in Eclipse, adding a servlet API dependency, creating a Tomcat server, adding an HTML page with a link, and creating a servlet to read and output parameter values from the link.

Uploaded by

n02019697m
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab 4 – Servlets

 Launch Eclipse selecting a default workspace


 Create a new Dynamic Web Project
• Use the menu bar to select a new project (Files>New>Project…)
• Search for Dynamic Web Project in the search bar, select the Dynamic Web Project and then “Next >”
• Enter “Lab04_Servlet as the Project Name and then select “Finish >” 1

• Open the EE perspective if prompted (otherwise select Window>Perspective>Open Perspective>Java EE


to open the EE perspective)
Lab 4 – Servlets

Convert the project to a Maven project


• Within the Project Explorer, configure the project as a Maven project (right click on the project>
Configure>Convert to Maven Project)
• Within the Create new POM window, select Finish to convert the project
Lab 4 – Servlets

Add the Servlet API maven dependency to the pom.xml file

• Open the pom.xml file and add the following (immediately before the Build tag):

<dependencies>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>9.0.1</version>
</dependency>
</dependencies>

• Save the pom.xml file


• Update the Maven Project
 Within the Explorer window, right click on the project > Maven > Update Project
Lab 4 – Servlets

 Add a Server (if it doesn’t already exist)


• Within the server tab, click on “Click this link to create a new server…” (if this window isn’t visible, then use
the menu bar to make it visible Window>Show Window>Servers…

• Within the New Server window, select Apache>Tome v9.0 Server and then select “Next >”
Lab 4 – Servlets

Add a Server
• Within the New Server window, use Browse… to locate and set the Tomcat Installation directory 1
• Select “Finish” to create the Tomcat Server
Lab 4 – Servlets

Create an HTML starting page

• Within the Project Explorer, right click on WebContent, select New>HTML


• Within the New HTML file window, enter a File name of “index.html” and select “Finish”
Lab 4 – Servlets

Edit the index.html file and add a hyperlink to pass a first_name and a last_name as
parameters using a GET method (which is the default)

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="Home?first_name=tony&last_name=stark">Click Me</a>
</body>
</html>
Lab 4 – Servlets

Create a Servlet
• Within the Project Explorer right click on the Java Resources, select New>Servlet

• Within the Create Servlet window enter a Java package name of “com.example”, enter a Class name of
“Home”, and then select “Finish”
Lab 4 – Servlets

Edit the Servlet file and modify the doGet method to read the first_name and
last_name, and output their values

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String first_name = request.getParameter("first_name");
String last_name = request.getParameter("last_name");
out.println( "<!DOCTYPE html>\n<html>\n" +
"<head><title>Process Servlet Parameters</title></head>\n" +
"<body>\n" +
"<ul>\n" +
" <li><b>First Name</b>: " + first_name + "\n" +
" <li><b>Last Name</b>: " + last_name + "\n" +
"</ul>\n" +
"</body>" +
"</html>");
}
Lab 4 – Servlets

 Use an external Web Browser (if not already configured)


• On the menu bar select Window>Web Browser > Chrome if it exists or anything other than Internal Web
Browser 1
 Run the index.html on Tomcat
• Within the Project Explorer window, right click on the index.html file and select Run>Run on Server
• Confirm that Tomcat v9.0 Server at localhost is selected and then select “Finish”
Lab 4 – Servlets

Confirm that something similar to the following is displayed in an external Web


Browser after the “Click Me” hyperlink is selected in the Web Browser

You might also like