How to retrieve init parameter in jsp

前端 未结 2 956
傲寒
傲寒 2021-01-02 09:00

I try to get url data using jsp, for that i used jsp code as

  String Url = pageContext.getServletConfig().getInitParameter(\"url\").toString();
 out.println         


        
2条回答
  •  孤独总比滥情好
    2021-01-02 09:28

    I think this will solve your issue.

    Please restart your server after making changes to the web.xml file.

    web.xml

    
        GetInitParam
        /GetInitParam.jsp
        
            url  
            hello  
        
    
      
        GetInitParam  
        /GetInitParam.jsp  
     
    

    GetInitParam.jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    
    
    
        
        Example of getting init param
    
    
    <%!
        String url= null;
        public void jspInit() { 
            ServletConfig config = getServletConfig(); 
            url= config.getInitParameter("url");
        }
    %>
    <%
        System.out.println(url);
    %>
    
    
    

    Update 1

    As you asked in your comments to access parameters in all jsp files. to access parameters in your all jsp files you have to set in your web.xml

    Put following lines in your web.xml

    
    param1
    hello
    
    

    you can access this parameters in following way in your jsp file:

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

    Update2

    web.xml

    
    
    
    
    param1
    hello
    
    
    

    JSP FILE

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
    
    
    
    
    Example of getting init param
    
    
    <%
        String param1=application.getInitParameter("param1"); 
        System.out.println(param1);
    %>
    
    
    

提交回复
热议问题