📄 servlets.xtp
字号:
<s1 title="A Hello, World Servlet"><p>Servlets are the pure Java solution to handle web requests. Manyapplication will use servlets instead of JSP and others will use servletsin conjunction with JSP. Experienced JSP programmers use servletsin conjunction with JSP to create clearer and simpler applications.The servlets handle Java processing: form handing, calculation anddatabase queries. JSP formats the results.</p><p>Servlets belong in <var>doc/WEB-INF/classes</var>. On this machine,you'll put the Java source in <var><jsp:expression>application.getRealPath("/WEB-INF/classes")</jsp:expression></var>. WEB-INF/classes is the standard locationfor servlets and other Java classes. Resin automatically reloads andrecompiles servlets, beans, and classes placed in WEB-INF/classes.You should make some changes and add errors to become familiar with Resin'srecompilation and the error reporting.</p><p>Create the following servlet in<var>WEB-INF/classes/test/HelloWorld.java</var> with your favoritetext editor: notepad, emacs, vi, or whatever. (On this machine,<var><jsp:expression> application.getRealPath("/WEB-INF/classes/test/HelloWorld.java")</jsp:expression></var>)</p><example title='WEB-INF/classes/test/HelloWorld.java'>package test;import java.io.*;import javax.servlet.http.*;import javax.servlet.*;public class HelloWorld extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println("Hello, world!"); out.close(); }}</example><p>Now browse the servlet at <jsp:expression>"http://" + request.getServerName() + ":" + request.getServerPort() +request.getContextPath() + "/servlet/test.HelloWorld"</jsp:expression>. Resin will automatically compiles the servlet for you.Browsing servlets differs from page browsing becauseyou're executing a servlet class, not looking at a page.The <var>/servlet</var> URL tells Resin to look for a servlet. </p><s2 title='Configuration'><p>The default resin.conf in the distribution is already configured forthe example, so you can just start Resin as described inthe <a href="../ref/httpd.xtp">standalone server</a>. Not all of thestandard resin.conf is necessary for this example, though. You can cut itdown to the following configuration.</p><p>For development, using the <var>/servlet</var> invoker isconvenient. The invoker servlet will let you specify the servlet classin the URL itself, like /servlet/test.HelloWorld. Its configuration inthe resin.conf looks like:</p><example><caucho.com><http-server> <http port='8080'/> <host id=''> <app-dir>doc</app-dir> <web-app id='/'> <servlet-mapping url-pattern='/servlet/*' servlet-name='invoker'/> </web-app> </host></http-server></caucho.com></example><p>Resin allows a short cut for the XML configuration in the example above;you can use XML attributes in place of elements. TheServlet 2.3 standard uses only elements. So the servlet-mappingconfiguration following the Servlet 2.3 standard would look like:</p><example><servlet-mapping> <url-pattern>/servlet/*</url-pattern> <servlet-name>invoker</servlet-name></servlet-mapping></example><p>The two are entirely equivalent. For larger configurations, using attributesmakes the resin.conf or web.xml more readable.</p><deftable><tr><th>tag<th>meaning<tr><td>caucho.com<td>Top-level tag for all Caucho configuration<tr><td>http-server<td>Configuration for the servlet engine (both http and srun)<tr><td><a href="../ref/http-config.xtp#http">http</a><td>Configures astandalone HTTP server. You can listen multiple <http> or<a href="../ref/http-config.xtp#srun"><srun></a> ports at once.<tr><td>port<td>The TCP port to listen to. When actually deploying Resin,you'll use port 80, but port 8080 is a good development port.<tr><td><a href="../ref/http-config.xtp#host">host</a><td>Configures thedefault <a href="../ref/virtual-host.xtp">virtual host</a>. Many sites willonly need the default.<tr><td><a href="../ref/app-config.xtp#app-dir">app-dir</a><td>The documentdirectory for the virtual host. You can change this to the location of thereal document root.<tr><td><a href="../ref/app-config.xtp">web-app</a><td>Configures the rootweb-app for this virtual host. Most sites will only need the root web-app.<tr><td><a href="../ref/app-config.xtp#servlet-mapping">servlet-mapping</a><td>Specifies the URL to servlet mapping.<tr><td><a href="../ref/app-config.xtp#url-pattern">url-pattern</a><td>WhichURL should map to the servlet.<tr><td><a href="../ref/app-config.xtp#servlet-name">servlet-name</a><td>Theservlet to invoke for the URL. In this case, the special "invoker" whichlooks up the class from the URL.</deftable><p>This example just uses the default virtual host and root web-app. Manysites will just use the default virtual host and root web-app. For now, youcan ignore them and concentrate on the other configuration.</p><p>The example configuration is a complete working configuration. You cancreate a configuration file in resin-2.0.x/conf/test.conf to test it out.</p><example title='Starting on Windows'>resin-2.0.x> bin\httpd -conf test.conf</example><example title='Starting on Unix'>resin-2.0.x> bin/httpd.sh -conf test.conf</example><p>If you run into any trouble trying to start Resin, look atthe <a href="../ref/httpd.xtp">standalone HTTP server</a> page.</p></s2></s1>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -