Jetty is a lightweight Servlet container which we suggest you use if you are going to develop servlets on your local machine, please don't run it on eniac.
If you have any questions, please post to the class newsgroup so others can benefit from the answer.
References:
http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
http://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld
Getting Jetty
- Make sure you have a Java SDK installed, you can download one here.
- Download the latest version of Jetty (6.1.11).
- Extract the zip archive to somewhere on your computer.
- Open a command line terminal and navigate to the directory where you extracted the archive.
- Run the command:
java -jar start.jar
The server will start and log messages should start appearing in the terminal. - Open a web browser and enter "http://localhost:8080" in the address bar.
- If you see a "Welcome to Jetty 6" page then the server is successfully set up. Return to the terminal and press Control-C to stop the server.
Writing a Simple Web Application
The Jetty server has a directory called "webapps" which contains a directory for every website (web application) it hosts. We will create a simple application and place it in this directory.- Make a new directory called "hello" in "webapps". Files in this directory will be accessible as "http://localhost:8080/hello".
- Make a new directory called "WEB-INF" in "hello". This directory will contain configuration information for your application.
- Make a new directory called "classes" in "WEB-INF". This directory will contain the .class files for all your servlets.
- Create a file "index.html" in "hello". This is the page you will get when you go to "http://localhost:8080/hello". The file should contain the following:
<html>
If you start the server at this point and click here you will see this new page.
<head><title>Example Web Application</title></head>
<body>
<p>This is a static document with a form in it.</p>
<form method="POST" action="servlet"/>
<input name="field" type="text" />
<input type="submit" value="Submit" />
</form>
</body>
</html> - Next we will create the handler for this form, which will be a Java Servlet. Create a file called HelloServlet.java with the following code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet
{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String q = req.getParameter("q");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("The paramter q was \"" + q + "\".");
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String field = req.getParameter("field");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<body>");
out.println("You entered \"" + field + "\" into the text box.");
out.println("</body>");
out.println("</html>");
}
} - To compile this class we need the servlet libraries, which are not part of the Java SDK, but can be found in Jetty. Use the following command (in the directory with HelloServlet.java) to compile your servlet:
javac -cp PATH_TO_JETTY/lib/servlet-api-2.5-6.1.11.jar HelloServlet.java
The "-cp" option adds the given jar file to the list of places to search for other classes your code requires. Copy the resulting HelloServlet.class file into the "hello/WEB-INF/classes" directory. - Now, we must tell Jetty that this class should be used to handle requests. The following "web.xml" file, which goes in the WEB-INF directory, describes these mappings:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Example</display-name>
<!-- Declare the existence of a servlet. -->
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<!-- Map URLs to that servlet. -->
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
</web-app> - If you restart the server you should be able to click here, enter something in the form, and the servlet will respond when you click "Submit".
If you have any questions, please post to the class newsgroup so others can benefit from the answer.
References:
http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
http://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld
No comments:
Post a Comment