cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kaydee <kays...@dattatri.com>
Subject Problem testing a simple ServletHandler with Jetty in CXF
Date Wed, 10 Jun 2009 01:54:30 GMT

I just modified one of the CXF samples as below

1. Start the server and get the JettyHttpEngine instance
2. Create a ServletHandler object and specify /sample path to a local
TestServlet
3. Invoke the /sample URL from a browser

My goal is to add a simple HttpServlet to the embedded Jetty engine with CXF
and to invoke that servlet. 

No matter what, I get  a response from Jetty of 404 NOT FOUND. Sure enough,
the request reaches the Jetty instance but my TestServlet is not being
invoked. I couldn't see any debug output either.

Much appreciate if someone could shed some light on the code below

------- Server3.java-----------
package demo.hw.server;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.*;
import org.apache.cxf.interceptor.*;
import javax.xml.ws.Endpoint;
import org.apache.cxf.transport.http_jetty.*;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.handler.*;
import org.mortbay.jetty.*;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URL;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.mortbay.log.StdErrLog;


public class Server3 {

    protected Server3() throws Exception {
        // START SNIPPET: publish


        StdErrLog stdlog = new StdErrLog();
        stdlog.setDebugEnabled(true);

        HelloWorldImpl implementor = new HelloWorldImpl();
        JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
        svrFactory.setServiceClass(HelloWorld.class);
        //svrFactory.setAddress("http://localhost:9000/helloWorld");
        svrFactory.setAddress("http://172.19.35.144:9000/helloWorld");
        svrFactory.setServiceBean(implementor);
        svrFactory.getInInterceptors().add(new LoggingInInterceptor());
        svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
        System.out.println("Creating the server instance...");
        org.apache.cxf.endpoint.Server svr = svrFactory.create();

        JettyHTTPDestination jettyDest =
(JettyHTTPDestination)svr.getDestination();
        System.out.println("Got the Jetty Destination...");
        JettyHTTPServerEngine jettyEngine = (JettyHTTPServerEngine) 
                                                jettyDest.getEngine();
        System.out.println("Got the JettyHTTPEngine...");
        
        ServletHandler handler=new ServletHandler();
          
        java.util.List<org.mortbay.jetty.Handler> lh = new
java.util.ArrayList<org.mortbay.jetty.Handler>();
        lh.add(handler);
        jettyEngine.setHandlers(lh);
        System.out.println("Added Handler " + handler);
        ServletHolder sh =
handler.addServletWithMapping("demo.hw.server.TestServlet", "/sample");
        System.out.println("ServletHolder is " + sh);

         java.util.List<org.mortbay.jetty.Handler> handlers =
jettyEngine.getHandlers();
        if (handlers != null)
           System.out.println("Handler= " + handlers.size());
        else 
           System.out.println("Handlers is null ");
          
      }
      
    public static void main(String args[]) throws Exception {
        new Server3();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}
-------------TestServlet.java------------
package demo.hw.server;

import java.util.Date;
import java.io.IOException;

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

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.servlet.ServletHandler;


public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
{       
        Date d = new Date(); 
        System.out.println("TestServlet invoked...");
             response.setContentType("text/html");
           response.setStatus(HttpServletResponse.SC_OK);
         response.getWriter().println("<h1>TestHello Servlet " +
                          d.toString() + " </h1>");

       }
    }



-- 
View this message in context: http://www.nabble.com/Problem-testing-a-simple-ServletHandler-with-Jetty-in-CXF-tp23954297p23954297.html
Sent from the cxf-user mailing list archive at Nabble.com.


Mime
View raw message