filter.xtp

来自「解压在c盘」· XTP 代码 · 共 95 行

XTP
95
字号
<title>Filter</title><p/>Servlets are Java classes which service HTTP requests.  The onlyrequirement for writing a servlet is that it implements the<a href='../javadoc/javax/servlet/Servlet.html'>Servlet</a> interface.<p/>Servlets are loaded from the classpath like all Java classes.Normally, users put servlets in <var/WEB-INF/classes/> so Resin willautomatically reload them when they change.<p/><a href='jsp.xtp'>JSP</a> pages are actually implemented asServlet, but tend to be more efficient for pages with lots of text.<section title='Basic Example'><p/>The following is a complete working resin.conf to run this example.  Ifyou're using a web server like Apache, you'll need to restart Apache to usethe new configuration.  After restarting the web server, the<var/http://localhost/caucho-status/> will show <var//hello/> as adispatching URL.<p/>The <var/servlet-mapping/> tells Resin and Apache that the URL<var//hello/> should invoke the <var/hello-world/> servlet.<p/>The <var/servlet/> tells Resin that <var/hello-world/> uses the<var/test.HelloWorld/> class and that the value of the <var/greeting/>init parameter is <var/Hello World/>.<example>&lt;caucho.com>&lt;http-server app-dir='/usr/local/resin/doc'>  &lt;servlet-mapping url-pattern='/hello'                   servlet-name='hello-world'/>  &lt;servlet servlet-name='hello-world'           servlet-class='test.HelloWorld'>    &lt;init-param greeting='Hello, World'/>  &lt;/servlet>&lt;/http-server>&lt;/caucho.com></example><p/>As indicated by the <var/app-dir/>, <var/HelloWorld.java/> belongs in<def>/usr/local/resin/doc/WEB-INF/classes/test/HelloWorld.java</def><p/>Or, if you're compiling the servlet yourself, the class file belongs in<def>/usr/local/resin/doc/WEB-INF/classes/test/HelloWorld.class</def><p/>Following is the actual servlet code.  It just prints a trivialHTML page filled with the greeting specified in the resin.conf.<p/><var/init()/> and <var/destroy()/> are included mostly forillustration.  Resin will call <var/init()/> when it starts the servletand <var/destroy/> before Resin destroys it.<example>package test;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class HelloWorld extends <a href='../javadoc/javax/servlet/http/HttpServlet.html'>HttpServlet</a> {  private String greeting;  public void <a href='../javadoc/javax/servlet/GenericServlet.html#init()'>init</a>()    throws ServletException  {    greeting = <a href='../javadoc/javax/servlet/GenericServlet.html#getInitParameter(java.lang.String)'>getInitParameter</a>("greeting");  }  public void doGet(<a  href='../javadoc/javax/servlet/http/HttpServletRequest.html'>HttpServletRequest</a> request, <a  href='../javadoc/javax/servlet/http/HttpServletResponse.html'>HttpServletResponse</a> response)    throws ServletException, IOException  {    PrintWriter pw = response.getWriter();    pw.println("&lt;title>" + greeting + "&lt;/title>");    pw.println("&lt;h1>" + greeting + "&lt;/h1>");  }    public void <a href='../javadoc/javax/servlet/GenericServlet.html#destroy()'>destroy</a>()  {    // nothing to do  }}</example></section>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?