gpt4 book ai didi

java - request.getServletContext() 和 getServletContext() 的区别

转载 作者:行者123 更新时间:2023-11-29 09:52:47 24 4
gpt4 key购买 nike

<分区>

我有 SampleServlet 类,在其中我重写了 doGet() 方法,如下所示

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");

String userid = (String)request.getServletContext().getInitParameter("userid");

out.print("Name = " + name + "<br>");

out.print("User id= " + userid+ "<br>");
}

在我的 Web.xml 中,我添加了如下上下文参数,

<context-param>
<param-name>userid</param-name>
<param-value>ABC12345</param-value>
</context-param>

我使用 request.getServletContext().getInitParameter("userid"); 语句来访问该参数。request.getServletContext().getInitParameter("userid"); 它工作正常。但是 getServletContext().getInitParameter("userid");request.getServletContext().getInitParameter("userid"); 之间有什么区别吗?两者都给我相同的输出但我对这两个没有正确的想法。

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