Java EE learning-servlet initialization parameters

Source: Internet
Author: User

1. The ServletConfig object needs to be defined to receive the initialization parameters of the servlet configuration.

2. when the servlet is configured with initialization parameters, the Web container automatically encapsulates these initialization parameters into the ServletConfig object when it creates the Servlet instance object, and when invoking the Servlet's Init method, Pass the ServletConfig object to the servlet. Furthermore, the programmer can get the initialization parameter information of the current servlet through the ServletConfig object.

3. Note: get servlet initial value (need to initialize in init () method) //Note that it must be set up here to automatically load,<load-on-startup>1</load-on-startup> //or configure the map to initialize the servlet initialization parameters, which cannot be initialized directly in the class.

Here's the code:

*helloworld.java

1  PackageMyWeb;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 Importjava.util.Enumeration;6 7 ImportJavax.servlet.ServletConfig;8 Importjavax.servlet.ServletException;9 ImportJavax.servlet.annotation.WebServlet;Ten ImportJavax.servlet.http.HttpServlet; One Importjavax.servlet.http.HttpServletRequest; A ImportJavax.servlet.http.HttpServletResponse; -  - /** the * Servlet Implementation class HelloWorld -  */ -  Public classHelloWorldextendsHttpServlet { -     Private Static Final LongSerialversionuid = 1L; +     /** - * Define the ServletConfig object to receive the initialization parameters of the servlet configuration +      */ A     Privateservletconfig config; at     /** - * After the servlet has configured the initialization parameters, the Web container, when creating the Servlet instance object, - * These initialization parameters are automatically encapsulated into the ServletConfig object, and when calling the Servlet's Init method, - * Pass the ServletConfig object to the servlet. In turn, programmers can use ServletConfig objects to - * Gets initialization parameter information for the current servlet.  -           */ in @Override -           Public voidInit (servletconfig config)throwsservletexception { to               This. config =config; +          } -     /** the      * @seeHttpservlet#doget (httpservletrequest request, httpservletresponse response) *      */ $     protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {Panax Notoginseng          -         //get the servlet initial value (needs to be initialized in the init () method) the         //Note that this must be set to automatically load,<load-on-startup>1</load-on-startup> +         //or configure the map to initialize the servlet initialization parameters, which cannot be initialized directly in the class A         //get the initialization parameters configured in Web. XML theString Paramval = This. Config.getinitparameter ("name");//gets the specified initialization parameter + response.getwriter (). print (paramval); -Response.getwriter (). Print ("); $                  //get all the initialization parameters $Enumeration<string> e =config.getinitparameternames (); -                   while(E.hasmoreelements ()) { -String name =e.nextelement (); theString value =Config.getinitparameter (name); -Response.getwriter (). Print (name + "=" + Value + "<br/>");Wuyi                  } the     } -      Wu  -     /** About      * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response) $      */ -     protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException { -         //TODO auto-generated Method Stub -     } A  +}

Xml

1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi: schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id= "Webapp_ ID "version=" 3.0 ">3 4<servlet>5<servlet-name>helloworld</servlet-name>6<servlet-class>myweb.helloworld</servlet-class>7 8<!--configuration HelloWorld (servlet) initialization Parameters--9<init-param>Ten<param-name>name</param-name> One<param-value>xianghu</param-value> A</init-param> -<init-param> -<param-name>password</param-name> the<param-value>123456</param-value> -</init-param> -<init-param> -<param-name>charset</param-name> +<param-value>UTF-8</param-value> -</init-param> +</servlet> A<servlet-mapping> at<servlet-name>helloworld</servlet-name> -<url-pattern>/Default</url-pattern> -</servlet-mapping> -</web-app>

Java EE learning-servlet initialization parameters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.