Reputation: 2976
I'm pretty new to the Java, JSP and Taglib world and one simple problem bothers me. I just want to check if the User's browser agent string contains "Opera Mini" or an old Android Version to check if it's compatible with my app.
I tested my function in Eclipse this is what it looks like:
package test.Regexp;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UserAgentRegexTest {
public static void main(String args[]){
System.out.println(isCompatible());
}
static boolean isCompatible(){
//String content = "Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";
//String content = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36";
//String content = "Opera/9.80 (J2ME/MIDP; Opera Mini/5.0.16823Mod.by.Handler/22.387; U; en) Presto/2.5.25 Version/10.54";
String content = "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.23 Mobile Safari/537.36";
String patternString = "Android (\\d.\\d)|Opera Mini";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if(matcher.find()) {
if(matcher.group(1) != null){
double version = Double.parseDouble(matcher.group(1).toString());
if(version > 4.4){
return true;
}
}
return false;
}
return true;
}
}
Now I want to use this function embedded as a jar taglib and use it in my jsp-File. I found a lot of examples for custom tag libraries but I didn't found an example for a taglib that simply has a function that returns a boolean. Is it even possible to use a taglib jar like that? I tried to create a jar taglib and I couldn't make it work.
According to this example at the bottom of this page: https://docs.oracle.com/javaee/1.4/tutorial/doc/JSPIntro7.html#wp77078 it seems to be possible. But I guess when you create a jar file for the lib the -Tag is required. I couldn't recreate the example. These is part of my taglib decriptor within the jar:
...
<uri>http://www.imaginary.url/my_taglib_name</uri>
<function>
<name>hello</name>
<function-class>my.package.Functions</function-class>
<function-signature>java.lang.String hello(java.lang.String)</function-signature>
</function>
...
This is the class:
package my.package;
public class Functions {
public static String hello(String name) {
return "Hiya, " + name + ".";
}
}
I tried it on my local xampp tomcat. I referenced the taglib in the web.xml and and created a tld-file in the tags-directory. In the jsp I referenced it with:
<%@ taglib prefix="test" uri="http://www.imaginary.url/my_taglib_name" %>
<test:hello("bla") />
This throws the following exception: No tag "hello(" defined in tag library imported with prefix "test"
.
What did I miss or what did I do wrong. I think by being able to run the example I'd be able to emebd my user-agent-if-statement easily. Or is there even a cleaner/easier way?
Upvotes: 0
Views: 869
Reputation: 2976
I found a solution: JSP:
<%@ taglib prefix="comp" uri="http://www.myurl.com/taglibs/comp" %>
<c:out value="${header['User-Agent']}" />
<c:set var="andver">
<comp:check UA="${header['User-Agent']}" />
</c:set>
<c:out value="${andver}" />
comp.tld in my tags folder:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<display-name>Check my app compatibility</display-name>
<short-name>comp</short-name>
<uri>http://www.myurl.com/taglibs/comp</uri>
</taglib>
comp.tld (taglib descriptor within jar):
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
<description>Check compatibility</description>
<tlib-version>1.0</tlib-version>
<short-name>comp</short-name>
<uri>http://www.myurl.com/taglibs/comp</uri>
<tag>
<description>
Check if browser is compatible with my app
</description>
<name>check</name>
<tag-class>my.package.comp.check</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>UA</name>
<required>true</required>
<type>java.lang.String</type>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
my java file:
package my.package.comp;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.util.regex.*;
public class check extends TagSupport
{
private String content;
public void setUA(String UA) {
this.content = UA;
}
public int doStartTag() throws JspException
{
try
{
JspWriter out = pageContext.getOut();
HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();
out.print(isCompatible());
//out.write(UA);
}
catch(Exception e)
{
throw new JspException(e.getMessage());
}
return EVAL_PAGE;
}
boolean isCompatible(){
String patternString = "Android (\\d.\\d)|Opera Mini";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(content);
if(matcher.find()) {
if(matcher.group(1) != null){
double version = Double.parseDouble(matcher.group(1).toString());
if(version > 4.4){
return true;
}
}
return false;
}
return true;
}
}
web.xml reference:
<taglib>
<taglib-uri>/tags/comp.tld</taglib-uri>
<taglib-location>http://www.myurl.com/taglibs/comp</taglib-location>
</taglib>
Well - maybe it's not the perfect solution but at least something that works for the start. :D
Upvotes: 0
Reputation: 7341
You don't need taglibs for this if you don't want to. The below is perhaps a bit ugly but it's the fastest way to do what you want, and elegant IMHO:
<% if (test.Regexp.UserAgentRegexTest.isCompatible(request)) { %>
... rest of your JSP in case of true ...
<% } else { %>
... rest of your JSP in case of false ...
<% } %>
Notice you need to pass request
to your isCompatible
method so inside your method you can fetch the actual User-Agent
header. You'll also need to make the method public. For instance:
public static boolean isCompatible(HttpServletRequest request) {
String content = request.getHeader("User-Agent");
...
Upvotes: 1