AJ-Servlet
AJ-Servlet
4/24/2025 1
Introduction to Servlet
4/24/2025 2
Advantage of Servlet over "Traditional" CGI
Java Servlet is :
o more efficient
o easier to use
o more powerful
o more portable
4/24/2025 3
1. Efficient
4/24/2025 4
Similarly, in traditional CGI, if there are N simultaneous
request to the same CGI program, then the code for the CGI
program is loaded into memory N times.
4/24/2025 5
2. Convenient
o handling cookies
4/24/2025 6
3. Powerful
4/24/2025 7
4. Portable
4/24/2025 8
5. Inexpensive
4/24/2025 9
Phases of Servlet life cycle (Anatomy of a Java Servlet)
◦ The statements in init() method are executed once during the life
of Java Servlet.
4/24/2025 10
Syntax of init() method is
4/24/2025 11
4. Service method is invoked
◦ Web container calls service method each time when request for
the servlet is received.
◦ The service() method examines the HTTP request type and then
calls the appropriate request method such as doGet() and
doPost().
4/24/2025 12
5. Destroy method is invoked
etc.
4/24/2025 13
There are three states of Servlet new, ready, end.
4/24/2025 14
Using Tomcat for Servlet development
15
2. Write the servlet source code.
6. Run Tomcat.
16
Program to print “HelloWorld” on Web server (static web page)
Right click on HelloWorld→ New→ HTML file→ specify file name (index.html)
<!DOCTYPE html>
<html>
<body>
<h2>Hello World</h2>
</body>
</html>
4/24/2025 17
Program Execution Procedure
4/24/2025 18
Reading Data from a Client
4/24/2025 19
getParameter() requires one argument, which is the name of
parameter that contains the data sent by the client.
getParameter() returns the String object.
4/24/2025 20
Sending Data to a Client
4/24/2025 21
Lab Program: 1
index.html
<!DOCTYPE html>
<html>
<body>
<form action="greet">
Enter User Name:<input type="text" name="uname"><br>
<input type="submit">
</form>
</body>
</html>
4/24/2025 22
HelloUserName.java
package rachana;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
4/24/2025 23
web.xml
4/24/2025 24
Lab Program: 2
index.html
<!DOCTYPE html>
<html>
<body>
<form action="student">
Enter Name:<input type="text" name="name"><br>
Enter USN:<input type="text" name="usn"><br>
Total marks:<input type="text" name="marks"><br>
<input type="submit">
</form>
</body>
</html>
4/24/2025 25
StudentServlet.java
package rachana;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
4/24/2025 27
Program to receive two numbers and print the sum of it on web server.
index.html
<!DOCTYPE html>
<html>
<body>
<form action="add“ method=“post”>
Enter first number:<input type="text" name="num1"><br>
Enter second number:<input type="text" name="num2"><br>
<input type="submit">
</form>
</body>
</html>
4/24/2025 28
AddServlet.java
package DemoAdd;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
4/24/2025 30
Explanation
4/24/2025 32
Outgoing data is sent by using a PrintWriter object using the
println() method and is forwarded to the client that made the
request.
4/24/2025 33
Deployment Descriptor
The file is called the web.xml file and contains the header,
DOCTYPE, and web app element.
1. servlet name
2. servlet class
3. servlet mapping
4/24/2025 34
The servlet name elements contain the name used to access
the java servlet.
Eg:- localhost:8080/DemoApp/add?num1=10&num2=10
4/24/2025 35
Example file
Application2.2//EN”> ..doctype
<web-app>
<servlet>
<servlet-name>MyJavaservlet</servlet-name>
<servlet-class>myPackage.MyJavaservletClass</servlet-class>
4/24/2025 36
<servlet -mapping>
<url-pattern> /abc</url-pattern>
</servlet -mapping>
</servlet>
</web-app>
4/24/2025 37
The Servlet API
38
javax.servlet package- core interfaces
Interface Description
39
javax.servlet package- core classes
Class Description
40