5.3. Create and use context and init parameters
- Application init parameters
The class that the container will use at application level is ServletContext.
Defining init parameters in deployment descriptor:
Defining init parameters in deployment descriptor:
<context-param> <param-name>name1</param-name> <param-value>value1</param-value> </context-param>
Retrieving init parameters:
getServletContext().getInitParameter("parameter1");
For the initialization process in an application the ServletContextListener can be defined (this code will run before the rest of the application for example to get the init parameters and create a database connection,..etc).
For the initialization process in an application the ServletContextListener can be defined (this code will run before the rest of the application for example to get the init parameters and create a database connection,..etc).
<context-param> <param-name>name1</param-name> <param-value>value1</param-value> </context-param> <listener> <listener-class>example.MyServletContextListener</listener-class> </listener>
Example: The servletContextListener class will retrieve the init parameters, in this case a property "blogName", and then servletContext will be accessed to display the property.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<context-param> | |
<param-name>blogName</param-name> | |
<param-value>http://javaWithBreakfast.blogspot.com</param-value> | |
</context-param> | |
<listener> | |
<listener-class> | |
example.MyServletContextListener | |
</listener-class> | |
</listener> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletContextEvent; | |
import javax.servlet.ServletContextListener; | |
public class MyServletContextListener implements ServletContextListener { | |
@Override | |
public void contextInitialized(ServletContextEvent sce) { | |
ServletContext sc = sce.getServletContext(); | |
String blogURL = sc.getInitParameter("blogName"); | |
sc.setAttribute("Blog", blogURL); | |
} | |
@Override | |
public void contextDestroyed(ServletContextEvent sce) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" | |
pageEncoding="ISO-8859-1"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title> JavaWithBreakfast Blog </title> | |
</head> | |
<body> | |
<p> | |
<%=getServletContext().getAttribute("Blog") %> | |
</p> | |
<br/> | |
<form action="login.do" method="post"> | |
Enter user:<input type="text" name="user"/><br/><br/> | |
Password :<input type="password" name="password"/><br/><br/> | |
<input type="submit" value="login"/> | |
</form> | |
</body> | |
</html> |
This can be useful when we define properties as user, password, urls, driver and we want to create a database connection (at application level), and it will be perform before any servlet is initialized. It can be useful to access to properties file or other resources (IO operations) to retrieve global parameters.
- Servlet init parameters
ServletConfig methods:
-Parameters: getInitParameter(String name), getInitParameterNames()
-Context : getServletContext()
To use init parameters there are 2 options:
- By deployment descriptor
- By annotations
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>example.MyServlet</servlet-class>
<init-param>
<param-name>parameter1</param-name>
<param-value>value1</param-value>
</init-param>
<init-param>
<param-name>parameter2</param-name>
<param-value>value2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
- a complete path: /name/MyName.do
- a directory: /name/*
- an extension: *.do
The way to retrieve the init parameter in the code is:
getServletConfig().getInitParameter("parameter1");
5.4. Use annotations to configure servlets
From the specification Servlets3.0 is possible to declare servlets by annotations (an alternative to deployment descriptor)
@WebServlet(name="MyServlet",
urlPatterns={"/MyServlet"},
initParams={@WebInitParam(name="parameter1", value="value1"),
@WebInitParam(name="parameter2", value="value2")})
public class MyServlet extends HttpServlet {
WebListener annotation:
@WebListener
public class MyServletContextListener implements ServletContextListener {
OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification
No comments:
Post a Comment