Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 上下文对象在jsp或servlet文件中不起作用_Java_Jsp_Servlets - Fatal编程技术网

Java 上下文对象在jsp或servlet文件中不起作用

Java 上下文对象在jsp或servlet文件中不起作用,java,jsp,servlets,Java,Jsp,Servlets,我正在创建许多jsp和servlet文件,但这次我很困惑 我有一个名为test.java的servlet文件 ServletContext context = request.getServletContext(); context.setAttribute("Fname","chintan"); context.setAttribute("Lname","popat"); request.getRequestDispacher("test.jsp").forword(request,res

我正在创建许多jsp和servlet文件,但这次我很困惑

我有一个名为test.java的servlet文件

ServletContext context =  request.getServletContext();

context.setAttribute("Fname","chintan");
context.setAttribute("Lname","popat");
request.getRequestDispacher("test.jsp").forword(request,response);
在test.jsp中

<%
   String fname = (String)context.getAttribute("Fname");  //popat
   String lname = (String)context.getAttribute("Lname");  //popat
%> 

在jsp文件中,获取存储在最后一个上下文对象中的所有上下文属性值 那怎么可能呢
2 diff属性返回示例值当我设置diff value时

如果您做错了,请从PageContext而不是从RequestDispatcher使用forward方法

       pageContext.forward("/resource.jsp"); 

这是我的工作测试:

在servlet中:

ServletContext context = request.getServletContext();
context.setAttribute("Fname","chintan");
context.setAttribute("Lname","popat");
request.getRequestDispatcher("/test.jsp").forward(request, response);
test.jsp:

<%
String fname = (String)application.getAttribute("Fname");  //chintan
String lname = (String)application.getAttribute("Lname");  //popat
out.write(fname); out.write(lname);
%> 

正确写入
chintanpoat


但是。。。它同时处理一个请求,因为应用程序上下文在应用程序的所有请求之间共享。在jsp中,servlet上下文可以通过
应用程序
而不是
上下文
访问(除非您在其他地方设置了它)。

这真的很奇怪。不应该发生。您是否在集合中为“chintan”和“popat”值使用变量(覆盖)值?即字符串value=“chintan”,context.setAttribute(“Fname”,value);context是“ServletContext”的对象,存在相同的问题try request.setAttribute(“属性名”、“属性值”)。并以“${requestScope['attribute-name']}”的形式从页面中获取它