www.webdeveloper.com
Results 1 to 11 of 11

Thread: Seeking a JSP Alternative to ASP Request.ServerVariables

  1. #1
    Join Date
    Oct 2008
    Location
    Bellevue, WA area
    Posts
    28

    Seeking a JSP Alternative to ASP Request.ServerVariables

    I would like to try and find an alternative solution in JSP to the following ASP code sample.

    Code:
    <%
    userAgent = Request.ServerVariables("HTTP_USER_AGENT")
    isMobile = false
    i
    f((InStr(userAgent, "AvantGo") > 0) OR (InStr(userAgent, "Windows CE") > 0) OR (InStr(userAgent, "NetFront") > 0) OR (InStr(userAgent, "BlackBerry") > 0) ) then
    isMobile = true
    end if
    if isMobile then
    response.redirect("http://www.namesandnumbers.com/mobile")
    end if
    %>
    I'm also wondering if the effort involved in doing this wouldn't just be time better spent on developing an HTML solution with a handheld CSS file fed dynamically to the page by some JavaScript browser/client detection. Probably a question for another forum here...

    As always, thanks in advance for any responses.

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    http://java.sun.com/javaee/5/docs/ap...etRequest.html

    request.getHeader("HTTP_USER_AGENT")

  3. #3
    Join Date
    Oct 2008
    Location
    Bellevue, WA area
    Posts
    28
    Well, I was able to cook something up in JSP but it doesn't seem to be catching and redirecting. I am actually trying to detect two different ways but neither redirects. In both attempts, I threw in an additional else statement to try and catch my local client Firefox 3 (Mozilla/5.0) and still all I get is the "Hello World" (at the end of the statement) printed out to the page.

    Can anyone tell me where I am going wrong here? Code included. FWIW, the first three lines are code snippets used frequently in our OpenCMS 6 environment.

    Code:
    <&#37;@ page session="false" %>
    <%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    
    <%
    String accept = request.getHeader("accept");
    String user_agent = request.getHeader("user-agent");
    String accept_charset = request.getHeader("accept-charset");
    String accept_language = request.getHeader("accept-language");
    String x_wap_profile = request.getHeader("x-wap-profile");
    String profile = request.getHeader("profile");
    %>
    <%
    if ((user_agent == ("AvantGo")) || (user_agent == ("Windows CE")) || (user_agent == ("NetFront")) || (user_agent == ("BlackBerry"))) {
    	String redirectURL = "http://www.xbox.com";
    	response.sendRedirect(redirectURL);
    } 
    else if (user_agent == ("Mozilla/5.0")) {
    %>
    <p>You are using Firefox</p>
    <%  } else { %>
    <p>Hello World</p>
    <% } %>
    
    <%
    if ((user_agent.indexOf("AvantGo") > 0) || (user_agent.indexOf("Windows CE") > 0) || (user_agent.indexOf("NetFront") > 0) || (user_agent.indexOf("BlackBerry") > 0)) {
    	String redirectURL = "http://www.xbox.com";
    	response.sendRedirect(redirectURL);
    } 
    else if (user_agent.indexOf("Mozilla/5.0") > 0) {
    %>
    <p>You are using Firefox</p>
    <% } else { %>
    <p>Hello World</p>
    <% } %>
    On a side note, I am curious about something here. Me personally, with regards to JSP, I am a noob but I manage to function with ASP and PHP. My last year spent trying to piecemeal learn JSP is just killing me, as I seem to have difficulty finding well flushed out examples of how to do some of the most basic tasks, tasks that I see being done with classic ASP or PHP, in JSP. At least in terms of web-based samples/examples of server-side JSP scripting.

    I'm actually still confused about what JSP exactly is, as many examples I see seem to include what looks to me like object-oriented Java code. Is this JSP or something different? Beans?

    At this point in this post I should probably just come right out and ask for a "beginning JSP" book recommendation.

    Does anyone else feel like they ran into this problem when attempting to learn JSP from scratch with no previous programming background?

  4. #4
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    use .equals or .equalsIgnoreCase

  5. #5
    Join Date
    Oct 2008
    Location
    Bellevue, WA area
    Posts
    28
    Quote Originally Posted by chazzy View Post
    use .equals or .equalsIgnoreCase
    Thanks Chazzy.

    However, I'm still running into a wall here.

    This is what I tried next. I also attempted the same with .equals.

    Code:
    <%
    if ((user_agent.equalsIgnoreCase("AvantGo")) || (user_agent.equalsIgnoreCase("Windows CE")) || (user_agent.equalsIgnoreCase("NetFront")) || (user_agent.equalsIgnoreCase("BlackBerry"))) {
    	String redirectURL = "http://www.xbox.com";
    	response.sendRedirect(redirectURL);
    } 
    else if (user_agent.equalsIgnoreCase("Mozilla")) {
    %>
    <p>You are using Firefox</p>
    <%  } else { %>
    <p>Hello World</p>
    <% } %>
    I am still not having any lucky making the redirect behavior work which tells me that I am not properly comparing these string values.

  6. #6
    Join Date
    Oct 2008
    Location
    Bellevue, WA area
    Posts
    28
    Okay, so I'm sure that I'm properly declaring my String user_agent variable.

    So I'm good down to at least here:
    Code:
    <%@ page session="false" %>
    <%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
    
    <%
    String user_agent = request.getHeader("user-agent");
    %>
    What I don't understand is why aren't my comparisons returning any results when a quick scriptlet of the user_agent string returns immediate results.

    Code:
    <%= user_agent%>
    Which returns
    user-agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11
    It seems as though (user_agent == ("Any Value")) will only return false.

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346

  8. #8
    Join Date
    Oct 2008
    Location
    Bellevue, WA area
    Posts
    28
    Thanks criterion, that did the trick. Can anyone point me to a specific reason as to why my attempts to declare a string variable called "user_agent" and populate it with information gleaned from request.getHeader("user-agent"); only returned false when attempting comparisons using .equals. and .equalsIgnoreCase?

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    What you spit out was a huge string that contained the characters you were looking for. .equals and .equalsIgnoreCase only check for equality. You were checking if the string contained another string. Does that make sense?

  10. #10
    Join Date
    Oct 2008
    Location
    Bellevue, WA area
    Posts
    28
    Yes, sorry, the differences between .equals and .equalsIgnoreCase versus .contains were not apparent to me. Namely the fact that both of the .equals examples I just gave, are looking for complete equality. The moment I switched it out it for .contains , the script worked flawlessly.

    By the way, here is my final working product. Simple but does the job.

    Code:
    <&#37;@ page session="false" %>
    <%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
    
    <%
    String user_agent = request.getHeader("user-agent");
    %>
    <%
    if ((user_agent.contains("AvantGo")) || (user_agent.contains("Windows CE")) || (user_agent.contains("NetFront")) || (user_agent.contains("BlackBerry"))) {
    	String redirectURL = "http://www.xbox.com/mobile";
    	response.sendRedirect(redirectURL);
    } else if ((user_agent.contains("Mozilla")) || (user_agent.contains("MSIE")) || (user_agent.contains("Opera"))) {
    	// out.println("<p>You are using Firefox</p>");
    	String redirectURL = "http://www.xbox.com";
    	response.sendRedirect(redirectURL);
    } else { 
    	out.println("<p>Hello world</p>");
    }
    %>
    Last edited by jeremybwilson; 06-15-2009 at 05:43 PM. Reason: More information to include, didn't want to double post to include this.

  11. #11
    Join Date
    Jun 2009
    Posts
    98
    I would recommend not putting logic in JSP and keeping it in java classes only.
    JSP should be for presentation only.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles