ServletContext is an object instantiated by Web Container when we deploy a Jakarta EE web application. It is used to store and retrieve information that we configure for all the Servlets present in the application. And ServletConfig helps us get information defined for a particular Servlet. How is it in detail? In this tutorial, I will discuss with you about ServletContext and ServletConfig in Jakarta EE Servlet.
First, I will create a Jakarta EE Servlet Maven project as an example:
Now, I will create 2 new Servlets:
HelloWorldServlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.jakartaee.servlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Writing the message on the web page PrintWriter out = resp.getWriter(); out.println("Hello Servlet"); } } |
and HuongDanJavaServlet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.huongdanjava.jakartaee.servlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class HuongDanJavaServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Writing the message on the web page PrintWriter out = resp.getWriter(); out.println("Huong Dan Java Servlet"); } } |
then declare them in the web.xml file as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> <servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>com.huongdanjava.jakartaee.servlet.HelloWorldServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>huongdanjavaServlet</servlet-name> <servlet-class>com.huongdanjava.jakartaee.servlet.HuongDanJavaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>huongdanjavaServlet</servlet-name> <url-pattern>/huongdanjava</url-pattern> </servlet-mapping> </web-app> |
The result when running the application, the request to http://localhost:8080/hello is as follows:
And request to http://localhost:8080/huongdanjava as follows:
If you define information for all Servlets in your application using one or more <context-param> tags in the web.xml file as follows:
1 2 3 4 5 6 7 8 9 |
<context-param> <param-name>name</param-name> <param-value>Khanh</param-value> </context-param> <context-param> <param-name>website</param-name> <param-value>Huong Dan Java</param-value> </context-param> |
Then, in both Servlets that I defined above, I can get this configuration information using the ServletContext object, for example as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.jakartaee.servlet; import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Writing the message on the web page PrintWriter out = resp.getWriter(); out.println("Hello Servlet"); ServletContext servletContext = getServletContext(); out.println(servletContext.getInitParameter("name")); out.println(servletContext.getInitParameter("website")); } } |
Or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.jakartaee.servlet; import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class HuongDanJavaServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Writing the message on the web page PrintWriter out = resp.getWriter(); out.println("Huong Dan Java Servlet"); ServletContext servletContext = getServletContext(); out.println(servletContext.getInitParameter("name")); out.println(servletContext.getInitParameter("website")); } } |
The getServletContext() method will return the ServletContext object managed by the Web Container that the application is being deployed to, which is initialized when we deploy the application.
Result:
Or:
And if you define information for a particular Servlet by declaring the <init-param> tag inside the <servlet> tag, this information will not be available to other Servlets of the application and we get these using ServletConfig.
For example, now, I declare the web.xml file as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> <servlet> <servlet-name>helloServlet</servlet-name> <servlet-class>com.huongdanjava.jakartaee.servlet.HelloWorldServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>name</param-name> <param-value>Khanh</param-value> </init-param> <init-param> <param-name>website</param-name> <param-value>Huong Dan Java</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>helloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>huongdanjavaServlet</servlet-name> <servlet-class>com.huongdanjava.jakartaee.servlet.HuongDanJavaServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>huongdanjavaServlet</servlet-name> <url-pattern>/huongdanjava</url-pattern> </servlet-mapping> </web-app> |
then with the above code, we will not get context-param information anymore:
And:
You can get the information declared in HelloServlet using ServletConfig as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.huongdanjava.jakartaee.servlet; import jakarta.servlet.ServletConfig; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Writing the message on the web page PrintWriter out = resp.getWriter(); out.println("Hello Servlet"); ServletConfig servletConfig = getServletConfig(); out.println(servletConfig.getInitParameter("name")); out.println(servletConfig.getInitParameter("website")); } } |
Result:
Request to http://localhost:8080/huongdanjava, you will see the following result:
There are many other information that you can configure with Jakarta EE Servlet and you can also use ServletContext and ServletConfig to get this information!