In this servlet tutorial we will see examples of servlet, Java Servlets is a web technology for Java. It was the first web technology for Java and many new web technologies have arrived since. Still, Java Servlets are very useful, both to know, and for certain use cases.
Java Servlets are part of the Java Enterprise Edition (Java EE). You will need to run your Java Servlets inside a Servlet compatible “Servlet Container” (e.g. web server) in order for them to work.
Java Servlets it Java technology for creating the dynamic web applications. Java Servlets are server side components in Java that runs on Servlet enabled web server such as Tomcat, Jetty, WebSphere etc.. Java Servlet is much faster then CGI and Perl since it runs in the same JVM. In case of CGI and Perl separate memory space is allocated for execution of the program which reduces it’s performance.
Servlet technology is robust and scalable as it uses the java language. Before Servlet, CGI (Common Gateway Interface) scripting language was used as a server-side programming language. But there were many disadvantages of 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.
- Servlet is a technology i.e. used to create web application.
- Servlet is an API that provides many interfaces and classes including documentations.
- Servlet is an interface that must be implemented for creating any servlet.
- Servlet is a class that extend the capabilities of the servers and respond to the incoming request. It can respond to any type of requests.
- Servlet is a web component that is deployed on the server to create dynamic web page.
Following diagram shows the position of Servelts in a Web Application.
Servlets perform the following major tasks:
Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it could also come from an applet or a custom HTTP client program.
Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types and compression schemes the browser understands, and so forth.
Process the data and generate the results. This process may require talking to a database, executing an RMI or CORBA call, invoking a Web service, or computing the response directly.
Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters, and other such tasks.
Java Servlets are Java classes run by a web server that has an interpreter that supports the Java Servlet specification.
Servlets can be created using the javax.servlet and javax.servlet.http packages, which are a standard part of the Java’s enterprise edition, an expanded version of the Java class library that supports large-scale development projects.
Servlet Tutorial Table of Contents
This Servlet Tutorial has a list of the topics covered in this Java Servlet trail. This list (menu) is also present at the top right of every page in the trail.
- Java Servlets Overview
- Servlets – Environment Setup
- Servlet Life Cycle
- Servlet API
- Servlet Example
- What is web application?
- Advantages of Servlets over CGI
- Servlets – Form Data
- Send Redirect in Servlet
- Cookies Handling
- Session Tracking
- Servlet Filter
- Database Access
- File Uploading
- Hits Counter
- Auto Refresh
- Sending Email
- Servlet Internationalization