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

Javaservlets

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

Javaservlets

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

Servlets | Servlet Tutorial

Servlet technology is used to create a web application (resides at server side and
generates a dynamic web page).

Servlet technology is robust and scalable because of java language. Before Servlet,
CGI (Common Gateway Interface) scripting language was common as a server-side
programming language. However, there were many disadvantages to this
technology. We have discussed these disadvantages below.

There are many interfaces and classes in the Servlet API such as Servlet,
GenericServlet, HttpServlet, ServletRequest, ServletResponse, etc.

What is a Servlet?
Servlet can be described in many ways, depending on the context.

o Servlet is a technology which is used to create a web application.


o Servlet is an API that provides many interfaces and classes including
documentation.
o Servlet is an interface that must be implemented for creating any Servlet.
o Servlet is a class that extends the capabilities of the servers and responds to
the incoming requests. It can respond to any requests.
o Servlet is a web component that is deployed on the server to create a dynamic
web page.
What is a web application?
A web application is an application accessible from the web. A web application is
composed of web components like Servlet, JSP, Filter, etc. and other elements such
as HTML, CSS, and JavaScript. The web components typically execute in Web Server
and respond to the HTTP request.

CGI (Common Gateway Interface)


CGI technology enables the web server to call an external program and pass HTTP
request information to the external program to process the request. For each
request, it starts a new process.
Disadvantages of CGI
There are many problems in CGI technology:

1. If the number of clients increases, it takes more time for sending the response.
2. For each request, it starts a process, and the web server is limited to start processes.
3. It uses platform dependent language e.g. C, C++, perl.

Advantages of Servlet

There are many advantages of Servlet over CGI. The web container creates threads
for handling the multiple requests to the Servlet. Threads have many benefits over
the Processes such as they share a common memory area, lightweight, cost of
communication between the threads are low. The advantages of Servlet are as
follows:

1. Better performance: because it creates a thread for each request, not process.
2. Portability: because it uses Java language.
3. Robust: JVM manages Servlets, so we don't need to worry about the memory
leak, garbage collection, etc.
4. Secure: because it uses java language.

A simple java servlet


Certainly! Below is a simple example of a Java Servlet that receives a request from a client
and sends a basic HTML response. In this example, the servlet extends the HttpServlet
class and overrides the doGet method to handle HTTP GET requests.
import java.io.IOException;
import java.io.PrintWriter;

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("/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {
// Set the content type and create a PrintWriter for sending the response
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// Write the HTML response


out.println("<html>");
out.println("<head><title>Simple Servlet</title></head>");
out.println("<body>");
out.println("<h2>Hello, this is a simple servlet!</h2>");
out.println("</body>");
out.println("</html>");

// Close the PrintWriter


out.close();
}
}

In this example:
 The @WebServlet("/SimpleServlet") annotation maps the servlet to the URL
pattern "/SimpleServlet".
 The doGet method is called when the servlet receives an HTTP GET request.
 The response.setContentType("text/html"); sets the content type of the
response to HTML.
 The PrintWriter out = response.getWriter(); is used to send the response to
the client.
 The HTML content is written using out.println() statements.

Make sure to deploy this servlet to a servlet container such as Apache Tomcat. You
can access the servlet by navigating to http://localhost:8080/your-web-app-
context/SimpleServlet in your web browser, where "your-web-app-context" is the
context path of your deployed web application.

Note: If you're using a more recent version of Java EE or Jakarta EE, you may not
need the web.xml deployment descriptor for simple cases, thanks to the use of
annotations like @WebServlet. If you're using an older version, you might need to
configure the servlet in the web.xml file.

Anatomy of a java servlet

The anatomy of a Java Servlet involves understanding its structure, lifecycle methods, and
how it handles HTTP requests and responses. Here's an overview:

1. Servlet Class:
 A Java Servlet is a Java class that extends one of the servlet classes provided by the Java
Servlet API. The commonly used class is HttpServlet.
 The servlet class should override one or more of the HTTP method-specific service methods
(e.g., doGet, doPost) to handle different types of HTTP requests.

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

public class MyServlet extends HttpServlet {


protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws IOException {

// Handling HTTP GET requests

// ...

You might also like