Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. 也就是说,ServletContext是用来和容器通讯的,不是和别的servlet聊天用的
解决方案 »
- struts2 anction使用DomainModel接收参数为空
- javadbf不支持整型的数据吗?郁闷
- 50分求助:如何在<%! %>中使用session、out等变量?
- 谁能解决在tomcat5下运行struts出的错误
- 请教:在URL中传递中文字符串,显示出来为什么是乱码呢?
- ★★★有谁知道上传进度条的制作,请高手帮忙★★★
- 从网页上的登陆框直接打开ftp? 有谁知道,进来看一下
- 那位大哥有jbx的注册码呀?给我发一份好吗?谢谢勒!
- 大家jsp中是如何分页的?
- 我在servlet中用out.print(".....")输出html语句,但每当html中有border、align这些标记时,html就不能显示?
- 用JSP网站连的数据库是sun Solaris操作系统中的Oracle,WEB服务器是Oracle自带的ias服务器,数据库就死了...
- 用jsp操作access数据库该怎么办?
测试代码如下:
test.jsp
<html><body>
<form action="/servleta" method=post>
<input type=submit value="servleta">
</form>
<form action="/servletb" method=post>
<input type=submit value="servletb">
</form>
</html></body>
Servleta.java
..
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
this.getServletContext().setAttribute("test", new TestBean("abc"));
RequestDispatcher rd = request.getRequestDispatcher("test.jsp");
rd.forward(request, response);
}
..
Servletb.java
..
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
TestBean s = (TestBean)this.getServletContext().getAttribute("test");
System.out.println(s.getTest());
}
..TestBean.javapublic class TestBean {
private String test;
public TestBean() {
}
public TestBean(String test) {
this.test = test;
}
public String getTest() {
return test;
}
}
TestBean是对象啊!
除非是分布式,两个虚拟机,那没的说,servletcontext不能交互。
否则没有问题啊?
这个代码是我看了你的问题以后写的啊,真的可行的.