⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webapp-overview.xtp

📁 RESIN 3.2 最新源码
💻 XTP
📖 第 1 页 / 共 4 页
字号:
<document> <header><product>resin</product><title>An Overview of Web Applications</title><description><p>A web application is a self-contained subtree of the web site.  It usesServlets, Filters, JSP, and the functionality provided by Resin and any otherjava code to provide a response to a client that makes an HTTP request.</p></description></header><body><localtoc/><s1 title="Web applications"><p>Each web application is part of the web server.  It has a unique name orpath that identifies it within the server.</p><p>Each web application has a corresponding url. The url begins with the partneeded to identify the server, followed by the webapp path:<code>http://server<var>/webapp-name</var></code>.  Each server has oneweb-app that is the default, it is the one that is used when no webapp-name isprovided.</p><p>Web applications are "deployed" within a web server, such as Resin.  Thesimplest way to "deploy" a new web application is to the create a subdirectoryin <code>$RESIN_HOME/webapps/<var>webapp-name</var></code>.  The specialwebapp name <code>ROOT</code> is used for the default web application. (Thereare other <a href="webapp-deploy.xtp">deployment options</a>, but for thepurposes of this discussion the one described here is used).</p><p>A web application has "web components", such as Servlets, Filters, JSP's,supporting Java source files, and supporting java libraries.</p><s2 title="Try it!"><p>You can make your own web application in a local install of Resin.  Make adirectory <code>$RESIN_HOME/webapps/test</code>.  Use the url<code>http://localhost:8080/test</code> to access the web application.</p><p>To start with, you can make a file named<code>$RESIN_HOME/test/index.jsp</code>.</p><example title="$RESIN_HOME/test/index.jsp">Hello, world!</example><p><code>index.jsp</code> is a JSP file, and is also the name of the default pageto show for a directory.  So you can use the url<code>http://localhost:8080/test/index.jsp</code> in your browser, or since<code>index.jsp</code> is the default page to show, you can use<code>http://localhost:8080/test</code>.  </p></s2><s2 title="Example web application"><p>For example, <code>www.hogwarts.com</code> has two webapplications, the <var>default</var> web application, and a web-applicationnamed "intranet".</p><p><b>Server:</b> www.hogwarts.com<br/><b>Server URL:</b> http://www.hogwarts.com<br/><br/><b>webapp:</b> default webapp<br/><b>webapp URL:</b> http://www.hogwarts.com/<br/><b>filesystem directory:</b> $RESIN_HOME/webapps/ROOT<br/><b>default jsp page:</b> $RESIN_HOME/webapps/ROOT/index.jsp<br/><br/><b>webapp:</b> intranet<br/><b>webapp URL:</b> http://www.hogwarts.com/intranet<br/><b>filesystem directory:</b> $RESIN_HOME/webapps/intranet<br/><b>default jsp page:</b> $RESIN_HOME/webapps/intranet/index.jsp<br/></p></s2> <!-- Example web application --></s1><s1 name="components" title="Components of a web application"><s2 name="servlet" title="Servlet"><p>From the Servlet Specification 2.2:</p><blockquote>A servlet is a web component, managed by a container, that generatesdynamic content. Servlets are small, platform independent Java classescompiled to an architecture neutral bytecode that can be loadeddynamically into and run by a web server. Servlets interact with webclients via a request response paradigm implemented by the servletcontainer. This request-response model is based on the behavior of theHypertext Transfer Protocol (HTTP).</blockquote><p>A Servlet is a Java class that has a method that gets called withinformation about a client request and is expected to produce somekind of result to be sent back to the client. It is just like anyother class in Java, it happens to inherit from <a href="javadoc|javax.servlet.http.HttpServlet|"/>, so Resin can call certainmethods on it when a request is made.  </p><p>A Servlet class is made available by placing the <code>.java</code> source filein the approriate sub-directory and file of <code>WEB-INF/classes</code>:</p><example title="WEB-INF/classes/example/HelloWorldServlet.java">package example;import java.io.*;import javax.servlet.http.*;import javax.servlet.*;/** * Hello world servlet.  Most servlets will extend * javax.servlet.http.HttpServlet as this one does. */public class HelloServlet extends HttpServlet {  /**   * Initialize the servlet.  Servlets should override this method   * if they need any initialization like opening pooled   * database connections.   */  public void init() throws ServletException  {  }  /**   * Implements the HTTP GET method.  The GET method is the standard   * browser method.   *   * @param request the request object, containing data from the browser   * @param repsonse the response object to send data to the browser   */  public void doGet (HttpServletRequest request,                     HttpServletResponse response)    throws ServletException, IOException  {    // Returns a writer to write to the client    PrintWriter out = response.getWriter();    // Write a string to the browser.    out.println("Hello, world!");    out.close();  }}</example><p>Entries in ithe <code>WEB-INF/web.xml</code> file tell Resin the URL thatshould invoke the Servlet:</p><example title="WEB-INF/web.xml">&lt;web-app xmlns="http://caucho.com/ns/resin"&gt;  &lt;servlet&gt;    &lt;servlet-name&gt;hello&lt;/servlet-name&gt;    &lt;servlet-class&gt;example.HelloServlet&lt;/servlet-class&gt;  &lt;/servlet&gt;  &lt;servlet-mapping&gt;    &lt;url-pattern&gt;/hello&lt;/url-pattern&gt;    &lt;servlet-name&gt;hello&lt;/servlet-name&gt;  &lt;/servlet-mapping&gt;&lt;/web-app&gt;</example><p>In a web-app named "foo" on a server named "localhost" listening on port"80", the servlet is now invoked with the URL<code>http://localhost:8080/foo/hello</code>.</p><p>More information on the usage of Servlets is available in the <a href="servlet.xtp">Servlet</a> section of the Resin documenation.</p></s2> <!-- servlet --><s2 name="jsp" title="JSP"><p>Java Server Pages are text files that contain text to be output (usuallyHTML or somesuch) and special directives, actions, scripting elements, andexpressionsthat are used to generate results dynamically.  </p><p>From the JSP 2.0 specification:</p><blockquote>JavaServer Pages technology supports scripting elements as well as actions.Actions encapsulate useful functionality in a convenient form that can bemanipulated by tools. Expressions are used to access data. Scripts can be usedto glue together this functionality in a per-page manner.</blockquote><p>With JSP the developer specifies the content mostly as the kind of thing theywant to send back to the client or browser, for example HTML.  Optionallyinterspersed with the HTML are special xml tags (directives and actions), ELexpressions, or specially marked scripting code (Java code).   The special xmltags, EL expressions and Java code are used to generate dynamic ouput.</p><s3 name="jsp-translation" title="JSP's are translated into Servlets"><p>It is helpful to understand what it is that Resin does with aJSP page.  Basically, it takes the JSP page and turns it into the Javacode for a Servlet, a process of <var>translation</var>.  </p><p>From the JSP specification: </p><blockquote>JSP pages are textual components.  They go through twophases: a translation phase, and a request phase. Translation is doneonce per page. The request phase is done once per request.</blockquote><p>The translation phase occurs when Resin takes a look at theJSP page, reads it in, and creates a Servlet. This only needs to bedone once. Now when a request from a client comes in, Resinwill call the appropriate method in the Servlet that it created from the.</p><p>During translation, Resin takes all of the code that has been speciallymarked in the JSP as java code and inserts it directly into the code for aServlet.  It takes all of the template text and makes the equivalent of printstatements to generate that ouput.</p><p>Because JSP files are translated into Servlets, JSP is an extension to JavaServlets. Everything that applies to Java Servlets also applies to JSP.  Muchinformation that is relevent to JSP programming is found in documentation aboutJava Servlets.  So you want to have the Servlet Specification around as a handyreference, as well as the JSP Specification.  Any reference to the capabilitiesand resources available to a Servlet are also available to a JSP page.  </p><p>This process is invisible to the JSP developer, all the developerneeds to do is make the JSP page and Resin will look at it andturn it into a Servlet.</p></s3> <!-- jsp-translation --><s3 name="jsp-syntax" title="The syntax of a JSP file"><p>JSP has it's own <a href="jsp.xtp">section</a> in the Resin documentation,the following is an introductory guide.</p><s4 title="template data - The text to be output"><p>Unless specially marked, the text in the JSP file will be sent exactlyas it is in the text file as part of the response. This is called<var>template data</var> in the JSP specification.</p></s4><s4 title="JSP EL and JSTL"><p>JSP EL is the JSP <var>Expression Language</var>.  It is used to evaluateexpressions that do not have side-effects (side-effects are changes toObjects). The use of EL is recognizable by it's syntax: <code>${'${'} <var>expr</var> }</code>.</p><p>JSTL is the JavaServer Pages Standard Tag Libray, a set of <var>tags</var> that areused to create dynamic output from JSP.  These tags look like regular XML tags,and are interpreted by Resin at translation time to generate java code thatperforms the desired action.</p><p>EL and JSTL are used throughout this discussion, the <a href="jsp.xtp">Resin JSP documentation</a>, the <a href="jstl.xtp">JSTL documentation</a> providemore information.</p><example title="x.jsp - Example JSP file using EL and JSTL">&lt;%@page session="false" contentType="text/html" %&gt;&lt;%@taglib uri="http://java.sun.com/jstl/core" prefix="c" %&gt;&lt;head&gt;&lt;title&gt;A simple thing&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;!-- this comment gets all the way to the browser --&gt;&lt;%-- this comment gets discarded when the JSP is translated into a Servlet --%&gt;&lt;%// some java code that makes the variable `x' available to ELpageContext.setAttribute("x",new Integer(5));%&gt;The value of x is ${'${'} x }The value of x + 2 is ${'${'} x + 2 }Is xless than 6?&lt;c:if test="${'${'} x &lt; 6 }"&gt;&lt;%@include file="y.jsp" %&gt;&lt;/c:if&gt;&lt;/body&gt;</example><example title="y.jsp - Example java code in JSP file to be included">Yes, it is true that x is less than 6, with a value of ${'${'} x }</example><results title="x.jsp output - The result of a call to x.jsp">&lt;head&gt;&lt;title&gt;A simple thing&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;!-- this comment gets all the way to the browser --&gt;The value of x is 5.The value of x + 2 is 7.Is xless than 6?Yes, it is true that x is less than 6,with a value of 5.&lt;/body&gt;</results><p>Prior to the introduction of JSTL and EL, JSP pages used the direct insertionof Java code to accomplish the same thing.  The use of JSTL and EL is muchcleaner and more maintable.</p></s4><s4 title="Including other files"><p>Often it is desirable to include the contents of another file into aJSP file. For example, sometimes there is code that you find yourselfusing over and over again. The mechanism for this is the<var>include</var> directive:</p><example>&lt;%@ include file="<var>relativeURLspec</var>"%&gt;</example><p>Using the include directive it is exactly the same as if the includedtext was in the original file.  The text is included at translationtime - when the JSP is turned into a servlet.</p></s4><s4 title="Specifying content type"><p>A JSP page can use the <var>contentType</var> attribute of the<code>page</code> directive to indicate the content type of the response it is sending. For example, <code>text/html</code> and<code>text/wml</code> are valid content types.</p><p>Since this value is part of a directive, a given page will alwaysprovide the same content type. It is also possible to dynamicallyindicate the content type using the <code>response</code> object, which isdiscussed later.</p></s4><s4 title="Comments"><p>A JSP comment is of the form:</p><example title="JSP comments">&lt;%-- anything but a closing --%&gt; ... --%&gt;</example><p>The body of the JSP content is ignored completely. JSP Comments are discardedat translation time, they do not become part of the Servlet that is used togenerate the response.  Comments are useful for documentation but also tocomment out some portions of a JSP page. JSP comments do not nest.</p><p>In order to generate comments that appear in the response to the requestingclient, the HTML and XML comment syntax is used, as follows:</p><example title="HTML comments">&lt;!-- comments ... --&gt;</example></s4><s4 title="Java code in the JSP file"><p>Java code in the JSP is marked by the special characters <code>&lt;%</code>and <code>%&gt;</code>.  To insert the value of a variable or an expression inthe output it is marked with <code>&gt;&lt;%= <var>expr</var> &amp;&gt;</code>.</p><p>Be careful not to depend on the ability of JSP to include Java code too much.JSP is best used to present a <var>view</var> of data that has already beenprepared in Servlets or other code, as discussed in <a href="#architecture">Architecture</a>.</p><p>Now that JSTL and JSP EL exist, they are preferred over the insertion of Javacode directly.</p><example title="x.jsp - Example java code in JSP file">&lt;%@page session="false" contentType="text/html" import="java.util.*, example.*%&gt;&lt;head&gt;&lt;title&gt;A simple thing&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;!-- this comment gets all the way to the browser --&gt;&lt;%-- this comment gets discarded when the JSP is translated into a Servlet --%&gt;<b>&lt;% int x = 5; // java-style comments valid here %&gt;</b>The value of x is <b>&lt;%= x %&gt;</b>.The value of x + 2 is <b>&lt;%= x + 2 %&gt;</b>.Is xless than 6?<b>&lt;% if (x &lt; 6) { %&gt;</b>&lt;%@include file="y.jsp" %&gt;<b>&lt;% } %&gt;</b>&lt;/body&gt;</example><example title="y.jsp - Example java code in JSP file to be included">Yes, it is true that x is less than 6, with a value of <b>&lt;%= x %&gt;</b>.</example>

⌨️ 快捷键说明

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