An enhancement for servlet route mapping

46 views
Skip to first unread message

Michael

unread,
Jan 8, 2013, 10:11:47 AM1/8/13
to google...@googlegroups.com
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!
ServletsModuleBuilder.java
ServletModule.java
ServletDefinition.java

Richard Mutezintare

unread,
Jan 8, 2013, 11:13:18 AM1/8/13
to google...@googlegroups.com
If you need to map URLs to methods you should check out http://jersey.java.net/. It can be easily integrated with guice using com.sun.jersey.guice.spi.container.servlet.GuiceContainer. 

On Tue, Jan 8, 2013 at 10:11 AM, Michael <fuh...@gmail.com> wrote:
mush

Reply all
Reply to author
Forward
0 new messages