Search code examples
javagoogle-app-engineservletsjdomultiplatform

Implementation of a servlet that identifies requester and sends dynamic html for browser or just information for mobile/desktop app


I trying to think in a way that I can have the same methods to get and print data from a JDO database to all the 3 platforms, but with the difference that if it's a browser request it will prints in a dynamic web page generator. But I cant seem to think of a good method.

For example, if a browser asks for a post it will go to the code that prints a dynamic page + the info request. Like this

for (Texto e : results) 
        {
 print = "<table width='100%' border='2' cellspacing='2' cellpadding='2'>"
                                    + "<tr><td colspan='2'>"
                                    + results.get(0).titulo
                                    + ";</td></tr><tr><td colspan='2'>"
                                    + results.get(0).texto

But if one of the apps asks for a post it just returns:

 for (Texto e : results) 
        {
            resp.getWriter().println("Titulo:"
                + results.get(0).titulo);
            resp.getWriter().println("Nome:"
                + results.get(0).nome);

Because it doesn't need to print to a dynamic web page and gets the info from here. So i want it to return different content based on the user platform. But using the same method call.

Now I have different links to request the same info, one to the web and one to apps (desk and android). But I was thinking in having just one link for all the three. So far i though of

1º Get information in the login cookie about the platform that is doing the request and do a if in the print methods by two different types.

2º Or just send one more parameter always with that information and do a if... etc...

But I'm thinking this methods are really unsophisticated.

Anyone has any toughs about this?


Solution

  • it's usually made by checking "extension" and/or requests's Accept header. For example for url like /person/profile.html you'll render this data as HTML, or if url is /person/profile + header Accept: application/json you'll render it as JSON.

    You can use same data for both methods, but with different serialization type. It's easy to configure if you are using 'Spring Web', or similar, framework.