user626912
user626912

Reputation: 2560

How to generate different HTML based on the device that access the page?

I am using Java EE 6 with all the reference implementations and I wonder how you can generate different responses based on the device accessing the page? At the moment when I develop a JSF page I target browsers running on PC. However I want to generate another HTML structure (that is, using another JSF page) when the user browses the page with a smart phone.

Now you wonder, "Why doesn't you use CSS media queries?". Yes, I could but that will only give limited control over the layout. Could someone give me some hints to where and what to start reading about to do this?

I don't want to use Spring, I know they have something like this.

Upvotes: 1

Views: 533

Answers (3)

BalusC
BalusC

Reputation: 1109302

I don't want to use Spring, I know they have something like this.

Just reinvent it then (cough).

Let's look how they did it. According to the Spring Mobile documentation, cited below,

LiteDeviceResolver

The default DeviceResolver implementation is based on the "lite" detection algorithm implemented as part of the Wordpress Mobile Pack. This resolver only detects the presence of a mobile device and does not detect specific capabilities. No special configuration is required to enable this resolver, simply configure a default DeviceResolverHandlerInterceptor and it will be enabled for you.

it seems that they have ported this piece of PHP code to this piece of Java code. You could just do the same (be aware of license rules!). The most sensible place for this would be a servlet filter which would then send a redirect depending on the outcome of the detection.

Upvotes: 1

ipolevoy
ipolevoy

Reputation: 5518

Inspect HTTP header

user-agent

you can retrieve this using Servlet API: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getHeader%28java.lang.String%29

Upvotes: 0

Alberto
Alberto

Reputation: 525

I think you will need to look to the HTTP_USER_AGENT.

No experience with Java, but look to System.getEnv("HTTP_USER_AGENT").

It should return a string name for the user agent. You should find in the web lists of common user agents, so you can easily classify them as mobile or not mobile.

Upvotes: 0

Related Questions