在jsp中访问initParameter时获取null

时间:2014-11-13 11:50:49

标签: jsp web.xml init-parameters

我正在尝试访问我的jsp中的initParameter

当我尝试访问initparameter时,它会显示为空。

我的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>Insert title here</title>
</head>
<body>
<%   
String user=config.getInitParameter("User");  
out.print("Hello "+user);  
%>  
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
<display-name>
Learning</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>config</servlet-name>
<jsp-file>/config.jsp</jsp-file>
<init-param>
  <param-name>User</param-name>
  <param-value>Dummy</param-value>
</init-param>
</servlet>
<servlet-mapping>
  <servlet-name>config</servlet-name>
<url-pattern>/config.jsp</url-pattern>
</servlet-mapping>
</web-app>

访问init参数时我得到null。请建议。

2 个答案:

答案 0 :(得分:3)

如果你的JSP文件名是config.jsp,它应该可以工作。

您可以在以下情况下获得空值:

<% String user=config.getInitParameter("User"); 
out.print("Hello "+user);%>

在不是 config.jsp 的JSP中。

所以请交叉检查您的JSP名称,它应该直接在您项目的WebContent下。

答案 1 :(得分:0)

web.xml中提到的jsp文件的名称应与WebContent中的实际jsp文件名匹配 例 假设您在web.xml中有/intipage.jsp,则需要在WebContent中插入initpage.jsp。