Redirect and request dispatch
"local" Parameter--servletconfig--servlet Initialization Parameters
"global" Parameter--servletcontext--context Initialization Parameters
Web app "constructor"--servletcontextlistener
Combat: How to create a global dog?
1, Redirect and request Dispatch.
Resp.sendredirect ("http://www.cnblogs.com/xkxf/");
RequestDispatcher view = Req.getrequestdispatcher ("result.jsp"); Instantiate a request dispatcher for JSP View.forward (req, resp);//use The request dispatcher to require the container to prepare the JSP and send the request and response to the JSP
- Redirection is done by the client, and request dispatch is done by the Server.
- Redirection is to let the customer complete the work, and the request dispatch asks the server to complete the Task.
2. Display email on the Page.
obviously, It's impossible to do it by hard coding. instead, we need to get the data from the database, imagine the process, when we open a social network, and then click "profile", this time the servlet will call a Getemail method to get an email, then pass it to the view, and finally show to the User.
so, is there any need to store email in the deployment profile (dd,deployment descriptor)? Suppose you write a personal blog, edit a Web resume, it is possible, it is always better to code in HTML.
3, or need to quickly build a test environment, the previous section of the Code.
4, How to add email in dd? As shown Below:
Modify Web. XML first,
<?XML version= "1.0" encoding= "iso-8859-1"?><Web-appxmlns= "http://java.sun.com/xml/ns/j2ee"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4"> <servlet> <Servlet-name>MyTest</Servlet-name> <Servlet-class>Com.example.web.MyTest</Servlet-class> <init-param> <param-name>email</param-name> <param-value>[email Protected]</param-value> </init-param> </servlet> <servlet-mapping> <Servlet-name>MyTest</Servlet-name> <Url-pattern>/mytest.do</Url-pattern> </servlet-mapping></Web-app>
Modify the servlet,
packagecom.example.web; Importjavax.servlet.*; Importjavax.servlet.http.*; ImportJava.io.*; public classMyTestextendsHttpServlet { public voidDoget (httpservletrequest req, HttpServletResponse resp)throwsioexception, servletexception {resp.setcontenttype ("text/html"); PrintWriter out=Resp.getwriter (); out.println (getservletconfig (). getinitparameter ( "email")); //this is equivalent to calling This.getservletconfig (). who is ge...,this? This is a httpservlet object. }}
Restart Tomcat and click Submit to see
BTW: It is important to note that Getservletconfig () cannot be called in the constructor, because at this time ServletConfig does not exist at all!
package javax.servlet; import java.io.IOException; public interface Servlet { void init (servletconfig var1) Throws servletexception; ServletConfig Getservletconfig (); void service (servletrequest var1, servletresponse var2) throws servletexception, ioexception; String Getservletinfo (); void destroy ();}
Recalling the life cycle of the servlet,the life cycle of the servlet is controlled by the Web container , and the Web container calls the Servlet's constructor first, after which the Servlet's init () function is Called. Observing the above code, you can see that the parameters of init () are servletconfig. The reality is This:
- When the container initializes a servlet, it creates a unique servletconfig for the Servlet.
- The container reads "servletconfig" (that is, Init-param) from DD (that is, Web. xml) and gives these parameters to Servletconfig. Then pass the servletconfig to the Servlet's init () method.
5, sometimes, need a more "global" parameter, in the Web world, it is called the context initialization parameter , and the servlet initialization parameters, It is the effective scope of the entire web App, not just a servlet. Set the method to:
<?XML version= "1.0" encoding= "iso-8859-1"?><Web-appxmlns= "http://java.sun.com/xml/ns/j2ee"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4"> <context-param> <param-name>email</param-name> <param-value>[email protected] </param-value> </context-param> <servlet> <Servlet-name>MyTest</Servlet-name> <Servlet-class>Com.example.web.MyTest</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>MyTest</Servlet-name> <Url-pattern>/mytest.do</Url-pattern> </servlet-mapping> <!--after this, you can also have more servlets, and Context-param is valid for all servlets! -</Web-app>
Called in a manner similar to the servlet initialization parameters:
packagecom.example.web; Importjavax.servlet.*; Importjavax.servlet.http.*; ImportJava.io.*; public classMyTestextendsHttpServlet { public voidDoget (httpservletrequest req, HttpServletResponse resp)throwsioexception, servletexception {resp.setcontenttype ("text/html"); PrintWriter out=Resp.getwriter (); // out.println (getservletconfig (). getinitparameter ("email")); Out.println (getservletcontext (). getinitparameter ("email")); //just change the config to the context to be ok! }}
After restarting tomcat, clicking Submit will still get the same output page, no difference for the user! But again, there is one servletconfig per servlet, and one servletcontext for each web Application.
6, in some cases, we want to have some code before the Web application to provide services, the first to be executed, similar to the "static loading block" to load the database connection, that is, before the program formally work, to do some initialization work, for example, create a "global" database connection, or other Global object , which requires the use of Servletcontextlistener.
We know that to complete an event response, you first need to implement the interface, then register the event, and finally write the response method. so, who should register this listener? How do I use this listener?
7, a simple servletcontextlistener: how to create a global dog?
package com.example; public class Dog { private String breed; public Dog (String breed) { this.breed = breed; } public String getbreed () { return breed; }}
<?XML version= "1.0" encoding= "iso-8859-1"?><Web-appxmlns= "http://java.sun.com/xml/ns/j2ee"Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"version= "2.4"> <servlet> <Servlet-name>Listenertester</Servlet-name> <Servlet-class>Com.example.ListenerTester</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Listenertester</Servlet-name> <Url-pattern>/listenertester.do</Url-pattern> </servlet-mapping> <context-param> <param-name>breed</param-name> <param-value>great DANE&L t;/param-value> </context-param> <listener> <listener-class> com.exa Mple. Myservletcontextlistener <!--initialize an app before initializing any servlet-</listener-class> </listener>< /c2></Web-app>
packagecom.example;Importjavax.servlet.ServletContext;Importjavax.servlet.ServletContextEvent;Importjavax.servlet.ServletContextListener; public class Myservletcontextlistener ImplementsServletcontextlistener {@Override public voidcontextinitialized (servletcontextevent servletcontextevent) {servletcontext servletcontext=Servletcontextevent.getservletcontext (); String breed = Servletcontext.getinitparameter ("breed"); Dog dog = new Dog (breed); Servletcontext.setattribute ("dog" , dog); } @Override public voidcontextdestroyed (servletcontextevent servletcontextevent) {}}
packagecom.example;Importjavax.servlet.ServletException;Importjavax.servlet.http.*;ImportJava.io.*; public classListenertesterextendsHttpServlet {@Override public voidDoget (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {resp.setcontenttype ("text/html"); PrintWriter out=Resp.getwriter (); Out.println ("test context attribute set by listener <br/>"); Out.println ("<br/>"); Dog Dog = (dog) getservletcontext (). getattribute ("dog") ); Out.println ("Dog ' s breed is:" +Dog.getbreed ()); }}
"Head first Servlets and JSP" note 7: How to create a global dog?