如何在jsp中检索init参数 [英] How to retrieve init parameter in jsp

查看:84
本文介绍了如何在jsp中检索init参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用jsp获取url数据,因为我使用jsp代码作为

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

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

web.xml

<servlet>
<servlet-name>IploginJSP</servlet-name>
<jsp-file>/Iplogin.jsp</jsp-file>
    <init-param>
    <param-name>url</param-name>
    <param-value>http://xxx.xxx.xxx.xxx:9000</param-value>
    </init-param>
</servlet>
<servlet-mapping>
<servlet-name>IploginJSP</servlet-name>
<url-pattern>/Iplogin</url-pattern>
</servlet-mapping>

但是我显示了NullPointerException

But i shows NullPointerException

推荐答案

我认为这将解决您的问题.

I think this will solve your issue.

请在更改web.xml文件后重新启动服务器.

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

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文件中的参数,必须在web.xml

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 <context-param> in your web.xml

将以下行放入web.xml

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

您可以通过以下方式在jsp文件中访问此参数:

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

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

Update2

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>

这篇关于如何在jsp中检索init参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆