jsp页面硬编码是个很头疼的问题,工作这么多年,看到好多项目都有这种问题,这里介绍下如何防止页面硬编码吧
在Java程序中可以通过静态常量的方法来避免硬编码。如果JSP中允许使用Scriplet的话当然也可以直接使用常量了,不过现在JSP中一般不允许出现<%%>这样的代码,比如在JSTL中怎么办呢?我们不希望看到如下代码:
<c:if test=${state=='01'}> </c:if>
下面介绍用JspTag的方式来处理:
第一步、在/WEB-INF/tld/下建立my-tag.tld描述文件:
注意:根据JSP2.1规范,tld文件不能存放在/WEB-INF/classes或者/WEB-INF/lib目录中,特别不能放在/WEB-INF/tags目录或子目录中,否则会出现错误:
exception:org.apache.jasper.JasperException: PWC6180: Unable to initializeTldLocationsCache
root cause:org.apache.jasper.JasperException: PWC6336: Illegal TLD path/WEB-INF/tags/fn.tld, must not start with “/WEB-INF/tags”
在Tomcat和Weblogic中不会出现该问题,GlassFish则严格遵照规范,可将tld文件放置在/WEB-INF/tld目录。
<?xml version="1.0" encoding="UTF-8" ?> <taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd " version="2.0"> <description>my tag</description> <display-name>my tag</display-name> <tlib-version>1.0</tlib-version> <short-name>my-tag</short-name> <uri>/my-tag</uri> <tag> <!-- 在JSP中初始化MyConstant常量,以便JSTL中引用。 jsp中使用范例:<my-tag:MyConstant var="常量名称"/> 必填参数var:指定初始化MyConstant中某个变量,如果为空,则报异常 可选参数scope:变量作用范围,默认为page --> <description>MyConstant常量</description> <name>MyConstant</name> <tag-class>com.test.util.tag.MyConstantTag</tag-class> <body-content>JSP</body-content> <attribute> <description>必填参数var:指定初始化MyConstant中某个变量,为空则报异常</description> <name>var</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <description>变量作用范围(默认为page)</description> <name>scope</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
第二步、创建JAVA处理代码 com.test.util.tag.MyConstantTag
它继承了TagSupport并重写doStartTag()方法,才使得JSP中可以直接使用java中的常量:
public class MyConstantTag extends TagSupport { private static final long serialVersionUID = 3258417209566116146L; private final Log log = LogFactory.getLog(MyConstantTag.class); public String clazz = MyConstant.class.getName(); protected String scope = null; protected String var = null; public int doStartTag() throws JspException { Class c = null; int toScope = PageContext.PAGE_SCOPE; if (scope != null) { toScope = getScope(scope); } try { c = Class.forName(clazz); } catch (ClassNotFoundException cnf) { log.error("ClassNotFound - maybe a typo?"); throw new JspException(cnf.getMessage()); } try { if (var == null || var.length() == 0) { throw new JspException("常量参数var必须填写!"); } else { try { Object value = c.getField(var).get(this); pageContext.setAttribute(c.getField(var).getName(), value, toScope); } catch (NoSuchFieldException nsf) { log.error(nsf.getMessage()); throw new JspException(nsf); } } } catch (IllegalAccessException iae) { log.error("Illegal Access Exception - maybe a classloader issue?"); throw new JspException(iae); } return (SKIP_BODY); } /** * @jsp.attribute */ public void setScope(String scope) { this.scope = scope; } public String getScope() { return (this.scope); } /** * @jsp.attribute */ public void setVar(String var) { this.var = var; } public String getVar() { return (this.var); } /** * Release all allocated resources. */ public void release() { super.release(); clazz = null; scope = MyConstant.class.getName(); } private static final Map scopes = new HashMap(); static { scopes.put("page", new Integer(PageContext.PAGE_SCOPE)); scopes.put("request", new Integer(PageContext.REQUEST_SCOPE)); scopes.put("session", new Integer(PageContext.SESSION_SCOPE)); scopes.put("application", new Integer(PageContext.APPLICATION_SCOPE)); } public int getScope(String scopeName) throws JspException { Integer scope = (Integer) scopes.get(scopeName.toLowerCase()); if (scope == null) { throw new JspException("Scope '" + scopeName + "' not a valid option"); } return scope.intValue(); } }
创建常量类MyConstant:
package cn.hshb.common; public final class MyConstant { public static String STATE_01 = "1"; }
第三步、在web.xml中配置<jsp-config/>:
<jsp-config> <taglib> <taglib-uri>/my-tag</taglib-uri> <taglib-location>/WEB-INF/tld/my-tag.tld </taglib-location> </taglib> </jsp-config>
第四部、JSP头部引入:
<%@ taglib prefix="my-tag" uri="/my-tag" %> <my-tag:MyConstant var="STATE_01"/>
现在用${STATE_01}便可使用常量。
再来看下与最初硬编码的区别:
<c:if test=${state==STATE_01}> </c:if>
相关推荐
jsp中使用echarts包括echarts的饼图,柱状图,折线图等等
jsp 防止sql注入jsp 防止sql注入jsp 防止sql注入jsp 防止sql注入
jsp中使用vuejs+element-ui+vuejs第三方组件
jsp使用URL编码传递中文参数乱码问题
项目用到echars开发报表,经过两天的苦战,终于搞出来实例,jsp页面集成echarts,用到的echarts版本为echarts-1.4.1希望能够给你带来帮助。
JSP和Servlet中的几个编码的作用及原理 JSP和Servlet中的几个编码的作用及原理
jsp使用URL编码传递中文参数问题.doc 介绍了如何在JSP编写动态网页程序中将字符进行转换
最近实验室做了一个项目,使用jspsmartupload来实现的文件的上传下载,原来在windows平台运行的挺好,但是后来系统移植到linux平台上,结果在上传或下载的文件名中有中文时就会出现乱码。后来查了网上一些人的解决...
JSP用户提交页面编码,JSP用户提交页面编码
该文档提出了解决jsp开发中常见的汉字编码乱码问题的解决方案,值得参考。
用javascript硬编码方式和jquery框架方式完成ajax异步请求的示例,不包括源码。直接访问方式是,可以直接在tomcat启动后访问helloAjaxDemo.jsp或是helloJQueryAjaxDemo.jsp。比如:...
JSP汉字编码问题 JSPServlet汉字编码问题
在jsp中使用ext显示一个简单的数据表格,非常实用,以后如果要开发Ajax的页面数据表格可以直接套用,使用编码UTF-8
JSP Servlet 中的汉字编码各种问题解决方法
JSP Servlet 汉字编码问题 乱码解决方法
主要为大家详细介绍了servlet+jsp实现过滤器,防止用户未登录访问,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
jsp中的服务和编码设置
JSP与Servlet中的汉字编码问题.pdf
jsp中使用jquery实现ajax源码,tomcat上使用
主要为大家详细介绍了JSP使用过滤器防止Xss漏洞,具有一定的参考价值,感兴趣的小伙伴们可以参考一下