Archive for January, 2010

Jan 22 2010

Running servlets inside Equinox/Eclipse

Published by under Eclipse,IT,Java

  • Creating plug-in hosting servlet
    • Create new plug-in projectequinox_servlets_1
    • Add plug-in dependencies
      • javax.servlet
      • org.eclipse.equinox.http.registry
    • Add extension: org.eclipse.equinox.http.registry.servlets
    • Configure servlet mapping in extension definition
      <?xml version="1.0" encoding="UTF-8"?>
      <?eclipse version="3.4"?>
      <plugin>
       
      	<extension point="org.eclipse.equinox.http.registry.servlets">
      		<servlet alias="/echo" class="servlets.EchoServlet" />
      	</extension>
       
      </plugin>
    • Create servlet class
      package servlets;
       
      package servlets;
       
      import java.io.IOException;
      import java.io.PrintWriter;
       
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
       
      public class EchoServlet extends HttpServlet {
       
      	private static final long serialVersionUID = 137926368689939745L;
       
      	@Override
      	protected void doGet(HttpServletRequest request,
      			HttpServletResponse response) {
       
      		String value = request.getParameter("value");
      		PrintWriter writer;
      		try {
      			writer = response.getWriter();
      			String outputText = "Echo Servlet inside Equinox/Eclipse says: "
      					+ value;
      			System.out.println(outputText);
      			writer.write(outputText);
      			writer.close();
      		} catch (IOException e) {
      			// TODO Auto-generated catch block
      			e.printStackTrace();
      		}
       
      	}
      }
  • Running plug-in hosting servlets in Eclipse IDE
    • Create new Run Configuration
    • Choose OSGi Framework
    • Deselect all preselected plug-ins from bundles list
    • Select only:
      • Newly created plug-in that’s hosting servlets
      • org.mortbay.jetty.server
      • org.eclipse.equinox.http.jetty
    • Use Add Required Bundles option
    • Save configuration and Run it
  • Using servlet
    • By default when running this configuration Jetty will start on port 80
    • Open a browser and hit URL for this example http://localhost/echo?value=Helloequinox_servlets_2equinox_servlets_3

3 responses so far