Monday, October 18, 2010

running a simple embedded servlet container with Jetty

1. Install MySQL for Python

a. Download and install Jetty from http://dist.codehaus.org/jetty/jetty-6.1.25/jetty-6.1.25.zip

b. unzip the pkg.
[root@skcc-nebdap37 src]# yum install mysql mysql-devel

c. include the following jar in Java build path
- jetty-6.1.25.jar
- jetty-util-6.1.25.jar
- servlet-api-2.5-20081211.jar


2. Java code for running a servlet on a Jetty


a. main()

package tkang.test;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

public class JettyTest {

public static void main(String args[]) throws Exception {
Server server = new Server(8080);
Context root = new Context(server,"/",Context.SESSIONS);
ServletHolder sh = new ServletHolder(HelloWorldServlet.class);
root.addServlet(sh, "/helloworld");
server.start();
}
}



b. HelloWorld servlet code : HelloWorldServlet.java

package tkang.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("

Hello

");
}

}

No comments: