The servlet method is an essential part of the servlet life cycle to create, operate, and maintain web applications as per user requirements. The servlet method creates, initializes, handles, and closes the web application’s operation cycle. The servlet methods are important features used to develop operational applications and invoke the web container to operate the servlet life cycle. The servlet provides multiple methods to send requests, get a response from the server, and operate operations as required.
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
The server-side component, a servlet, is a potent tool for creating server-side apps. Building Web-based applications with Servlets is a component-based, platform-independent solution that does not have the performance restrictions of CGI programs. Web developers may quickly and effectively create server-side applications using servlets, and these apps can operate on any Web server that supports servlets. Inside the Java virtual computer, Servlets can function. The browser is not tested for compatibility while the servlet runs on the server.
Servlets have access to the Java API family, including the JDBC API for enterprise database access. Several Java classes in the javax.servlet and javax.servlet.http packages employ servlets HTTP protocols. Additionally, servlets have access to a library of HTTP-specific calls and can take advantage of all the Java language’s mature features, such as portability, performance, reuse, and crash protection.
The generic servlet uses and operates the following five methods in the operational servlet life cycle:
The servlet container only calls the init() method once in a servlet operation. This inits() method informs the servlet that it puts into service.
One of the following conditions must use with the init method:;[p ‘mi;/.
Syntax:
public void init(ServletConfig configs) throws ServletException{ //initialize servlet object and parameters. }
The ServletConfig object that holds the initialization functions and basic servlet settings are passed to the init() method, which throws a ServletException if an error has occurred.
The servlet container calls the service() method once the servlet begins receiving requests so it may react. The Servlet container also passes ServletResponse to the database and display page.
Two objects which are javax.servlet.ServletRequest and javax.servlet.ServletResponse help the servlet process to the client request.
Syntax:
public void service(ServletRequest requests, ServletResponse responses) throws ServletException{ //Pass servlet object and parameters. }
The Servlet Container creates a servlet method called ServletConfig(). It is provided to the servlet during the initialization of the object life cycle. It contains some initial parameters or configuration data. It is recommended to save servlet-specific information in web.xml.
Syntax:
public String getServletConfig(){ // Add servlet code. }
A servlet method displays or returns servlet information from the container. This information is available on the servlet container for operation.
Syntax:
public String getServletInfo(){ // Add servlet code. }
The servlet destroys method uses to close the servlet life cycle and connections. It ends the servlet functionality after displaying the required output.
Syntax:
public void destroy(){ // End servlet connections. }
The following examples show servlet different methods and their output as per operations:
The Hello World servlet methods example and its output.
HelloOutput.java:
Code:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class HelloOutput implements Servlet { private static final long serialVersionUID = 1L; public HelloOutput() { } ServletConfig configurates=null; @Override public void init(ServletConfig configurates) throws ServletException { this.configurates = configurates; System.out.println("Servlet Object initializes here."); } @Override public void destroy() { System.out.println("Close connection and End process here."); } @Override public ServletConfig getServletConfig() { return configurates; } @Override public String getServletInfo() { return "Educba Website"; } @Override public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printout = response.getWriter(); printout.println("<h2>Hello World First Example using " + "Servlet Methods.</h2>"); printout.close(); } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name> HelloOutput </servlet-name> <servlet-class> com.educba.learn.HelloOutput </servlet-class> </servlet> <servlet-mapping> <servlet-name> HelloOutput </servlet-name> <url-pattern> /HelloOutput </url-pattern> </servlet-mapping> </web-app>
Output:
The servlet methods example with getServletInfo() method output.
HelloOutput.java:
Code:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class HelloOutput implements Servlet { private static final long serialVersionUID = 1L; public HelloOutput() { } ServletConfig configurates=null; @Override public void init(ServletConfig configurates) throws ServletException { this.configurates = configurates; System.out.println("Servlet Object initializes here."); } @Override public void destroy() { System.out.println("Close connection and End process here."); } @Override public ServletConfig getServletConfig() { return configurates; } @Override public String getServletInfo() { return "Educba Website"; } @Override public void service(ServletRequest requests, ServletResponse responses) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printout = response.getWriter(); printout.println("<h2>Hello World First Example using " + getServletInfo() + ".</h2>"); printout.close(); } }
web.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name> HelloOutput </servlet-name> <servlet-class> com.educba.learn.HelloOutput </servlet-class> </servlet> <servlet-mapping> <servlet-name> HelloOutput </servlet-name> <url-pattern> /HelloOutput </url-pattern> </servlet-mapping> </web-app>
Output:
The servlet methods example with html element and its output.
HelloOutput.java:
Code:
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class HelloOutput implements Servlet { private static final long serialVersionUID = 1L; public HelloOutput() { } ServletConfig configurates=null; @Override public void init(ServletConfig configurates) throws ServletException { this.configurates = configurates; System.out.println("Servlet Object initializes here."); } @Override public void destroy() { System.out.println("Close connection and End process here."); } @Override public ServletConfig getServletConfig() { return configurates; } @Override public String getServletInfo() { return "Educba Website"; } @Override public void service(ServletRequest requests, ServletResponse responses) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter printout = response.getWriter(); printout.print("<html><body>"); printout.println("<h5> Simple servlet method example </h5>"); printout.println("<p> Simple servlet method example </p>"); printout.print("</body></html>"); } }
web.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name> HelloOutput </servlet-name> <servlet-class> com.educba.learn.HelloOutput </servlet-class> </servlet> <servlet-mapping> <servlet-name> HelloOutput </servlet-name> <url-pattern> /HelloOutput </url-pattern> </servlet-mapping> </web-app>
Output:
The servlet method plays an important role in web application functionality. It shows and defines about server and displays the output.
The above is the detailed content of Servlet Methods. For more information, please follow other related articles on the PHP Chinese website!