Click to See Complete Forum and Search --> : Seeking a JSP Alternative to ASP Request.ServerVariables
jeremybwilson
06-10-2009, 05:30 PM
I would like to try and find an alternative solution in JSP to the following ASP code sample.
<%
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.
chazzy
06-10-2009, 05:51 PM
http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpServletRequest.html
request.getHeader("HTTP_USER_AGENT")
jeremybwilson
06-12-2009, 12:43 PM
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.
<%@ 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?
chazzy
06-12-2009, 07:06 PM
use .equals or .equalsIgnoreCase
jeremybwilson
06-15-2009, 11:50 AM
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.
<%
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.
jeremybwilson
06-15-2009, 03:03 PM
Okay, so I'm sure that I'm properly declaring my String user_agent variable.
So I'm good down to at least here: <%@ 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.
<%= 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.
criterion9
06-15-2009, 03:05 PM
Try contains: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
jeremybwilson
06-15-2009, 04:10 PM
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?
criterion9
06-15-2009, 04:40 PM
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?
jeremybwilson
06-15-2009, 05:40 PM
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.
<%@ 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>");
}
%>
JavaServlet
06-18-2009, 01:46 PM
I would recommend not putting logic in JSP and keeping it in java classes only.
JSP should be for presentation only.