|
•10 Tips for Killer Website Design
•7 Sure shots ways to improve your website
•Attracting Visitors and Improving Your Search Results
•Chasing the Search Engines Algorithms
•Crash Course in Getting a 1 Google Ranking
•Design Basics
•Design Your Site for Traffic in 2005
•Designing A Website That Sells
•Googles Good Writing Content Filter
•How to Write Effective Web Copy
•How to Write Title Tags for Your Web Pages
•JSP
•JSP Actions
•JSP Directives
•JSP Scripting Elements and Variables
•JavaBeans
•Java Brewing A Tutorial
•Java How to Send Email in Java
•Java Intro to JSP
•Java JSP Browser Detection
•Java JSP Syntax
•Java JSP versus ASP
•Java MySQL Database Connection
•Java Programming Language
•Java Virtual Machine
•Java myths
•Linux Commands
•Make Money Fast With Google Adwords
•Make Money On The Internet What Is Your Niche
•Make Money Quick With Google Adsense
•PHP Redirects
•Ranked 1 at Google for Invisible Entrepreneurs But No Traffic
•Ruby Basic Input Output
•Ruby Classes Objects and Variables
•Ruby Containers Blocks and Iterators
•Ruby and the Web
•SEO One Way Web Links 5 Strategies
•SEO Success Step Two - Attracting Search Engine Attention
•The 10 Best Resources for CSS
•The 3 Best Website Traffic Sources
•The 5 Biggest Mistakes Almost All Web Designers Make
•The Click Fraud Problem
•The Five Ways You Should Be Using Keywords
•The Three Principles Of Image Optimization
•Top 5 Secrets to Making Money with Adsense
•True Paid Inclusion Programs are a Thing of the Past
•Understanding Web Logs And Why it Matters
•Index
•Temp
|
Web Hosting Tips for Webmasters -
JSP Browser Detection
JSP provides us with access objects that we can use to get
information from the incoming request and modify the resulting
response. This makes it possible for us to do things like identify
what type of browser is making the request. We can then set up a
different type of request based upon what that browser supports.
The following example determines whether the incoming browser is
Internet Explorer, Netscape, or a
WML client (like a
cell phone). Depending on the browser it finds, it returns an
appropriate greeting. Let's take a gander at an example of this at
work. (Note: I'm going to use line numbering here because it makes
the explanation of the example so much easier, but it isn't part of
the code -- it's just there for reference.)
| Browser Detection in
JSP | <%@ page
info="Greetings" errorPage="err.jsp" %>
|
This first line simply adds some arbitrary information to the
generated servlet and tells the Web server to redirect any Java
errors encountered on the page to the err.jsp page. Other
things that can be done in this page directive include importing
various Java APIs.
<%!
public final static int IE = 0;
public final static int NETSCAPE = 1;
public final static int WML = 2;
public int getBrowser(HttpServletRequest request) {
String accept = request.getHeader("ACCEPT");
if (null != accept && -1 !=
accept.indexOf("wml")) {
return WML;
}
String agent = request.getHeader("USER-AGENT");
if (null != agent && -1 !=
agent.indexOf("MSIE")) {
return IE;
}
return NETSCAPE;
}
%>
|
This section of code declares three static values that represent
the different browser types we are detecting. It also defines the
getBrowser() method that accepts the request object and uses
it to determine what browser has made the request. It does so by
looking at the ACCEPT HTTP request header to see if the
browser will accept wml. If it doesn't, it then looks at the
USER-AGENT HTTP request header to see if it contains "MSIE."
If it does, then the browser making the request is IE. Otherwise,
we assume it's a Netscape browser.
<%
switch (getBrowser(request)) {
case WML:
response.setContentType("text/vnd.wap.wml");
%>
<?xml version="1.0"?>
<wml>
<card id="index" title="Browser Greeting">
<p align="center">Hello, Wireless Fan!</p>
</card>
</wml>
<%
break;
case IE:
%>
<HTML><BODY><H1>Hello, IE
Fan!</H1></BODY></HTML>
<%
break;
default:
%>
<HTML><BODY><H1>Hello, Netscape
Fan!</H1></BODY></HTML>
<%
}
%>
|
In the last section of code, we used the declared
getBrowser() method in a Java switch statement to determine
what should be returned to the browser. For IE and Netscape
browsers, a simple HTML document is returned. For WML-accepting
browsers, the content type of the response is set to
text/vnd.wap.wml and a WML document is returned.
In these very meaningful forty-two lines of code, we have a
simple page that will serve up a pleasant greeting to three
different browsers. If all has gone well, creating this code should
have whetted your appetite for more. If you're interested in
pushing JSP even further, then read on for a list of additional
resources you can use to expand your JSP horizons.
|