Java learning notes-ServletConfig, ServletContext interface (13), servletcontext
ServletConfig is an object created and passed in by the Tomcat server when initializing the Servlet.
This object mainly describes the configuration information of a servlet.
For example:
<Servlet> Configure a servlet <servlet-name> helloservlet </servlet-name> to specify the servlet name (any) to specify the fully qualified name (package name) of the specific servlet class. class Name) <servlet-class> cn. itcast. servlets. helloServlet </servlet-class> </servlet> <servlet-mapping> maps a servlet <servlet-name> helloservlet </servlet-name> servlet name to be mapped <url-pattern >/helloservlet </url-pattern> path to be accessed by the browser </servlet-mapping>
The above configuration is the configuration information of a servlet. If the developer needs to obtain the data, the developer can obtain the information from the created ServletConfig interface object of tomcat.
Example 1: Get the configuration name of the specified servlet.
Public class Demo1 extends HttpServlet {private ServletConfig config = null; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// get servlet configuration name String name = this. config. getServletName (); response. getOutputStream (). write (name. getBytes ();} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );} // Override the init method @ Override public void init (ServletConfig config) throws ServletException {super. init (config); this. config = config ;}}
Example 2: Obtain the servlet configuration parameters.
In the actual programming process, all variable data needs to be encapsulated into the configuration file, so as to facilitate the later maintenance of the software. Android.
Public VoidDoGet (HttpServletRequest request, HttpServletResponse response)ThrowsServletException, IOException {
// Obtain the servlet configuration name
String name =This. Config. getServletName ();
Response. getOutputStream (). write (name. getBytes ());
// Obtain the configuration parameters
String ip =This. Config. getInitParameter ("ip ");
String dbtype =This. Config. getInitParameter ("dbtype ");
Response. getOutputStream (). write ("<br/>". getBytes ());
Response. getOutputStream (). write (ip + "," + dbtype). getBytes ());
// Obtain all configuration parameters cyclically
Enumeration <String> names =This. Config. getInitParameterNames ();
While(Names. hasMoreElements ()){
String ini_name = names. nextElement ();
String ini_value =This. Config. getInitParameter (ini_name );
Response. getOutputStream (). write (ini_name + "=" + ini_value + "<br/>"). getBytes ());
}
}
Conclusion: It is difficult to receive the servletconfig object created and passed by tomcat.
// Obtain the servletconfig object ServletConfig config = this. getServletConfig (); System. out. println (this. config = config );
To facilitate developers to obtain the servletconfig object, HttpServlet directly provides getServletConfig (),
The two methods obtain the same object.
ServletContext Interface
The ServletContext interface object mainly implements communication between servlets. This object can communicate with the web server. Forward requests.
The ServletContext interface is an abstract description of a website. A ServletContext represents a web application (website ).
This object is created by Tomcat and passed through the initialization method together with the ServletConfig object.
1. Implement servlet Communication
1. Put data
// Obtain the public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ServletContext context1 = this. getServletConfig (). getServletContext (); ServletContext context2 = this. getServletContext (); System. out. println (context1 = context2); // true // one of the main functions is to implement data communication between servler context1.setAttribute ("news_title", "Haze Weather in Beijing ");}
2. Fetch data
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { String name = (String) this.getServletContext().getAttribute("news_title"); response.getOutputStream().write(name.getBytes()); }
2. Implement Server Communication
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// communicates with tomcat to obtain the MIME type ServletContext context = this. getServletContext (); String type = context. getMimeType ("/jnb.doc"); System. out. println (type );}
1. Obtain website resources (important)
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// obtain the website object ServletContext context = this. getServletContext (); // obtain the static resource InputStream in = context in the website. getResourceAsStream ("/123.bmp"); // obtain the output stream FileOutputStream out = new FileOutputStream (new File ("d:/123.bmp ")); // read and write byte [] bs = new byte [1024]; int len = 0; while (len = in. read (bs )) ! =-1) {out. write (bs, 0, len); out. flush () ;}// release the resource in. close (); out. close ();}
3. Request forwarding
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// obtain the website object ServletContext context = this. getServletContext (); // forward System. out. println ("sorry, no money !, Find the Old Tang to go to... "); context. getRequestDispatcher ("/lt "). forward (request, response );}
Request forwarding: The request Forwarding is done by the server itself, so it cannot be displayed on the client, so the address bar of the browser will not change.