rorygames
rorygames

Reputation: 3

URL Mapping in Java AppEngine (for Python-like functionality)

First Post.

I'm a mobile game developer looking into using the Java version of AppEngine for the backend of an Andriod game. Since the game is written in Java, I figured that I'd use the Java version of AppEngine. I've some experience with the Python version of AppEngine, and am finding some difficulty in my migration to the Java version of AppEngine, specifically in relation to URL mapping.

In Python, this is what I was used to:

def main():
application = webapp.WSGIApplication(
[('/', Main),
    ('/admin', Admin),
    ('/setScore', SetScore), 
    ('/getScores', SetScores),
    ('/getUser', GetUser),
    ('/getCatelog', GetCatelog)
])

webapp.util.run_wsgi_app(application)

The WSGI application would map different URLs to the different request handlers. My question is whether there is equivalent functionality in Java or if a similar approach is even considered best practices in the Java incarnation of AppEngine.

Does Java have an alternate way to achieve this functionality, or is there some alternate paradigm for how Java Servlets handle this sort of thing?

I am aware that the web.xml file gives you the opportunity to map urls to servelets, but I'm not sure if that's the proper way.

What's the standard way that one might map URLs in the Java version of AppEngine to have different functionality triggered by different URLs?

Thanks.

Upvotes: 0

Views: 586

Answers (2)

alexey28
alexey28

Reputation: 205

My recomendation is to use Spring 3. It is cool framework that is complient with GAE. To use it with GAE for url mapping you have to:

1 Download Spring jars and copy it to WEB-INF/lib

2 Define in web.xml Dispatcher servlet

<servlet>
    <servlet-name>spring-servlet&lt;/servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class>
    <load-on-startup>1&lt;/load-on-startup>
</servlet>

3 In web.xml specify mappings for Dispatcher servlet. In example I define two url mappings: all request starting from /admin and /service will be processed in Spring

<servlet-mapping>
    &lt;servlet-name>myspring</servlet-name>
    &lt;url-pattern>/admin/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
    &lt;servlet-name>myspring</servlet-name>
    &lt;url-pattern>/service/*</url-pattern>
</servlet-mapping>

4 Create controllers

// This controller process /admin addreses:
@Controller
public class AdminController {

    @RequestMapping(value="/admin", method = RequestMethod.GET)
    public String getAdminPage(HttpServletRequest request) {
        return "/pages/admin/admin.jsp";
    }

}

// This controller process /service addreses. Method getServicePage implements some 
// RESTfull idea. You put in address id /service/123 and it return a page for this
// service
@Controller
public class ServiceController {

    @Autowired
    private ServiceDao serviceDao;

    @RequestMapping(value="/service", method = RequestMethod.GET)
    public String getServicesListPage(HttpServletRequest request) {
        return "/pages/servise/service-list.jsp";
    }

    @RequestMapping(value="/service/{serviceId}", method = RequestMethod.GET)
    public String getServicePage(HttpServletRequest request,
        @PathVariable(value = "serviceId") int serviceId) {
        Service service = serviceDao.get(serviceId);
        request.setAttribute("service", service);
        return "/pages/servise/service.jsp";
    }

}

5 Create Spring context file and specify scan path to get into context your controller

Name it according to Dispatcher servlet name and set scan package acoording to your controller. Think you can handle it without examples.

Upvotes: 1

Moritz Petersen
Moritz Petersen

Reputation: 13047

Mapping URLs to servlets in web.xml is pretty basic, but might get the job done for you. However, depending on your actual intention, it might be quite cumbersome.

Are you writing a "traditional" web application or a REST service? Then, there are a myriad of frameworks you could choose from. For appengine I learned to use lightweight frameworks, such as Stripes, which I have made some good experience with.

For REST, I recommend Jersey, but there are other options out there, too.

So, in Stripes, for example, you use ActionBeans which are then mapped to URLs. Here is a simple example:

@UrlBinding( "/main/score/{$event}" )
public class ScoreActionBean extends AbstractActionBean {
    public Resolution getScores() {
        ...
    }

    public Resolution setScore() {
        ...
    }
}

Edit: since you are writing a game backend, you might be more interested in a REST service? There are some good tutorials out there, but to give you an impression, it's quite similar to the example above:

@Path("/hello")
public class Hello {

    // This method is called if TEXT_PLAIN is request
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey";
    }

    ...
}

Upvotes: 1

Related Questions