jsp如何使访问每个页面时附加一段HTML?
如题。
不要用每个文件去包含。
------解决方案--------------------在web.xml中对JSP属性进行设置,
<jsp-config>
<jsp-property-group>
<display-name> url </display-name>
<url-pattern> /manager </url-pattern>
<page-encoding> UTF-8 </page-encoding>
<include-prelude> header.jsp </include-prelude>
<include-coda> foot.jsp </include-coda>
</jsp-property-group>
</jsp-config>
其中的url-pattern指定作用的目录,page-encoding指定页面编码,指定页首包含文件,include-coda指定也叫包含文件
------解决方案--------------------使用过滤器也可以实现
public class MyFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws
IOException,
ServletException {
// TODO Auto-generated method stub
arg2.doFilter(arg0, arg1);
arg1.getWriter().println( " <h1> 过滤器加的 </h1> ");//输入页脚
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
web.xml
<?xml version= "1.0 " encoding= "UTF-8 "?>
<web-app version= "2.4 "
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-app_2_4.xsd ">
<filter>
<filter-name> myfilter </filter-name>
<filter-class> test.MyFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> myfilter </filter-name>
<url-pattern> /* </url-pattern>
</filter-mapping>
</web-app>