Servlet in Java can be described in many ways. As a technology, the servlet is used to create web pages; as an API, which provides interfaces, etc. It is used to extend the capabilities of the server which hosts applications on a request-response programming model. Servlets provide component-based and a platform-independent method to build web-based applications without any performance limitations. Servlets in Java have entire access over Java API’s and JDBC to access the enterprise database. We shall see in detail what are these Servlets, why are they used, its advantages and limitations, and how actually servlets work in Java.
ADVERTISEMENT Popular Course in this category JAVA SERVLET - Specialization | 18 Course Series | 6 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Servlets can be described in many other ways,
With growing technology, we need to get ourselves acquainted with the latest updates or latest tech stack daily. Servlets act as an interface, or as a technology, or as a web component, or a class, or as an API. With servlets, we can collect user information through web pages/ forms, or a database, and any other data sources and create web pages.
Servlets in Java check the communication interface, requirements of client and server, the protocol used, programming language, and the software involved. Servlets get executed in the following steps,
Step 1: The client sends a request to the web server, reads explicit data sent by the client, which can be HTML form, applet, or custom HTTP client program.
Step 2: The web server then receives the request.
Step 3: The web server then passes the request to the corresponding servlet, the processing request may include communicating with the database, invoking web service, or direct response.
Step 4: Servlet then processes the request and generates a response in the form of output. It can be in any format, HTML or XML, GIF if images, or Excel.
Step 5: These servlets then sends a response back to the server
Step 6: Then the web server sends a response back to the client and the client, as the browser display on the UI.
The above servlet architecture uses some Java methods like:
First, we need to install Java, Eclipse, and Tomcat:
1. We will create a Dynamic Web project using File-> New-> Dynamic Web Project.
2. Enter Project Name and select Target Runtime, Clicking on Next, need to check mark “Generate web.xml” and then Finish
3. The project structure will look somewhat as below.
4. Then, Click on File-> Create New Servlet.
5. Click on Finish above. Now Eclipse will generate Servlet Class based on the inputs or configuration done in previous steps.
Code:
FirstProgram.java
package com.srccode.example; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class FirstProgram */ @WebServlet("/FirstProgram") public class FirstProgram extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FirstProgram() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
We shall modify our code for Servlet Class as below,
package com.srccode.example;
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; /** * Servlet implementation class FirstProgram */ @WebServlet("/FirstProgram") public class FirstProgram extends HttpServlet { private static final long <em>serialVersionUID</em> = 1L; /** * @see HttpServlet#HttpServlet() */ public FirstProgram() { super(); // TODO Auto-generated constructor stub } private String mymsg; public void init() throws ServletException { mymsg = "Hi eduCBA Team! We are working on Java Servlet Tutorial! This is the first Servlet Program!"; } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); PrintWriter printWriter = response.getWriter(); printWriter.println("<h1>" + mymsg + "</h1>"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
In web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>ServletExample</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
In index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>BeginnersBook Servlet Demo</title> </head> <body> <a href="welcome">Click to call Servlet</a> </body> </html>
Output:
Right, Click on the Project and Select Run As-> Run on Server.
Now Open the Browser and we can see the below Output, server will run on localhost:
http://localhost:8080/ServletExample/FirstProgram
There are many advantages of Servlet in Java. Servlets can be taken as applet running on the server-side:
With this, we conclude the topic ‘Servlet in Java’. We have seen what Servlets are in Java and How are they used with an example. We have also seen its advantages and learned how Servlets can be used step by step with Servlet Architecture and Servlet methods used. Also seen Why Servlets are used in Java and its advantages over CGI. We have much more to explore on Servlets, there are types of Servlets also available, will dig deeper in further tutorials.
The above is the detailed content of Servlet in Java. For more information, please follow other related articles on the PHP Chinese website!