简体   繁体   中英

How to access web.xml init parameters in JSP

I have stated my init parameter in web.xml and on accessing same in init.jsp I get output as null . I am using Tomcat-8

My web.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
    <display-name>SecondWebProject</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>InitJsp</servlet-name>
        <jsp-file>/init.jsp</jsp-file>
        <init-param>
            <param-name>default_username</param-name>
            <param-value>default user</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>InitJsp</servlet-name>
        <url-pattern>/init.jsp</url-pattern>
    </servlet-mapping>
</web-app>

MY init.jsp is:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Init jsp</title>
 </head>
<body>
 Default username is: <%= config.getInitParameter("default_username") %>
<br>
 Default username is: <%=   
 getServletConfig().getInitParameter("default_username") %>
 </body>
 </html>

File structure within Eclipse is as: 文件

I am not able to find where I am doing wrong. Any help from your side will be valuable..


I tested ur code(with tomcat7) & it is running fine, Only if i Put my jsp at correct path, path which is mentioned in 提到的路径)时

Check snapshot for reference, if place jsp directly under 在此处输入图片说明

Check the directory structure WebContent / init.jsp正确的路径在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM