- Creating plug-in hosting servlet
- Create new plug-in project
- 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(); } } }
- Create new plug-in project
- 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=Hello
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.
LikeLike
Thank you for the useful example.
LikeLike
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)
LikeLike