web.xml配置中信息的一些含义


java配置文件web.xml中参数设置及含义

1.ServletConfig的<init-param>配置

<servlet>

<init-param>

<param-name>username</param-name>

<param-value>admin</param-value>

</init-param>

</servlet>

如上所述,在某个Servlet标签内,仅在这个Servlet内有效获取。

调用方法:ServletConfig.getInitParameter("username");

获取方法:

public void init()

{

ServletConfig config = getServletConfig();

String userName = config.getInitParameter("username");

}

2.ServletContext的<context-param>配置

<context-param>

<para-name>path</para-name>

<para-value>/WEB-INF/config.properties</para-value>

</context-param>

在Servlet标签外获取的应用是上下文参数,可以被该应用下任何一个资源使用。

调用方法:ServletContext.getInitParameter("username");

获取方法:

public void init()

{

ServletContext cont= getServletContext();

String userName = cont.getInitParameter("username");

}

context-param和init-param的区别:

第一种参数在servlet里面可以通过getServletContext().getInitParameter("path")得到

第二种参数只能在servlet的init()方法中通过this.getInitParameter("username")取得

init-param属于一个servlet所有,context-param属于整个应用程序所有 ,不仅是在servlet中可以得到,jsp文件中也可以得到.

在jsp中config就相当于这里的servletContext:<%String title1=application.getInitParameter("title1");%>或者

<%=config.getServletContext().getInitParameter("...") %>或者<%=this.getServletContext().getInitParameter("...") %>

action中ServletActionContext.getServletContext().getInitParameter("...").

3.配置默认项目打开首页

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

这种情况下,在地址栏只需要输入到项目名则会默认打开index.jsp。相应的,如果你的首页是login.jsp,则可以手动改成login.jsp,这样在访问项目的时候就可以方便的访问了。

4.设置session的有效时间。

<session-config>

<session-timeout>40</session-timeout>

</session-config>

设置session的有效时间,默认为30分钟。单位是分钟,如上例,则有效时间为40分钟,无需调用。


5.Servlet跳转配置

<!-- 用户登录 Servlet跳转 -->

<servlet>

<description>This is the description of my J2EE component</description>

<display-name>This is the display name of my J2EE component</display-name>

<servlet-name>UserLogin</servlet-name>

<servlet-class>com.ufnovo.www.action.LoginAction</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>UserLogin</servlet-name>

<url-pattern>/servlet/login</url-pattern>

</servlet-mapping>

6. 404和500页面配置

<!-- 404页面 -->

<error-page>

<error-code>404</error-code>

<location>/userInfoCenter/login.jsp</location>

</error-page>

<!-- 500页面 -->

<error-page>

<error-code>500</error-code>

<location>/userInfoCenter/login.jsp</location>

</error-page>


7. jsp中取得xml文件中设置参数

<?xml version="1.0" encoding="UTF-8"?>

<web-app>

<!--设置首页标题-->

<context-param>

<param-name>title1</param-name>

<param-value>中晟易通—用户登录</param-value>

</context-param>

</web-app>

application是内置对象,JSP页面中可以这样取得

<%=application.getInitParameter("title1") %>

或者

<%

String title1=application.getInitParameter("title1");

%>

<%=title1%>

或者像下面那样取值:

<%

//第一种

ServletContext sc=this.getServletContext();

String log4jConfigLocation=sc.getInitParameter("contextConfigLocation");

out.println("****log4jConfigLocation="+log4jConfigLocation);

out.println("*********************************************");

out.print("<br>");

//第二种

ServletContext application1=getServletContext ();

Enumeration enema=application1.getInitParameterNames();

while(enema.hasMoreElements()){

String name=(String)enema.nextElement();

String value=application1.getInitParameter(name);

out.println(name+",");

out.println(value);

out.print("<br>");

}

%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值