This section describes the two interfaces ServletContext and ServletConfig. Through these two interfaces, we can set some parameters in the Web.xml, such as database address, username password, etc. for the servlet to use, so that each parameter value changes, do not have to recompile the servlet.
The Javax.servlet.ServletContext interface (context parameter) is the global setting for the entire project, whereas the Javax.servlet.ServletConfig interface (initialization parameters) is The settings for the current servlet. The settings for both are written in Web.xml. When you create a new servlet in eclipse, you can see where you set the initialization parameters, as shown in the following figure:
The context parameter is not so convenient, can only be added by hand-edited web.xml. Fortunately, Eclipse provides a handy XML editor, opens Web.xml and then sees the individual servlet configuration, right click on the topmost "Web-app" element, and select Add child->context-param , and then modify param-name and Param-value respectively.
So how do you get these two types of arguments in a program? Take a look at these several ways:
javax.servlet.GenericServlet.getServletConfig()
Returns the ServletConfig object for the current servlet.
javax.servlet.ServletConfig.getServletContext()
Returns the ServletContext object for the current application.
With these two methods, we can get the initialization parameter object and the context parameter object of the current servlet.
The main methods of ServletContext:
String getInitParameter(String name)
Gets the value of the specified context parameter.
The main methods of ServletConfig:
String getInitParameter(String name)
Gets the value of the specified initialization parameter.
java.util.Enumeration getInitParameterNames()
Gets the name of all initialization parameters.
The sample program reads the values and displays them through ServletContext and servletcontent respectively. Because the value of the parameter is constant in the servlet's lifecycle, it only needs to be read once, so the code that takes the parameter is written in the init () method instead of the Doget () method. Other places where there is no special need to explain, the method of parameter appending can be referenced in the preceding text.
Sample Downloads: Initparam_jb51net.zip
+++++++++++++++++++++++++++++++++++++==
Copy Code code 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 () {
Get 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 ("}
}