📄 index.xtp
字号:
<document> <header> <product>resin</product> <title>Dependency-injection servlet configuration</title> <type>tutorial</type> <description> <p> Resin allows servlets to be configured using dependency injection. </p> </description> <tutorial-startpage>hello</tutorial-startpage> </header> <body> <summary/><s1><p>With Resin, servlets can use Java Bean-style configuration. A "Java Bean"is just a Java class that follows a simple set of rules. Each configurationparameter <var>foo</var> has a corresponding setter method<code>setFoo</code> with a single argument for the value. Resin canlook at the class using Java's reflection and find the <code>setFoo</code>method. Because Resin can find the bean-style setters from looking at theclass, it can configure those setters in a configuration filelike the web.xml.</p></s1><s1 title="Files in this tutorial"><deftable><tr><td><viewfile-link file="WEB-INF/web.xml"/> </td><td>Configures the Servlet with bean-style init</td></tr><tr><td><viewfile-link file="WEB-INF/classes/test/HelloServlet.java"/> </td><td>The servlet implementation.</td></tr></deftable></s1><s1 title="HelloServlet"><p>The following <code>HelloServlet</code> servlet is a trivial bean-styleservlet. Instead of hardcoding the "Hello, world" string, it lets theweb.xml configure the string as <var>greeting</var>. To make that work,<code>HelloWorld</code> adds a bean-style <code>setGreeting(String)</code>jmethod.<example title="WEB-INF/classes/test/HelloServlet.java">package test;import java.io.*;import javax.servlet.http.*;import javax.servlet.*;public class HelloServlet extends HttpServlet { private String _greeting = "Default"; public void setGreeting(String greeting) { _greeting = greeting; } public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println(_greeting); out.close(); }}</example></p></s1><s1 title="Configuration"><p>The <a config-tag="servlet"/> configuration sets the <var>greeting</var> propertyinside an <a config-tag="init/servlet"/> tag. After Resin instantiates the servlet object,it looks at the configuration file for any <init> section. Resin thencalls a <code>setXXX</code> method for each <var><xxx></var> tag in<init>. In this case, Resin will call <code>setGreeting</code></p><p>Resin will perform any type conversion necessary, so you can useintegers and doubles as well as strings. After Resin calls the <code>setXXX</code> methods, it will call the <code>init(ServletConfig)</code> method.</p><p>When Resin initializes the servlet, it will makethe following calls:</p><ol><li><code>servlet = new test.HelloServlet();</code></li><li><code>servlet.setGreeting("Hello, World!");</code></li><li><code>servlet.init(servletConfig);</code></li></ol><example title="WEB-INF/web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <servlet servlet-name="hello" servlet-class="test.HelloServlet"> <init> <greeting>Hello, World!</greeting> </init> </servlet> <servlet-mapping url-pattern="/hello" servlet-name="hello"/></web-app></example></s1> </body></document>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -