9

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

  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

4

2 回答 2

15

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

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

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> 

获取初始化参数.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 文件中设置<context-param>的参数web.xml

将以下行放入您的web.xml

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

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

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

更新2

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>
于 2013-02-20T06:09:58.507 回答
2

人们普遍认为,在 JSP 中我们应该使用 EL 而不是 scriptlet。所以这个版本在 JSP 文件中带有 EL:

<%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %>
<HTML>
<HEAD>
   <TITLE>Using of initParam</TITLE>
</HEAD>
<BODY >
    <c:set var="LANDING_DEFAULT_URL"  value="${initParam.LANDING_DEFAULT_URL}"/>
    <p>${LANDING_DEFAULT_URL}"</p> 
</BODY>

和 web.xml:

<context-param>
  <param-name>LANDING_DEFAULT_URL</param-name>
  <param-value>https://xxx.xxx.xxx.com/</param-value>
</context-param>
于 2016-11-16T09:41:04.563 回答