0% found this document useful (0 votes)
24 views

Servlets

Servlets provide benefits for web application development such as improved performance over CGI, portability across operating systems, rapid development using the Java library, and robustness through management by the Java Virtual Machine. A servlet application is created by defining a directory structure under Tomcat, writing the servlet Java class, compiling it, creating a deployment descriptor, running Tomcat, and making requests to the servlet URL.

Uploaded by

adityapat
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Servlets

Servlets provide benefits for web application development such as improved performance over CGI, portability across operating systems, rapid development using the Java library, and robustness through management by the Java Virtual Machine. A servlet application is created by defining a directory structure under Tomcat, writing the servlet Java class, compiling it, creating a deployment descriptor, running Tomcat, and making requests to the servlet URL.

Uploaded by

adityapat
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chap 3

Servlet
The Benefits of Servlets

 Performance. The performance of servlets is superior


to CGI because there is no process creation for each client
request. Instead, each request is handled by the servlet
container process. After a servlet is finished processing a
request, it stays resident in memory, waiting for another
request.

 Portability. Similar to other Java technologies, servlet


applications are portable. You can move them to other
operating systems without serious hassles.
 Rapid development cycle. As a Java technology, servlets
have access to the rich Java library, which helps speed up
the development process.

 Robustness. Servlets are managed by the Java Virtual


Machine. As such, you don't need to worry about memory
leak or garbage collection, which helps you write robust
applications.

 Widespread acceptance. Java is a widely accepted


technology. This means that numerous vendors work on
Java-based technologies
Servlet Application Architecture
How a Servlet Works
Step 1: Create a Directory Structure Under Tomcat

tyit

WEB-INF *.html *.jsp

classes

*.class
Six Steps to Running Your First Servlet

 1. Create a directory structure under Tomcat


for your application.
 2. Write the servlet source code. You need to
import the javax.servlet package and the
 javax.servlet.http package in your source file.
 3. Compile your source code.
 4. Create a deployment descriptor.
 5. Run Tomcat.
 6. Call your servlet from a web browser
Step 2: Write the Servlet Source Code

import javax.servlet.*;
import java.io.*;

public class TestingServlet extends GenericServlet


{
public void service(ServletRequest req,ServletResponse
res) throws ServletException,IOException
{
System.out.println("service");
}
 }
Step 3: Compile Your Source Code

javac -classpath C:\tomcat\common\lib\servlet.jar


TestingServlet.java
Step 4: Create the Deployment Descriptor

<web-app>
<servlet>
<servlet-name>Testing</servlet-name>
<servlet-class>TestingServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>first</url-pattern>
</servlet-mapping>
</web-app>
 Step 5: Run Tomcat
Step 6: Call Your Servlet from a Web Browser

http://localhost:8080/tyit/first

You might also like