[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Browser Detection using Java

Posted on 2004-04-30
11
Medium Priority
?
2,610 Views
Last Modified: 2013-11-24
Hi,
I am very very new to Java programming and was hoping someone could help me. I am sure what i am doing is really simple but at the moment it is doing my head in.

I need to be able to detect which browser and version is being used to view my jsp.

I have tried a couple of browser sniff examples using JSPs and Java Beans but they did not work.(have no idea why)

I am limited to using TOMCAT and J2SE and cannot use J2EE. Would prefer examples using Servlets or just JSPs, so any working examples or code snipets would be great.

Thanks for your help
0
Comment
Question by:bcops
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 4

Expert Comment

by:91mustang
ID: 10960991
<% // get User Agent header from request
String usrAgent = request.getHeader("User-Agent").toLowerCase();
if (usrAgent == null) usrAgent = "";
// platform
boolean is_mac68k = ((usrAgent.indexOf("68k") > -1) || (usrAgent.indexOf("68000") > -1));
boolean is_macppc = ((usrAgent.indexOf("ppc") > -1) || (usrAgent.indexOf("powerpc") > -1));
boolean is_mac = ((usrAgent.indexOf("mac") > -1) || is_mac68k || is_macppc);
// browser
boolean is_msie = (usrAgent.indexOf("msie") > -1);
double version = 0.0;
// get MSIE version
if (is_msie) {
  try {
    String tempStr = usrAgent.substring(usrAgent.indexOf("msie"),usrAgent.length());
    version = Double.parseDouble(tempStr.substring(4,tempStr.indexOf(";")));
  } catch (NumberFormatException nfe) {
       version = 0.0;
  }
       catch (StringIndexOutOfBoundsException siobe) {
     version = 0.0;
  }
}
boolean is_msie4up  = (is_msie && (version >= 4.00));
// decide whether to use JavaScript or Not
Boolean useJS = new Boolean(!is_mac && is_msie4up);
request.setAttribute("useJS", useJS);
%>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Expert Comment

by:91mustang
ID: 10961014
thats last one gets OS and browser...
0
 
LVL 1

Accepted Solution

by:
xjiang007 earned 1500 total points
ID: 10961041
something like this in your jsp, or put the code inside the filter or servlet, it depends on what you want to do once you detect the browser.
<%
               String useragent = null;
      boolean netEnabled = false;
      boolean ie = false;
      boolean ns6 = false;
      boolean ns4 = false;
      boolean ie5up = false;
      boolean ie3 = false;
      boolean ie4 = false;
      boolean ie5 = false;
      useragent = request.getHeader("User-Agent");
      String user = useragent.toLowerCase();
      if(user.indexOf("msie") != -1)
      {
            ie = true;
            if(user.indexOf("msie 5.0") != -1)
            {
                  ie5 = true;
            }
            if(user.indexOf("msie 4") != -1)
            {
                  ie4 = true;
            }
            if(user.indexOf("msie 3") != -1)
            {
                  ie3 = true;
            }
            if(ie && !ie3 && !ie4)
            {
                  ie5up = true;
            }
      }
      else if(user.indexOf("netscape6") != -1)
      {
            ns6 = true;
      }
      else if(user.indexOf("mozilla") != -1)
      {
            ns4 = true;
      }

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

Author Comment

by:bcops
ID: 10961152
xjiang007 you are saying that i can just put that code into a jsp or a servlet?

Would prefer to use a servlet, so how would it work in a servlet?
thanks
S
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10961288
try this..simple code...

<logic:match header="User-Agent" value="IE">
  <logic:match header="User-Agent" value="6.0">
    <p>Microsoft Internet Explorer 6.0</p>
  </logic:match>
</logic:match>
<logic:match header="User-Agent" value="Opera">
  <p>Opera</p>
</logic:match>
<logic:match header="User-Agent" value="Mozilla">
  <p>Mozilla</p>
</logic:match>

R.K
0
 

Author Comment

by:bcops
ID: 10961317
thanks RK but where do i put that code and could you possibly explain how it works? What is that using?
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10961438
The Jakarta Struts <logic:match> tag can be used to detect the version of the web browser being used to view the page. The tag performs a case-insensitive substring search using the value specified. This enables the User-Agent string in the HTTP header to be queried for the browser version.

The code given below displays “Microsoft Internet Explorer 6.0” if running IE 6.0, or “Opera” or “Mozilla” if running any version of those browsers.

R.K
0
 
LVL 1

Expert Comment

by:isoplane
ID: 10966598
The requesting browser and all sorts of info can be found in the HTTP request header. Check out this link. It shouls tell you all you need in a much nicer format than we can achieve here.

http://www.stardeveloper.com/articles/display.html?article=2001072201&page=1

Cheers.
0
 

Author Comment

by:bcops
ID: 10994635
thanks all!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question