Jan 22 2010

Running servlets inside Equinox/Eclipse

Published by at 6:57 pm 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

3 Responses to “Running servlets inside Equinox/Eclipse”

  1. Lars Vogelon 26 Jan 2010 at 10:17 pm

    Thanks. This is a nice tip. As far as I know the servlet specification supported in the current version of Eclipse is pretty old (org.mortbay.jetty.server). If that is correct I hope we see improved servlet support in Eclipse 3.6.

  2. timokkon 15 May 2010 at 9:40 am

    Thank you for the useful example.

  3. Na_Don 06 Aug 2010 at 8:25 am

    hello,

    A very neat tutorial indeed .I tried the same example but I am getting the following error:

    java.net.SocketException: Permission denied
    at sun.nio.ch.Net.bind(Native Method)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:137)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:77)
    at org.mortbay.jetty.nio.SelectChannelConnector.open(SelectChannelConnector.java:216)

Trackback URI | Comments RSS

Leave a Reply

You must be logged in to post a comment.