JSP
Published: 2023-08-17 06:04:38

Servlet and JSP Tutorial: Web & HTTP
Web is a system of Internet servers that supports formatted documents. The documents are formatted using a markup language called HTML (HyperText Markup Language) that supports links to other documents like graphics, audio, and video files etc. 

Servlet and JSP Tutorial: Web & HTTPNow that we know what is web,  let’s move further and understand what is a website. So, a website is a collection of static files i.e. web pages like HTML pages, images, graphics etc. And, the Web application is a website with dynamic functionality on the server. Google, Facebook, Twitter are examples of web applications.

So, what is the link between the Web and HTTP? Let’s now find out.

HTTP (Hypertext Transfer Protocol)

HTTP is used by clients and servers to communicate on the web. It is considered as a stateless protocol because it supports only one request per connection. With HTTP the clients connect to the server to send one request and then disconnect. And this mechanism allows more users to connect to a given server over a period of time. Here, the client sends an HTTP request and the server answers with an HTML page to the client, using HTTP.

That was all about HTTP and Web. Now let’s dive deep into Servlets and understand its working mechanism.


Introduction to Servlets
Servlet is a server-side Java program module that handles client requests and implements the servlet interface. Servlets can respond to any type of request, and they are commonly used to extend the applications hosted by web servers.


Servlets-Introduction to Java Servlets - EdurekaIn this figure you can see, a client sends a request to the server and the server generates the response, analyses it and sends the response back to the client.

Now, let’s jump into the next concept and understand Servlet Life Cycle.

Servlet Life Cycle
The entire life cycle of a servlet is managed by the Servlet container which uses the javax.servlet.Servlet interface to understand the Servlet object and manage it.

 Servlet Life Cycle: The Servlet life cycle mainly goes through four stages:

Servlet Life Cycle-Servlet and JSP tutorial- Edureka

Loading a Servlet
When a server starts up, the servlet container deploy and loads all the servlets.

Initializing the Servlet
Next, a servlet is initialized by calling the init() method. Servlet.init() method is called by the Servlet container to notify that this Servlet instance is instantiated successfully and is about to put into service.

Request handling
Then, servlet calls service() method to process a client’s request and is invoked to inform the Servlet about the client requests.

Destroying the servlet
Finally, a servlet is terminated by calling the destroy(). The destroy() method runs only once during the lifetime of a Servlet and signals the end of the Servlet instance.


init() and destroy() methods are called only once. Finally, a servlet is garbage collected by the garbage collector of the JVM. So this concludes the life cycle of a servlet. Now, let me guide you through the steps of creating Java servlets.


 Servlet and JSP Tutorial: Steps to Create Servlet
In order to create a servlet, we need to follow a few steps in order. They are as follows:

Create a directory structure
Create a Servlet
Compile the Servlet
Add mappings to the web.xml file
Start the server and deploy the project
Access the servlet
Now, based on the above steps, let’s create a program to understand better, how a servlet works.

Course Curriculum
Java Certification Training Course
Instructor-led SessionsReal-life Case StudiesAssignmentsLifetime Access
To run a servlet program, we should have Apache Tomcat Server installed and configured. Eclipse for Java EE provides in-built Apache Tomcat. Once the server is configured, you can start with your program. One important point to note – for any servlet program, you need 3 files – index.html file, Java class file, and web.xml file. The very first step is to create a Dynamic Web Project and then proceed further.

Now, let’s take an example where I will be creating a simple login servlet and display the output in the browser.

First,  I will create index.html file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!DOCTYPE html>
<html>
<body>
 
<form action="Login" method="post">
 
<table>
 
<tr>
 
<td>Name:</td>
 
 
<td><input type="text" name="userName"></td>
 
</tr>
 
 
<tr>
 
<td>Password:</td>
 
 
<td><input type="password" name="userPassword"></td>
 
</tr>
 
</table>
 
<input type="submit" value="Login">
</form>
 
</body>
</html>
Next, let’s code the Java Class file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package Edureka;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse;
public class Login extends HttpServlet
{
protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
String user=req.getParameter("userName");
String pass=req.getParameter("userPassword");
pw.println("Login Success...!")
if(user.equals("edureka") && pass.equals("edureka"))
pw.println("Login Success...!");
else
pw.println("Login Failed...!");
pw.close();
}
}
In the above code, I have set a condition – if username and password are equal to edureka, only then it will display successfully logged in, else login will be denied. After writing the Java class file, the last step is to add mappin