Servlet configuration and resource parameters, servlet configuration parameters

Source: Internet
Author: User

Servlet configuration and resource parameters, servlet configuration parameters


Import java. io. *; import javax. servlet. *; import javax. servlet. http. *; // inherit from the HttpServlet class public class HelloWorld extends HttpServlet {private String message; public void init () throws ServletException {// execute the required initialization message = "Hello World ";} // This method is used to process the GET request public void doGet (HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException {// set the response content type response. setContentType ("text/html"); // The actual logic is here PrintWriter out = response. getWriter (); out. println ("
 

 

Configuration in web. xml

<Context-param> <! -- Global parameter --> <param-name> uploadfolder </param-name> <param-value> attachment </param-value> </context-param> <servlet -name> HelloWorld </servlet-name> <servlet-class> HelloWorld </servlet-class> </servlet> <init-param> <! -- Only this Servlet can be read, not a global parameter --> <param-name> message </param-name> <param-value> welcom toservlet </param-value> </init-param> <servlet-mapping> <servlet-name> HelloWorld </servlet-name> <url-pattern>/HelloWorld </url-pattern> </servlet-mapping>

<Url-pattern> you can configure multiple mappings at the same time, or use wildcards "*" and "_".

In ServletGetServletConfig. getInitParameter (Stringname)To obtain the initialization parameter in the configuration. (If the context initialization parameter is not the same as the Servlet initialization parameter, getServletContext. getInitParameter (String name) can also obtain this parameter, but this parameter is not recommended)

To obtain the context initialization parameter, useGetServletConfig. getServletContext. getInitParameter (String name)

The above two methods both read the initial parameters when writing the program code. After java EE 5, resource injection (Resource InjectionThe Servlet does not need to actively read resources. When Tomcat is started, the information configured in web. xml is actively injected into the Servlet.

Example:

 

Private @ Resource (name = "hello") String hello; // write @ Resource (name = "I") // write private int I in two rows;

 

Use the <env-entry> label in web. xml to configure resources.

<env-entry>      <env-entry-name>hello</env-entry-name>      <env-entry-type>java.lang.String</env-entry-type>      <env-entry-value>welcom toservlet</env-entry-value><env-entry> <env-entry>      <env-entry-name>i</env-entry-name>      <env-entry-type>java.lang.Integer</env-entry-type>      <env-entry-value>66</env-entry-value><env-entry>
 

The principle of resource injection isJNDI(Java Naming and Directory Interface, Java Naming and Directory Interface). If you do not use the @ Resource annotation, you can obtain these three resources by searching for JNDI.

Context ctx =new InitialContext();String message =(String)ctx.lookup("message");Integer i =(Integer)ctx.lookup("i");
 

Servlet can not only comment variables of the String or Integer type, but also inject custom Java Beans, data sources, and other complex types of variables.

 

Servlet is thread insecure


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.