[thelist] jsp/java browser detect

evolt at mccullough-net.com evolt at mccullough-net.com
Mon Oct 18 11:08:57 CDT 2004


What I ended up doing was writting a bean.

##############################################################

package com.dynamic.logic;

import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;

public final class DetectBrowser implements Serializable {

	private HttpServletRequest request = null;
	private String useragent = null;
	private boolean netEnabled = false;
	private boolean ie = false;
	private boolean ns6 = false;
	private boolean ns7 = false;
	private boolean op = false;
	private boolean moz = false;
	private boolean ns4 = false;
	

	public void setRequest(HttpServletRequest req) {
		request = req;
		useragent = request.getHeader("User-Agent");
		String user = useragent.toLowerCase();
		if(user.indexOf("msie") != -1) {
			ie = true;
		} else if(user.indexOf("netscape6") != -1) {
			ns6 = true;
		} else if(user.indexOf("netscape/7") != -1) {
			ns7 = true;
		} else if(user.indexOf("opera") != -1) {
			op = true;
		} else if(user.indexOf("gecko/2004") != -1) {
			moz  = true;
		} else if(user.indexOf("mozilla/4.7") != -1) {
			ns4 = true;
		}

		if(user.indexOf(".net clr") != -1)
			netEnabled = true;
	}

	public String getUseragent() {
		return useragent;
	}

	public boolean isNetEnabled() {
		return netEnabled;
	}

	public boolean isIE() {
		return ie;
	}
	
	public boolean isNS7() {
		return ns7;
	}
	
	public boolean isNS6() {
		return ns6;
	}
	
	public boolean isOP() {
		return op;
	}
	
	public boolean isMOZ() {
		return moz;
	}
	
	public boolean isNS4() {
		return ns4;
	}
}

##############################################################

to detect the user-agent string for the various browsers, Mozilla, Firefox and
Netscape have very similar strings, and since I wanted to allow Mozilla and
Firefox, it took me more trial and error to get it right.

Quoting Hassan Schroeder <hassan at webtuitive.com>:

> evolt at mccullough-net.com wrote:
> > What I have started to do is in my jsp page is to put in some browser
> detection,
> > I need to redirect based on what browser you have.  So for Netscape 4.7,
> > generation 6, earlier IE versions and so on.
> > 
> > CODE: #################################################################
> 
> <snip/>
> 
> > Help :)
> 
> with what, exactly?
> 
> -- 
> Hassan Schroeder ----------------------------- hassan at webtuitive.com
> Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com
> 
>                            dream.  code.
> 
> 
> -- 
> 
> * * Please support the community that supports you.  * *
> http://evolt.org/help_support_evolt/
> 
> For unsubscribe and other options, including the Tip Harvester 
> and archives of thelist go to: http://lists.evolt.org 
> Workers of the Web, evolt ! 
> 




----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.



More information about the thelist mailing list