- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我是 servlet 的新手。我使用 getInitParameter
("name") 在 init()
方法中获得了 DD 中的 init
参数。我在 doGet()
方法中尝试了很多方法来访问 init
参数,但它总是返回 null
。
我试过了
getServletContext().getInitParametr("name")
与
getServletConfig().getInitParametr("name")
但它们都返回 null
。我可以在 doGet()
中获取初始化参数吗?
最佳答案
答案是 - 可以。
好的,除了JB Nizet 的评论,这里还有一些建议。
1) 您是否在 Web Container 时添加了初始化参数?/Application Server正在运行吗?
引自 "Head First Servlets & JSP: Passing the Sun Certified Web Component Developer Exam" :
The servlet init parameters are read only ONCE - when the Container initializes the servlet. ...
When the Container makes a servlet, it reads the DD and creates the name/value pairs for the ServletConfig. The Container never reads the init parameters again! Once the parameters are in the ServletConfig, they won’t be read again until/unless you redeploy the servlet.
2)有两种类型的初始化参数可用。 “Head First Servlets and JSP”的另一句话(强调我的):
There are context init parameters (defined in
<context-param>
element) and servlet init parameters (defined in<init-param>
element). They are both referred to as init parameters, although defined in different elements.
Context init parameters are available to any servlet or JSP that are part of the current web app.
Servlet init parameters are available to only the servlet for which the
<init-param>
was configured.Context init parameters are defined within the
<web-app>
element.Servlet init parameters are defined within the
<servlet>
element for each specific servlet.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>Servlet testing app</display-name>
<!-- This is a context init parameter -->
<context-param>
<param-name>email</param-name>
<param-value>admin@example.com</param-value>
</context-param>
<servlet>
<servlet-name>Info Servlet</servlet-name>
<servlet-class>com.example.InfoServlet</servlet-class>
<!-- This is a servlet init parameter -->
<init-param>
<param-name>name</param-name>
<param-value>John Doe</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Info Servlet</servlet-name>
<url-pattern>/test/ShowInfo.do</url-pattern>
</servlet-mapping>
</web-app>
getServlet<b>Context</b>().getInitParameter(“email”);
getServlet<b>Config</b>().getInitParameter("name");
获取servlet 初始化参数 的另一种方法是使用抽象类GenericServlet 中定义的方法。 :
public String getInitParameter(String name);
提供此方法是为了方便。它从 servlet 的 ServletConfig 对象中获取命名参数的值。
还有Enumeration<String> getInitParameterNames()
两种方法ServletContext和 ServletConfig获取所有初始化参数。
关于java - 在 servlet 中获取初始化参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665037/
我有一个关于 Java Servlet 的问题。 假设我在 servlet 网页“somePage”上。我想登录(使用另一个 servlet,“登录”servlet)。所以我点击“somePage”上
如何将变量数组从一个 servlet 传递到另一个 servlet? 最佳答案 如果您要将当前请求传递给另一个servlet,则只需将其设置为请求属性即可。 request.setAttribute(
什么可能导致此错误? Caused by: jakarta.servlet.UnavailableException: Servlet class org.restlet.ext.servle
我的maven依赖树是这样的 我想问我maven如何解决这个冲突,有两个servlet-api.jar?提前谢谢你。 最佳答案 如果您想从 Velocity 工具中删除 servlet-api,您可以
config ProcessReg ProcessReg text HelloWorld1 public class config implements Serv
您好,我有一个关于 servlet 调用另一个 servlet 的问题 我有一个名为 Relay 的主 servlet,它将负责控制其他 servlet 用户将点击并将转发到 Relay servle
在我的 REST API 项目中,我已将 /* 映射到 RESTServlet,并且需要在同一 WAR 中托管静态内容。我更愿意将 /static/* 映射到 WAS liberty 提供的默认 se
响应映射在 Servlet 中如何工作? 每个响应如何知道清除特定 HTML 或 Handlebars 上的输出? 最佳答案 有一个 ember-java带有 Jersey REST 服务 的 git
有一个 @WebServlet(urlPatterns = "/myServlet/") .如果用户转到 myapp/myServlet/other ,我仍然希望我的 servlet 能够捕获。也就是
我正在使用 Filter 在我的所有页面中插入反点击劫持 header - 这工作正常,除了 JBoss EAP 6.3 容器管理的登录页面,这是更重要的页面之一拥有它。 登录页面根本不调用过滤器,登
我正在尝试使用 RequestDispatcher 将数据从一个 servlet 传递到另一个 servlet。这是我的调度程序代码。 String address; address = "/Java
我刚刚开始使用 Servlet,并设法让一些 Servlet 充当单独的 URL,用于填充数据库以进行一些虚拟测试。某种形式: public class Populate_ServletName ex
我是否需要同时配置app.servlet.version 和 grails.servlet.version? 前者在application.properties中,后者在BuildConfig.gro
在Myeclipse中我创建了一个名为web1的Web项目,并添加了一个名为servlet1的servlet,web.xml如下: servlet1 servlet1
这个问题在这里已经有了答案: How to run a background task in a servlet based web application? (5 个回答) 6年前关闭。 是否可以在
在我的 ManagedBean 中,如果我将范围从 @RequestScoped 更改为 @ViewScoped,我将收到以下错误堆栈。我该如何解决这个问题?不过,当我运行应用程序时,我可以在页面中看
这个问题已经有答案了: How do I execute multiple servlets in sequence? (2 个回答) 已关闭 3 年前。 我已经构建了jdbc-Servlet的代码,
我一直在徒劳地尝试实现 tomcat 9 的 jakarta servlet,而不是以前的 javax.servlet 实现(因为我的理解是 jakarta 包是前进的方向)。问题是,当我将浏览器指向
我是 Spring 新手,正在尝试使用 Maven 部署和运行我的第一个 Spring Web 应用程序。有关更多详细信息,请参阅 here (我昨天发布的一个问题): 现在,我遇到的问题是:当我尝试
我正在尝试学习如何使用 JSP。我遇到了一些毫无意义的事情,至少在我看来是这样: 当我尝试运行时: response.getWriter().println(m.getDb().printAll())
我是一名优秀的程序员,十分优秀!