Servlet Tutorial
From Resin 4.0 Wiki
(Created page with " Servlets are the pure Java solution to handle web requests. Many application will use servlets instead of JSP and others will use servlets in conjunction with JSP. Experien...") |
|||
Line 7: | Line 7: | ||
database queries. JSP formats the results. | database queries. JSP formats the results. | ||
− | Servlets belong in < | + | Servlets belong in <em>WEB-INF/classes</em>. On this machine, |
− | the source is in Java source in < | + | the source is in Java source in <em>/var/www/webapps/ROOT/WEB-INF/classes</em>. |
− | + | WEB-INF/classes is the standard location | |
− | </ | + | |
for servlets and other Java classes. Resin automatically reloads and | for servlets and other Java classes. Resin automatically reloads and | ||
recompiles servlets, beans, and classes placed in WEB-INF/classes. | recompiles servlets, beans, and classes placed in WEB-INF/classes. | ||
Line 55: | Line 54: | ||
specify the class name for the servlet. | specify the class name for the servlet. | ||
− | The <servlet-mapping | + | The <servlet-mapping> tag specifies the URLs which will invoke the servlet. In our case, |
− | the | + | the ''/hello'' URL invokes the servlet. |
− | + | ||
− | + | ||
− | + | ||
=== WEB-INF/web.xml === | === WEB-INF/web.xml === | ||
Line 79: | Line 75: | ||
you can use XML attributes in place of elements. The | you can use XML attributes in place of elements. The | ||
Servlet 2.4 standard uses only elements. So the servlet-mapping | Servlet 2.4 standard uses only elements. So the servlet-mapping | ||
− | configuration following the Servlet 2.4 standard would look like: | + | configuration following the Servlet 2.4 standard would look like: |
=== WEB-INF/resin-web.xml === | === WEB-INF/resin-web.xml === | ||
Line 93: | Line 89: | ||
using attributes makes the resin.conf or web.xml more readable. | using attributes makes the resin.conf or web.xml more readable. | ||
− | < | + | <table> |
− | <tr><th>tag<th>meaning | + | <tr><th>tag</th><th>meaning</th> |
− | <tr><td>web-app<td>Web application top-level tag. | + | <tr><td>web-app</td><td>Web application top-level tag.</th> |
− | </ | + | </table> |
The ''xmlns="http://caucho.com/ns/resin"'' lets Resin | The ''xmlns="http://caucho.com/ns/resin"'' lets Resin | ||
validate the web.xml configuration. The validator will catch most | validate the web.xml configuration. The validator will catch most | ||
errors in the web.xml. | errors in the web.xml. |
Revision as of 00:00, 28 September 2012
Servlets are the pure Java solution to handle web requests. Many application will use servlets instead of JSP and others will use servlets in conjunction with JSP. Experienced JSP programmers use servlets in conjunction with JSP to create clearer and simpler applications. The servlets handle Java processing: form handing, calculation and database queries. JSP formats the results.
Servlets belong in WEB-INF/classes. On this machine, the source is in Java source in /var/www/webapps/ROOT/WEB-INF/classes. WEB-INF/classes is the standard location for servlets and other Java classes. Resin automatically reloads and recompiles servlets, beans, and classes placed in WEB-INF/classes. You should make some changes and add errors to become familiar with Resin's recompilation and the error reporting.
Create the following servlet in WEB-INF/classes/test/HelloServlet.java with your favorite editor: eclipse, notepad, emacs, vi, or whatever.
Contents |
WEB-INF/classes/test/HelloServlet.java
package test; import java.io.*; import javax.servlet.http.*; import javax.servlet.*; public class HelloServlet extends HttpServlet { public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println("Hello, world!"); out.close(); } }
Now browse the servlet at http://localhost:8080/hello. Resin will automatically compiles the servlet for you. Browsing servlets differs from page browsing because you're executing a servlet class, not looking at a page. The /hello URL is configured for the hello, world servlet below.
Configuration
Configuration for the servlet is in the WEB-INF/web.xml file.
The servlet needs to be configured and it needs to be mapped to a URL. The <servlet> tag configures the servlet. In our simple example, we just need to specify the class name for the servlet.
The <servlet-mapping> tag specifies the URLs which will invoke the servlet. In our case, the /hello URL invokes the servlet.
WEB-INF/web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>test.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app>
Resin allows a short cut for the XML configuration in the example above; you can use XML attributes in place of elements. The Servlet 2.4 standard uses only elements. So the servlet-mapping configuration following the Servlet 2.4 standard would look like:
WEB-INF/resin-web.xml
<web-app xmlns="http://caucho.com/ns/resin"> <servlet servlet-name="hello" servlet-class="test.HelloServlet"/> <servlet-mapping url-pattern="/hello" servlet-name="hello"/> </web-app>
The two are entirely equivalent. For larger configurations, using attributes makes the resin.conf or web.xml more readable.
tag | meaning |
---|---|
web-app | Web application top-level tag.</th>
</table> The xmlns="http://caucho.com/ns/resin" lets Resin validate the web.xml configuration. The validator will catch most errors in the web.xml. |