Hi All,
I did a little enhancement for servlet route mapping for my own project, it could map a URL into a method within a servlet (Just like struts2) rather than a whole servlet. It works locally fine for me currently.
I just changed 3 source class a little very carefully. But I am still not sure whether I coded in a correct way. Especially under multi-thread situation.
If someone could take a review of these changes will be mush appreciated ! Many thanks!
I introduced 'at' to tell Guice bind the URL at the specified method:
return Guice.createInjector(new ServletModule() {
@Override
protected void configureServlets() {
serve("/index").with(IndexServlet.class).at("index");
serve("/index/update").with(IndexServlet.class).at("update");
serve("/index/delete").with(IndexServlet.class).at("delete");
}
});
The method mush be follow :
public %AnyReturnType% %methodName%(HttpServletRequest request, HttpServletResponse response) [@Optional throws ServletException, IOException]
Here is an example of my servlet :
@Singleton
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 3594023761495055538L;
public void index(HttpServletRequest request, HttpServletResponse response) {
System.out.println("index");
}
public String update(HttpServletRequest request, HttpServletResponse response) {
System.out.println("update");
return "Hello";
}
public Object delete(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
System.out.println("delete");
return new Object();
}
}
Attached is the source code I made changes. Many thanks again!