getServletContext().getAttribute
时间: 2024-06-07 20:06:39 浏览: 19
getServletContext().getAttribute() 是一个方法,它可以在 Java Servlet 中获取 ServletContext 对象中存储的属性值。ServletContext 是一个 Web 应用程序的上下文对象,可以在整个 Web 应用程序中共享数据。通过调用 getAttribute() 方法,可以获取存储在 ServletContext 中的属性值。该方法需要一个 String 类型的参数,该参数表示要获取的属性的名称。如果属性存在,则该方法将返回属性的值;否则,它将返回 null。
相关问题
var name=<%request.getServletContext().getAttribute("name");%>这句话对吗
不完全正确。
这里的代码 `<%request.getServletContext().getAttribute("name");%>` 是 JSP 页面中的脚本表达式,它可以在页面中嵌入 Java 代码,并将其执行结果输出到 HTML 页面中。在这个表达式中,我们通过 `request.getServletContext().getAttribute("name")` 方法获取了 ServletContext 中名为 `name` 的属性值,但是我们没有将其赋值给前端的 `var` 对象。
如果我们想要将 ServletContext 中的属性值赋值给前端的 `var` 对象,可以将这个表达式的值赋给 `var` 对象,即:
```html
<script>
var name = "<%= request.getServletContext().getAttribute("name") %>";
</script>
```
在这个代码中,我们将 `request.getServletContext().getAttribute("name")` 的返回值赋值给前端的 `var` 对象 `name`。注意,在这里我们需要使用 `"<%= %>"` 这种方式来将表达式的执行结果输出到 HTML 页面中。同时,由于属性值可能是字符串类型,因此我们需要使用双引号将其包裹起来。
对于以上3个页面在输入值“小孩”ZuoPageContext.jsp页面登录之后描述错误的是()。 (选择二项) A : PageContext.jsp页面输出结果: name=小孩 pageContext.getAttribute('useName') next-->人生除了学习还是学习! 活到老学到老。 B : PageContext.jsp页面输出结果: name=小孩 next--> 人生除了学习还是学习!活到老学到老 c: PageContext.jsp点击next-->转到页面DaPageContext.jsp输出结果: pageContext.getSession().getAttribute('sessionValue')=只有在Session中才是共享的值pageContext.getServletContext().getAttribute('sharevalue')=多个页面共享的值D : PageContext.jsp点击next-->转到页面DaPageContext.jsp输出结果: pageContext.getSession().getAttribute('sessionValue')=多个页面共享的值 pageContext.getServletContext().getAttribute('sharevalue')=只有在Session中才是共享的值
选项B和D是描述错误的。在PageContext.jsp页面中,只输出了“name=小孩”,没有输出“next--> 人生除了学习还是学习! 活到老学到老”这段话。在DaPageContext.jsp页面中,sessionValue和sharevalue的描述被错误地互换了。正确的输出应该是:在PageContext.jsp页面输出“name=小孩”,在点击next按钮后转到DaPageContext.jsp页面,输出“pageContext.getSession().getAttribute('sessionValue')=多个页面共享的值, pageContext.getServletContext().getAttribute('sharevalue')=只有在ServletContext中才是共享的值”。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
请把下面这段代码改成rest风格 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); List productList = (List) getServletContext().getAttribute("products"); for (Product product : productList) { String item = response.encodeURL(request.getContextPath() + "/item?id=" + product.getId()); response.getWriter().println("" + product.getName() + ""); } response.getWriter().println("浏览足迹为:"); History.showRecentViews(request, response); }
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)