martedì 24 novembre 2009

JSP Redirecting Page based on client device

By user agent you can determine
client type.

<pre name=code class=java>
<%!
public String[] mobileTags = { "cellphone",
"iemobile",
"midp",
"mini",
"mmp",
"mobile",
"nokia",
"pda",
"phone",
"pocket",
"ppc",
"psp",
"symbian",
"up.browser",
"up.link",
"wap",
"windows ce" };

public Boolean isMobile( String browserInfo )
{
for ( int n=0; n<mobileTags.length; n++ )
{
if ( browserInfo.toLowerCase().contains(
mobileTags[n].toLowerCase() ) )
{
return true;
}
}
return false;
}
%>
<%
String bInfo = (String)request.getHeader( "User-Agent" );

if ( isMobile( bInfo ) )
{
response.sendRedirect( "mobile.jsp" );
}
else
{
response.sendRedirect( "home.jsp" );
}
%>
</pre>

Nessun commento:

Posta un commento