Hello World Servlet Tutorial

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
Line 166: Line 166:
 
Since all servlets use the same basic structure as this tutorial, it's a good idea to understand this most
 
Since all servlets use the same basic structure as this tutorial, it's a good idea to understand this most
 
basic example completely.
 
basic example completely.
 +
 +
 +
== Cookbooks and Tutorials ==
 +
 +
* [[Building a simple listing in JSP]]: covers model 2, Servlets, JSP intro.
 +
* [[Java EE Servlet tutorial : Adding create, update and delete to the bookstore listing]]: covers more interactions.
 +
* [[Java EE Servlet tutorial : Using JSPs to create header, footer area, formatting, and basic CSS for bookstore]].
 +
* [[Java EE Servlet tutorial : Adding MySQL and JDBC to bookstore example]].
 +
* [[Java EE Servlet tutorial : Adding validation and JSP tag files to bookstore example]].
 +
* [[Java EE Servlet tutorial : Adding I18N support to bookstore example]].
 +
* [[Java EE Servlet tutorial : Load testing and health monitoring using bookstore example]].
 +
* [[Java EE Servlet tutorial : Setting up clustering and session replication]].
 +
* [[Java EE Servlet tutorial : Setting up security for bookstore example]].
 +
* [[Java EE Servlet tutorial : File uploads for bookstore example]].
 +
* [[Java EE Servlet tutorial : Using JPA for bookstore example]].
 +
* [[Java EE Servlet tutorial : Using JCache for bookstore example]].

Revision as of 00:00, 29 September 2012

Cookbook-48.png

Servlets are the pure Java solution to handle web requests. Many web 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.

REST web-applications will also use servlets. The REST output will typically use XML or JSON instead of HTML, but will use the same servlet container as for web pages.

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.

Contents

Files in the Tutorial

  • WEB-INF/classes/test/HelloServlet.java
  • WEB-INF/web.xml

There are two files that you will create for the tutorial: a Java class and an XML file. The Java class implements the servlet itself. The XML file tells the servlet engine which URLs go to the servlet.

Creating the Servlet class

Create the following servlet in WEB-INF/classes/test/HelloServlet.java with your favorite editor: eclipse, notepad, emacs, vi, or whatever.

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!");
   }
 }

Now browse the servlet at http://localhost:8080/hello. Resin will automatically compile 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.

WEB-INF/web.xml 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="test.HelloServlet"/>
 </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.
servlet defines the servlet class and gives the servlet a name
servlet-mapping translates the URL to the servlet

The xmlns="http://caucho.com/ns/resin" lets Resin validate the web.xml configuration. The validator will catch most errors in the web.xml.

installing and deploying a servlet application

To test this tutorial, you'll need to have a servlet engine like Resin installed on your computer. The Resin servlet engine download is at http://resin.caucho.com.

installing Resin servlet engine on ubuntu

On ubuntu system, you can install Resin directly with the following three steps:

 unix# add-apt-repository http://resin.caucho.com/download/debian
 unix# apt-get update
 unix# apt-get install resin

The Resin directory for your application will be in /var/www/resin/webapps/ROOT, and the default port will be port 8080, as in this tutorial.

deploying an application

You can also bundle up your application into a .war archive, which is just a zip file with a java ".war" extension that you create with the "jar" command (like zip). You'll deploy the .war archived application using Resin's command-line tool:

 unix> cd myapp; jar -cf ../ROOT.war *
 unix> resinctl deploy ROOT.war

The "deploy" will copy the ROOT.war to the webapps directory and deploy your application. Most servlet applications are bundled into .war files and deployed like this.

Review

  • The servlet is a Java class that handles HTTP web requests by implementing the Servlet interface, usually by extending HttpServlet
  • The servlet classes belong in WEB-INF/classes, or in .jar files in WEB-INF/lib
  • The servlet to URL mapping is configured in WEB-INF/web.xml
  • The servlet doGet method serves a standard HTTP request
  • The servlet text output is generated from ServletResponse.getWriter
  • Servlet applications are usually bundled in *.war files and deployed in webapps directories
  • Applications bundled as war files are deploy using resinctl deploy myapp.war

Servlet Applications

Servlets are some of the fastest web application, from web servers, to JSON/REST web services, to high-performance websocket mobile messaging applications, web sites, and even fast PHP servers written in Java. For example, the Resin web server uses servlets to implement a web server that's faster than the best C-based web servers, including Apache httpd and nginx.

Since all servlets use the same basic structure as this tutorial, it's a good idea to understand this most basic example completely.


Cookbooks and Tutorials

Personal tools
TOOLBOX
LANGUAGES