WEB中一个关于静态方法和变量访问的问题
时间:2011-12-05
来源:互联网
比如定义了一个类,一个静态变量,两个静态方法
public final class ConnectionPoolInit {
private static String test = null;
public static String getTest(){
return test;
}
public static void setTest(String test){
test = test;
}
}
在一个servlet中赋值
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ConnectionPoolInit.setTest("abcd");
}
}
在另一个servlet中取值
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String test = null;
test = ConnectionPoolInit.getTest();
System.out.println(test);
}
}
为什么总是返回空呢
public final class ConnectionPoolInit {
private static String test = null;
public static String getTest(){
return test;
}
public static void setTest(String test){
test = test;
}
}
在一个servlet中赋值
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ConnectionPoolInit.setTest("abcd");
}
}
在另一个servlet中取值
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String test = null;
test = ConnectionPoolInit.getTest();
System.out.println(test);
}
}
为什么总是返回空呢
作者: sunny_1976 发布时间: 2011-12-05
又重新做了实验,没有错误
public static void setTest(String test){
test = test;
}
赋值有问题,这样变成自己给自己赋值了,参数名称改成test1就好了。
public static void setTest(String test){
test = test;
}
赋值有问题,这样变成自己给自己赋值了,参数名称改成test1就好了。
作者: sunny_1976 发布时间: 2011-12-05
test = test;
改成
ConnectionPoolInit.test =test
改成
ConnectionPoolInit.test =test
作者: panmenghaha 发布时间: 2011-12-05
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28