How to develop a servlet
The mapping path of the servlet
servlet Default Path
The life cycle of Sevlet
Automatic loading of Servlets
The Init method with parameters and the Init method without parameters
Multithreading concurrency problems for servlets
ServletConfig Object
ServletContext Object
Servlet Learning
Serial Number |
Object |
function |
1 |
HttpServletRequest Request Object |
GET request Information |
2 |
HttpServletResponse Response Object |
Setting the Response object |
3 |
ServletConfig Object |
Servlet Configuration Objects |
4 |
ServletContext Object |
The context object for the servlet |
9. ServletConfig Object
9.1, the role of ServletConfig objects
ServletConfig object: primarily used to load the servlet's initialization parameters . Multiple ServletConfig objects can exist in a Web application (one servlet corresponds to one ServletConfig object)
9.2. Object creation and getting
Creation time: After the Servlet object is created, it is created before calling the Init method.
Get object: Getservletconfig ()!!!
Part of the source code of the Javax.servlet.GenericServlet class:
package javax.servlet;import java.io.ioexception;import java.util.enumeration;public abstract class genericservlet implements servlet, servletconfig, Java.io.serializable{private transient servletconfig config;/** * does nothing. All of the servlet initialization is done by one of The <code>init</code> methods. */public genericservlet () { }/** * called by the servlet container to indicate that the servlet is being placed into service. */ public void init (Servletconfig config) throws servletexception {this.config = config;this.init (); } /** * a convenience Method which can be overridden so that there ' s no need * to call <code>super.init (config) </code>. * * instead of overriding init ( ServletConfig), simply override * this method and It will be called by <code>genericservlet.init (ServletConfig config) </ Code>. * the <code>servletconfig</code> object can still be retrieved via getServletConfig. */ public void init () throws ServletException { } /** * Returns this Servlet ' S&NBSP;SERVLETCONFIG&NBSP;OBJECT.&NBSp; */ public servletconfig getservletconfig () { return config; } public string getservletname () { return config.getservletname (); } public string Getinitparameter (String name) {return getservletconfig (). Getinitparameter (name); public enumeration getinitparameternames () { Return getservletconfig (). Getinitparameternames (); } /** * returns a reference to the ServletContext in which this servlet is running. */ &nBsp;public servletcontext getservletcontext () { return Getservletconfig (). Getservletcontext (); }}
1) The Tomcat server encapsulates these parameters into the ServletConfig object when the Web application is loaded
2) The Tomcat server calls the Init method to pass in the ServletConfig object
9.3. servlet initialization parameter configuration and ServletConfig API
In the configuration information of the servlet, plus <init-param>, you can configure multiple!!
Example of initialization parameter configuration for servlet:
<servlet> <servlet-name>ConfigDemo</servlet-name> < Servlet-class>com.rk.http.d_config. configdemo</servlet-class> <!-- Initial parameters: These parameters are encapsulated into ServletConfig objects when the Web app is loaded --> <init-param> <param-name>path</param-name> < Param-value>d:/rk/mytest.txt</param-value> </init-param> <init-param > <param-name>website</param-name> <param-value>www.rk.com</ Param-value> </init-param> <init-param> <param-name>rk </param-name> <param-value>lsieun</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>configdemo</ Servlet-name> <url-pattern>/config</url-pattern> </servlet-mapPing>
Note: The parameters of the servlet can only be obtained by the current Sevlet!!!
ServletConfig's API
Serial Number |
function |
function |
1 |
String Getservletname () |
Get the name of the servlet |
2 |
String Getinitparameter (string name) |
Get parameter values based on parameter name |
3 |
Enumeration Getinitparameternames () |
Get all parameter names |
4 |
ServletContext Getservletcontext () |
Get the Servlet context object |
Example code:
package com.rk.http.d_config;import java.io.bufferedreader;import java.io.file;import java.io.filereader;import java.io.ioexception;import java.io.printwriter;import java.util.enumeration;import javax.servlet.servletconfig;import javax.servlet.servletexception; import javax.servlet.http.httpservlet;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;public class configdemo extends httpservlet{@ Overrideprotected void doget (httpservletrequest request, httpservletresponse response ) throws servletexception, ioexception{response.setcharacterencoding ("UTF-8"); Response.setcontenttype ("Text/html;charset=utf-8"); Printwriter out = response.getwriter (); Servletconfig config = this.getservletconfig ();//Get Servlet object string servletname = config.getservletname ();//Get the name of the servlet out.write ("Name of the servlet:" + servletname + "<br/>") out.write ("<strong>servet configuration information for file contents under the path Path </strong> :<br/> "); String path = config.getinitparameter ("path");//Read the initial parameters of the servlet file file = new file (path); Filereader reader = new filereader (file); Bufferedreader bufferedreader = new bufferedreader (reader); String line = null;while ((Line=bufferedreader.readline ()) != null) {Out.write (line + "<br/>");} Out.write ("<strong>servlet configuration Information:</strong><br/>"); Enumeration<string> initparameternames = config.getinitparameternames ();// Queries all initialization parameters of the current servlet while (Initparameternames.hasmoreelements ()) {string paramname = Initparameternames.nextelement (); String paramvalue = config.getinitparameter (paramname); Out.write (paramName + ":" + paramValue + "<br/>");}}
Servlet Programming: (4) ServletConfig