Servlet obtains Initialization Configuration Information and built-in objects

Source: Internet
Author: User

Servlet obtains the Initialization Configuration Information:

 

The config object can be used to read configuration initialization parameters in web. xml. This object is actually an instance of ServletConfig. You can use the init () method to find the ServletConfig interface instance.

 

Read Initialization Configuration Information ------ InitParamServlet. java

[Java]
Package com. ls. servlet_01;
 
Import java. io. IOException;
Import java. io. PrintWriter;
 
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 InitParamServlet extends HttpServlet {
 
String initParam = null;
Public void init (ServletConfig config) throws ServletException {
This. initParam = config. getInitParameter ("ref ");
}
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
System. out. println ("initialization parameter:" + this. initParam );

}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doGet (request, response );
}
}
Configure initialization information in web. xml

[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Servlet>
<Servlet-name> InitParamServlet </servlet-name>
<Servlet-class> com. ls. servletd_01.InitParamServlet </servlet-class>
<Init-param>
<Param-name> ref </param-name>
<Param-value> liusheng </param-value>
</Init-param>
</Servlet>
<Servlet-mapping>
<Servlet-name> InitParamServlet </servlet-name>
<Url-pattern>/InitParamServlet </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>
 

Program running result: Print in Tomcat background

Initialization parameter: liusheng


Use <init-param> to directly configure Initialization

<Param-name> indicates the parameter name.

<Param-value> indicates the parameter content.

 

 

Note:

In Servlet, there are two initialization Methods: init () and init (ServletConfig config). If both initialization methods appear at the same time, the init (ServletConfig config) method is called.

 

Servlet retrieves other built-in objects:

1. Obtain an HttpSession instance

To obtain a session object in the servlet program, you can use the HttpServletRequest interface to perform the following operations:

Public HttpSession getSession () returns the current session

Public HttpSession getSession (boolean create) returns the current session. If no session exists, a new session object is created and a new session object is returned.

 

Get HttpSession object --- HttpSessionservlet. java

[Java]
Package com. ls. servlet_02;
 
Import java. io. IOException;
Import java. io. PrintWriter;
 
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Import javax. servlet. http. HttpSession;
 
Public class HttpSessionservlet extends HttpServlet {
 
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
HttpSession ses = request. getSession ();
System. out. println ("SESSION -->" + ses. getId ());
Ses. setAttribute ("username", "Liu Sheng ");
System. out. println ("username attribute content:" + ses. getAttribute ("username "));
}
 
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doGet (request, response );
 
}
 
}
Configure the web. xml file

[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Servlet>
<Servlet-name> HttpSession </servlet-name>
<Servlet-class> com. ls. servlet_02.HttpSessionservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> HttpSession </servlet-name>
<Url-pattern>/servlet_HttpSession </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>

Running result: Tomcat background display

 

After obtaining an HttpSession object through the getSession () method, this program outputs the Session Id and attribute settings and acquisition operations.

 

2. Retrieve the ServletContext instance

The application's built-in object is an instance of the ServletContext interface, indicating the Servlet context. To use this phenomenon in a servlet, use the methods provided by the GenericServlet class:

Public ServletContext getServletContext () Get the ServletContext object

 

Obtain the application Object ----- ServletContext. java

[Java]
Package com. ls. servlet_03;
 
Import java. io. IOException;
Import java. io. PrintWriter;
 
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Public class ServletContext extends HttpServlet {
 
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Javax. servlet. ServletContext app = super. getServletContext ();
System. out. println ("actual path:" + app. getRealPath ("/"));
}
 
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
 
This. doGet (request, response );
}
 

}

Configure the web. xml file

[Java]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Web-app version = "2.5"
Xmlns = "http://java.sun.com/xml/ns/javaee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<Servlet>
<Servlet-name> ServletContext </servlet-name>
<Servlet-class> com. ls. servlet_03.ServletContext </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> ServletContext </servlet-name>
<Url-pattern>/servlet_ServletContext </url-pattern>
</Servlet-mapping>
<Welcome-file-list>
<Welcome-file> index. jsp </welcome-file>
</Welcome-file-list>
</Web-app>

Program running result: Tomcat background output


 

This program uses the getServletContext () method to obtain the ServletContext instance and output the actual path corresponding to the virtual directory.

 

 

 

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.