This section describes the ServletContext and ServletConfig interfaces. Through these two interfaces, we can. set some parameters in xml, such as the database address and user name and password, to be used by Servlet, so that the Servlet does not need to be re-compiled every time the parameter value changes.
The javax. servlet. ServletContext interface (context parameter) is a global setting for the entire project, while the javax. servlet. ServletConfig interface (initialization parameter) is a setting for the current Servlet. Both settings are written in web. xml. When creating a Servlet in Eclipse, you can see the location where the initialization parameters are set, for example:
Context parameters are not so convenient. You can only manually edit web. xml to add them. Fortunately, Eclipse provides a convenient XML editor. After opening web. xml, you can see the configuration of each Servlet. Right-click the top-level "web-app" element and chooseAdd Child-> context-paramAnd modify the param-name And param-value respectively.
How to obtain these two types of parameters in the program? See the following methods:
javax.servlet.GenericServlet.getServletConfig()
Returns the ServletConfig object of the current Servlet.
javax.servlet.ServletConfig.getServletContext()
Returns the ServletContext object of the current application.
Through these two methods, we can obtain the initialization parameter object and context parameter object of the current Servlet.
The main methods of ServletContext:
String getInitParameter(String name)
Obtains the value of the specified context parameter.
The main methods of ServletConfig are as follows:
String getInitParameter(String name)
Obtains the value of the specified initialization parameter.
java.util.Enumeration getInitParameterNames()
Obtain the names of all initialization parameters.
The sample program reads and displays the values through ServletContext and ServletContent. Because the parameter value remains unchanged in the Servlet lifecycle and only needs to be read once, the code for obtaining the parameter is written in the init () method instead of the doGet () method. For other things that need to be explained, refer to the previous article for parameter appending methods.
Download the example: initparam_jb51net.zip
++ =
Copy codeThe Code is as follows: package com. idv2.learnjsp;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletConfig;
Import javax. servlet. ServletContext;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
/**
* Servlet implementation class for Servlet: InitParam
*
*/
Public class InitParam extends javax. servlet. http. HttpServlet implements javax. servlet. Servlet {
/**
* Automatically generated serial number
*/
Private static final long serialVersionUID = 7732869603825506920L;
/**
* Program name
*/
Private String appName;
/**
* Page title
*/
Private String title;
Public void init (){
// Obtain parameters
ServletConfig config = getServletConfig ();
ServletContext context = config. getServletContext ();
Title = config. getInitParameter ("title ");
AppName = context. getInitParameter ("appname ");
}
/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # doGet (HttpServletRequest request, HttpServletResponse response)
*/
Protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Response. setContentType ("text/html; charset = UTF-8 ");
PrintWriter out = response. getWriter ();
Out. println ("Out. println ("Out. println ("<title>" + title + "</title> ");
Out. println ("Out. println ("<body> ");
Out. println ("Application name:" + appName );
Out. println ("</body> ");
Out. println ("}
}