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