栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在jsp中检索init参数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在jsp中检索init参数

我认为这可以解决您的问题。

更改

web.xml
文件后,请重新启动服务器。

web.xml

<servlet>    <servlet-name>GetInitParam</servlet-name>    <jsp-file>/GetInitParam.jsp</jsp-file>    <init-param>        <param-name>url</param-name>          <param-value>hello</param-value>      </init-param></servlet><servlet-mapping>      <servlet-name>GetInitParam</servlet-name>      <url-pattern>/GetInitParam.jsp</url-pattern>  </servlet-mapping>

GetInitParam.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">    <title>Example of getting init param</title></head><body><%!    String url= null;    public void jspInit() {         ServletConfig config = getServletConfig();         url= config.getInitParameter("url");    }%><%    System.out.println(url);%></body></html>

更新1

如您在评论中所问

to access parameters in all jsp files
。在你所有的JSP文件访问参数你必须设置
<context-param>
你的
web.xml

将以下几行

web.xml

<context-param><param-name>param1</param-name><param-value>hello</param-value></context-param>

您可以通过以下方式在

jsp
文件中访问此参数:

<%     String param1=application.getInitParameter("param1");     System.out.println(param1);%>

更新2

web.xml

<?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: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_3_0.xsd" id="WebApp_ID" version="3.0"><context-param><param-name>param1</param-name><param-value>hello</param-value></context-param></web-app>

JSP文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Example of getting init param</title></head><body><%    String param1=application.getInitParameter("param1");     System.out.println(param1);%></body></html>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号