gpt4 book ai didi

jSTL - 在 Scriptlet 和 EL 中使用 ServletContext 和 ServletConfig

转载 作者:行者123 更新时间:2023-12-01 11:05:05 24 4
gpt4 key购买 nike

我尝试运行以下几行。

<%=application.getInitParameter("tagline")%>               
<br />
<%=config.getInitParameter("admincontact")%>

${initParam.tagline}
<br />
${pageContext.servletConfig.initParameter("admincontact")}

我的 web.xml 是

<servlet>
<jsp-file>/index.jsp</jsp-file>
<init-param>
<param-name>admincontact</param-name>
<param-value>8939302763</param-value>
</init-param>
</servlet>
<context-param>
<param-name>tagline</param-name>
<param-value>Each one Plant one</param-value>

我在 ${pageContext.servletConfig.initParameter("admincontact")}和空值 <%=config.getInitParameter("admincontact")%> .

问候,约翰

最佳答案

有一个FAQ on JavaRanch关于这个。

声明如下;

How to access servlet init parameters using EL?

You cannot use the following syntax to access servlet init parameters:

${pageContext.servletConfig.initParameter.name}

You cannot get Servlet init parameters using this technique. The getInitParameter(java.lang.String name) does not fit in this case, because it requires some arguments.

According to the JavaBean spec, the property has getter & setter methods in the form

public type1 getXXX() -- WITH NO ARGUMENTS.

public void setXXX(type1)

Now consider the pageContext as bean Object. The PageContext class has methods like getServletConfig(), getRequest(), getSession() etc. You can access these like pageContext.page, pageContext.request etc in EL.

ServletContext object has a couple of methods like getMajorVersion(), getMinorVersion() with no args. so we can access these methods treating it as properties to sevletContext bean as pageContext.servletContext.majorVersion and pageContext.servletContext.minorVersion.

If you want to access Servlet init parameters using EL, then it is better to create a Map of the init parameters for the servlet and place it in the request as a scoped variable -- let's say initParameters. You would then be able to obtain any param by name with ${requestScope.initParameters.name}.

NOTE:

We can access context init parameters with ${initParam.name}

关于jSTL - 在 Scriptlet 和 EL 中使用 ServletContext 和 ServletConfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6750645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com