Servlet Development
Servlet Development
net/publication/352785295
CITATIONS READS
0 553
2 authors:
All content following this page was uploaded by Somasundaram Karuppanagounder on 28 June 2021.
ka.somasundaram@gmail.com, kalaiselvi.gri@gmail.com
Introduction
Servlet is an important component in developing Dynamic Web Applications. From the learners
point of view, it is essential to know how to install appropriate software tools to develop a servlet.
As servlet is a web component, it is to be placed in a web container to execute the servlets. Unlike
Java JDK where one can install and develop a Java application, servlet requires several supporting
tools to develop and run it on a application server. A typical environment to run a servlet is
shown in Fig.1. Several Integrated Development Environment (IDE) tools that combines, editors,
compilers, plugins, deployment tools, integrating with servers etc. have been developed.
NetBeans from Apache Foundation, Eclipse from Oracle are few of the popular IDEs .Further for
deployment of web components like Servlet, JSP XML documents, a web server is needed.
Tomcat from Apache Software foundation, Glassfish fro Oracle, Visual Studio from Microsoft etc.
are few of the popular web servers. These IDEs integrate the web servers into their working
environment and therefore becomes an application server. The current version of these tools
are too sophisticated , that it is too complex to configure them in the IDEs. From the student
point of view, it is necessary to have tools that can be easily installed and configured to develop
a servlet and other web components like JSP,XML, EJB etc.
1
Fig.1. A typical view of a web development and deployment environment.
In this technical report, we provide a step-by-step procedure how to install Eclipse-2019-12-R IDE
and Tomcat 9.0. Server and develop a servlet and other web components. Though Tomcat 10.0
is available, it is using jakarta.servlet package instead of the traditional javax.servlet package.
Though both have identical syntax and use, the currently (2021) available IDEs have not fully
implemented the jakarta.servlet. Many manipulations might be required to run them. Tomcat9.0
supports javax.servlet package and hence the examples in the textbooks can be implemented as
such without any trouble. Therefore, we have chosen Tomcat 9.0. The procedure is the same for
installing Eclipse-2021-03 with the latest Tomcat10.0.
It is also assumed that you have already installed at least JDK1.8 in your system, before
proceeding to the next steps.
2
1. Download the Apache Tomcat 9.0 .
In the list of software suitable for different platforms, select the installer version. We need the
Tomcat for windows64 bit platform. .
Copy the downloaded file to a suitable folder of your choice. We have downloaded file
apache-tomcat-9.0.4.exe file from the download site.
3
This is an installer file. Double click the file , it will automatically install the Tomcat9.0 server in
your system. During installation, it will ask for user name and password. You may give the user
name as admin and password as rootroot to remember them easily. Otherwise you can give any
user name and password. Note them in your records, as you may need them latter for system
administration. After installation you can see the Tomcat 9.0 server running. You can test it by
opening the local host by entering the following URL in your web browser:
http://localhoast:8080/
You will get the following screen in your web browser indicating that you have successfully
installed Apache Tomcat 9.0 .
4
You can find the Tomcat 9.0 installation directory in C: drive as follows:
5
3. Stop the Tomcat server.
When the Tomcat was installed, it started running. Stop the running Tomcat server before you
plug it into Eclipse IDE. You can stop the Tomcat server by following the steps given below.
6
iii. From System and Security, select : Administrative Tools
7
v. From services select Apache Tomcat 9.0
After selection, you will find the status of the Tomcat 9.0 server. It is running. Therefore,
you can Stop it or Restart it. Now select Stop. The Tomcat 9.0 server will be stopped.
Once you have stopped, you can see the status of the Tomcat as:
8
4. Download Eclipse 2019-12-R
https://www.eclipse.org/downloads/packages/release/2019-12/r
Download Eclipse IDE for Enterprise Java Developers for Windows x86-64
9
4.1 Unzip the file
Inside the unzipped folder, you will find another folder eclipse. Inside the eclipse folder, the
following files and folders can be found.
The file eclipse.exe with a logo is the file one has to double click to launch the Eclipse. Unlike
other IDEs, this IDE is not installed, but is to be launched each time you need it to run. You can
also create a desktop short cut by right clicking on the icon and Send to option.
10
4.2 Launching the Eclipse
Double click Eclipse icon (eclipse.exe) in the folder. It will start launching. First, you will get the
following Eclipse start up logo.
The eclipse launcher screen then will appear. It will ask you to select a workspace directory. You
can choose any drive, like D, E,F . Then click the Launch button.
11
After launching, you will get the starting screen with the green progress bar as follows:
12
Now the IDE is ready for use. Before you create any Project, you need to pug-in the Tomcat 9.0
server in the Eclipse IDE. After closing the Welcome window, you will get a Project Explorer
window
13
At the bottom of the Eclipse main window, there is a button Servers. Click the Servers button.
14
Wait for the system to populate the possible types of servers that are available in the package..
Look at the bottom corner of the main window, where you can see the progress in green color.
Out of several options, you will get Apache as one of the options.
You will find options from Tomcat Server3.2 to Tomcat Server 9.0. Select Tomcat Server 9.0.
15
After selection of Tomcat 9.0, you will find that the two fields in the window are automatically
filled:
16
Now it is asking for the Tomcat 9.0 installation directory. Using Browse option, search in the C:
drive and locate the installation directory (see Step-1) of Tomcat 9.0.
After locating the Tomcat 9.0 folder, select Select Folder button. If your selection is correct, then
you will get a new window as follows:
17
Now click Finish button. You will get a new window as follows:
At the bottom, you will find under Servers option the following message:
One need to create a Project folder and then create the other application programs under that
Project. We want to create a servlet and run.
18
From File menu, select new and Dynamic Web Project. You will get a new window as follows.
After the selection of the Project, you will get new window as follows.
For Project Name, you can type any name you like. We will give it as Demoweb.
19
After filling the Project name as Demoweb, select Finish button. You will get a new window, with
Project explorer and other windows.
20
In the Project Explorer window, you will get a folder Demoweb.
21
In this Create Servlet window, you need to fill two items.
Java Package: Give a name for the package in which the current servlet will be packaged. We give
it as servpack.
Class Name: Here you need to give a class name for the servlet. We give it as Welcome
After filling the two items, click Finish button and a skeleton of the servlet will be generated
automatically by the IDE as given below. All the imports, the annotation
((@WebServlet("/Welcome")), the doGet() and doPost() methods are inserted by default.
22
Since we are going to make simple servlet, which will give a welcome note, we only need one of
the servlet methods, either doGet() or doPost() method. We retain doGet() method and delete
the doPost() method. Then we insert the following java code to generate a HTML file that will be
executed by the client browser inside doGet() method and delete the other items.
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.printf("<html>");
out.printf("<head><title>First Servlet</title></head>");
out.printf("<body>");
out.printf("<h1>Servlet Programming - An Example</h1>");
out.printf("</body>");
out.printf("</html>");
23
Since PrinterWriter is of OutputStream type, we need to import java.io package additionally in
the default program.
Finally, the whole servlet Welcome.java will be as follows.
package servpack;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.printf("<html>");
out.printf("<head><title>First Servlet</title></head>");
out.printf("<body>");
out.printf("<h1>Servlet Programming - An Example</h1>");
out.printf("</body>");
out.printf("</html>");
}
}
24
After doing the editing of the program, right click and save the program.
Then right click in the Welcome.java program file and run the program as:
25
Select : 1Run on Server option. You will get the following window asking for the confirmation of
using the Tomcat9.0 server.
Click Finish button and you will get the problem message as:
26
Under Sever menu at the bottom as:
Now click on the Details button in the error window. You will get the details of the problem as:
The problem message says that one or more of the ports are invalid. Now press Ok and close the
Problem Occurred window. To correct the problem, double click on the Tomcat v9.0 sever at
localhost at the bottom of the main window. You will get the following screen.
27
Now you edit it. For the empty Tomcat admin port, you enter 8085 as port number.
After entering the port number, save the file and close the Overview window.
Now again run the program and you will get the output in the Firefox browser as given below.
28
You have now successfully installed the Tomcat9.0 server, Eclipse-2019-12-R and run a sample
servlet program.
To run the program you can use the Run icon in the main window. You can learn more when you
start using the Eclipse.
Now we will create a HTML file to be run at the client side. In this HTML we will give the name
,place ,district and state of the user as input and the servlet in the sever will echo the details back
to the client. So it requires two programs:
29
i. HTML document.: gethtml.html
<!-
Authors: K.Somasundaram and T.Kalaiselvi
e-mail: ka.somasundaram@gmail.com, kalaiselvi.gri@gmail.com
->
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Welcome</TITLE>
</HEAD>
<BODY>
<H1>HTML FORM WITH GET METHOD</H1>
<FORM METHOD="GET" ACTION="Myname">
<p> Please provide the following details</p>
<p> Name :<INPUT TYPE="text" NAME="name"></p>
<p> Place:<INPUT TYPE="text" NAME="place"></p>
<p> District:<INPUT TYPE="text" NAME="district"></p>
<p> State:<INPUT TYPE="text" NAME="state"></p>
<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>
</BODY>
</HTML>
ii.Servlet :Myname.java
package servpack;
import java.io.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
30
@WebServlet("/Myname")
First run the client side program gethtml.html. You will get the following input window:
31
Give the Inputs at client side by filling the above form generated by gethtml.html.
When you click the Submit button, you will get the following response from the sever side
(Myname.java):
32
--------------------x x x x x x x-------------------
33