【问题标题】:Getting null while accessing initParameter in jsp在jsp中访问initParameter时获取null
【发布时间】:2015-01-10 13:29:35
【问题描述】:

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

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

我的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 参数时得到空值。请提出建议。

【问题讨论】:

  • 它正在和我一起工作,你能检查你的目录结构吗?

标签: jsp web.xml init-parameters


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    你可以得到一个空值的情况是当你写以下内容时:

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

    在不是 config.jsp 的 JSP 中。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-12
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 2016-08-16
      相关资源
      最近更新 更多