08 December, 2017

Create Servlet Project as Module

You would have seen that many times we are writing out own servlet in Liferay portal to achieve some specific use case.
In DXP, You can still cont. with such implementation and here you will see how you can write your servlet application as module.

Create liferay module type of API using developer studio or blade ci.

In bnd file:

 Bundle-Name: test-servlet
Bundle-SymbolicName: TestServlet
Bundle-Version: 1.0.0

Web-ContextPath: /test


In build.gradle:

 dependencies {
compileOnly group: "javax.servlet", name: "javax.servlet-api", version: "3.0.1"
}


In component class,

@Component(
immediate = true,
property = {
"osgi.http.whiteboard.context.path=/test",
"osgi.http.whiteboard.servlet.name=com.jignesh.MyServlet",
"osgi.http.whiteboard.servlet.pattern=/my-servlet",
},
service = Servlet.class)
public class MyServlet extends HttpServlet {
 @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
}

Just deploy the module and acces through URL : http://localhost:8080/o/test/my-servlet

Rate Me:

0 Discussion: