JSP基础语法之十:config对象,路径映射、init-param

本文介绍如何在web.xml中配置Servlet映射及初始化参数,并在JSP页面中获取这些参数的值。通过设置虚拟目录下的配置文件,可以实现对特定请求路径的处理,并展示初始化参数。



一:修改虚拟目录下 /WEB-INF/web.xml

增加一个servlet映射和初始化参数配置

<servlet>  
    <servlet-name>test</servlet-name>  <!-- 映射名称 -->
    <jsp-file>/configDemo/configDemo.jsp</jsp-file> 
    <init-param>  <!-- 第一个参数 -->
        <param-name>p_k1</param-name>   
        <param-value>p_v1</param-value>  
    </init-param> 
     <init-param>  <!-- 第二个参数 -->
        <param-name>p_k2</param-name>   
        <param-value>p_v2</param-value>  
    </init-param>   
</servlet>  
<servlet-mapping>  
    <servlet-name>test</servlet-name>  <!-- 映射名称 -->
    <url-pattern>/testMapping</url-pattern> <!-- //localhost/pathDemo/testMapping --> 
</servlet-mapping> 

修改完之后重启tomcat,使配置生效



二:上述<jsp-file>标签中应包含测试信息

<h2>param_key1: <%=config.getInitParameter("p_k1")%></h2>
<h2>param_key2: <%=config.getInitParameter("p_k2")%></h2>




三:直接访问“虚拟目录/testMapping" 就能获得上边初始化的两个键-值对














<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>CVTI_BHMC</display-name> <security-constraint> <display-name>Forbidden</display-name> <web-resource-collection> <web-resource-name>Http Method Contraint</web-resource-name> <url-pattern>/*</url-pattern> <http-method>PUT</http-method> <http-method>DELETE</http-method> <http-method>TRACE</http-method> <http-method>OPTIONS</http-method> </web-resource-collection> <auth-constraint /> </security-constraint> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter> <filter-name>HTMLTagFilter</filter-name> <filter-class>able.com.web.filter.HTMLTagFilter</filter-class> </filter> <filter-mapping> <filter-name>HTMLTagFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <!-- J2534 전송 테그 필터 적용 해야하는지 확인해야함 안하도록 우선 적용 --> <!-- <filter> <filter-name>HTMLTagFilter2</filter-name> <filter-class>com.cvti.core.HTMLTagFilter</filter-class> </filter> <filter-mapping> <filter-name>HTMLTagFilter2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping> --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring/context-*.xml</param-value> </context-param> <context-param> <param-name>contextInitialTaskingFactory</param-name> <param-value>able.com.web.context.ContextInitialTaskingFactory</param-value> </context-param> <session-config> <session-timeout>120</session-timeout> </session-config> <listener> <listener-class>able.com.web.context.ContextListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/springmvc/dispatcher-servlet*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>/na/getToken.action</url-pattern> </servlet-mapping> <servlet> <servlet-name>action2</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/j2534/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action2</servlet-name> <url-pattern>*.action</url-pattern> <url-pattern>*.bin</url-pattern> <url-pattern>*.BIN</url-pattern> <url-pattern>*.zip</url-pattern> </servlet-mapping> <servlet> <servlet-name>tilesDispatcher</servlet-name> <servlet-class>org.apache.tiles.web.util.TilesDispatchServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>tilesDispatcher</servlet-name> <url-pattern>*.tiles</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> <!-- <resource-ref> <res-ref-name>jndi/CVTIHME</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> --> <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> <include-prelude>/WEB-INF/jsp/include/declare.jspf</include-prelude> </jsp-property-group> </jsp-config> <error-page> <exception-type>java.lang.Throwable</exception-type> <location>/WEB-INF/jsp/com/error.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/jsp/com/error404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/jsp/com/error.jsp</location> </error-page> </web-app> 什么意思
最新发布
01-09
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd" version="6.0"> <display-name>wenote</display-name> <!-- 欢迎页面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- Servlet配置 --> <servlet> <servlet-name>HomeController</servlet-name> <servlet-class>cn.edu.lyu.fxfx.wenote.controller.HomeController</servlet-class> </servlet> <servlet-mapping> <servlet-name>HomeController</servlet-name> <url-pattern>/home/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>UserController</servlet-name> <servlet-class>cn.edu.lyu.fxfx.wenote.controller.UserController</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserController</servlet-name> <url-pattern>/user/*</url-pattern> </servlet-mapping> <servlet> <servlet-name>NoteController</servlet-name> <servlet-class>cn.edu.lyu.fxfx.wenote.controller.NoteController</servlet-class> </servlet> <servlet-mapping> <servlet-name>NoteController</servlet-name> <url-pattern>/note/*</url-pattern> </servlet-mapping> <!-- 编码过滤器 --> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>cn.edu.lyu.fxfx.wenote.filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 身份验证过滤器(修正后的映射--> <filter> <filter-name>AuthFilter</filter-name> <filter-class>cn.edu.lyu.fxfx.wenote.filter.AuthFilter</filter-class> </filter> <filter-mapping> <filter-name>AuthFilter</filter-name> <url-pattern>/home/*</url-pattern> <url-pattern>/note/*</url-pattern> </filter-mapping> <!-- 错误页面 --> <error-page> <error-code>404</error-code> <location>/WEB-INF/error/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/error/500.jsp</location> </error-page> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/WEB-INF/error/error.jsp</location> </error-page> <!-- 会话配置 --> <session-config> <session-timeout>30</session-timeout> <cookie-config> <http-only>true</http-only> <secure>false</secure> </cookie-config> </session-config> <!-- 上下文参数 --> <context-param> <param-name>uploadPath</param-name> <param-value>/upload</param-value> </context-param> <context-param> <param-name>appName</param-name> <param-value>个人笔记系统</param-value> </context-param> <!-- JSP配置 --> <jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> <default-content-type>text/html;charset=UTF-8</default-content-type> <trim-directive-whitespaces>true</trim-directive-whitespaces> </jsp-property-group> </jsp-config> </web-app> 怎么改
12-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值