SERVLET-02 Servlet Related Classes

Source: Internet
Author: User

Servletconfig/servletcontext/servletrequest/servletresponse

I. ServletConfig: Encapsulates the configuration information for the servlet and can obtain the ServletContext object.

1 Getting initialization parameters

A. Configuring initialization parameters

<servlet>      <servlet-name>helloServlet</servlet-name>      <servlet-class> Com.bwr.start.helloservlet</servlet-class>
<!--Init-param nodes must be load-on-startup before-- <init-param> <param-name>encode</ param-name> <param-value>utf-8</param-value> </init-param>
<init-param>
<param-name>encode</param-name>
<param-value>utf-8</param-value>
</init-param>

</servlet>

B. Getting initialization parameters

Getinitparamter (String name) gets the initialization parameters of the servlet-specified name

Getinitparameternames () Gets the enumeration value of all initialization parameter names in the servlet

       public void init (ServletConfig config) throws servletexception {        System.out.println ("init ...");        String name = Config.getinitparameter ("name");        System.out.println ("Name:" + name);                enumeration<string> params = config.getinitparameternames ();        while (params.hasmoreelements ()) {String param = params.nextelement (); String value = config.getinitparameter (param); System.out.println (">>" + param + ":" +         

Background output when servlet loads:

name:tom>> encode:utf-8>> name:tom

2 getting the servlet name Getservletname ()

3 Get servlet context Getservletcontext ()

Two. ServletContext

The 1.Servlet engine creates a corresponding ServletContext object for each Web application, which can be obtained through the ServletConfig Getservletcontext () method in the servlet.

2. Because all servlets in a Web application share the same ServletContext object, they are also called application objects (Web application objects)

3. You can get information about all aspects of your current web app via ServletContext:

① get current web App initialization parameters

Configuration: Configured under the Web-app tab of the Web. xml file

  <context-param>      <param-name>web-name</param-name>      <param-value>hello</ param-value>  </context-param>  <context-param>      <param-name>version</ param-name>      <param-value>2.2.1</param-value>  </context-param>

Fetch: Obtained by ServletContext Getinitparamter (String name) and Getinitparamternames ().

② gets the absolute path of a file for the current web App

A. The file must be in the Web application, i.e. under the webcontent of the project; the absolute path to get is the absolute path to the file where the application was deployed

B. Using method Servletcontext.getrealpath ("/note.txt"), "/" represents webcontent in the Eclipse project structure

③ get the name of the current web App

Servletcontext.getcontextpath ()//"/JAVAWEB01"

④ gets the input stream for a file of the current Web application

Servletcontext.getresourceasstream (String path); The "/" in front of path is the root directory of the Web App

A. Under the Eclipse project, there are documents under SRC db.properties

ServletContext (). getResourceAsStream ("/web-inf/classes/db.properties");

getclass (). getClassLoader (). getResourceAsStream ("Db.properties");

B. Under the Eclipse Project WebContent there are files Note.txt

      ServletContext (). getResourceAsStream ("/note.txt");

⑤ and attribute-related methods

Three. ServletRequest

Four. Servletresponse

SERVLET-02 Servlet Related Classes

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.