...
public class MyServlet extends HttpServlet {
ServletConfig config;
public void init(ServletConfig config) throws Exception {
this.config = config;
} public void doPost(...)throws ...{
...
this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
...
}
}我查过servlet的API,HttpServlet从GenericServlet继承了getServletContext(),init(),init(ServletConfig)方法 而我的MyServlet类继承至HttpServlet 但在MyServlet中从写了有参的init方法为什么就不能直接调用getServletContext()而要通过config才能取得ServletContext对象实例
public class MyServlet extends HttpServlet {
ServletConfig config;
public void init(ServletConfig config) throws Exception {
this.config = config;
} public void doPost(...)throws ...{
...
this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
...
}
}我查过servlet的API,HttpServlet从GenericServlet继承了getServletContext(),init(),init(ServletConfig)方法 而我的MyServlet类继承至HttpServlet 但在MyServlet中从写了有参的init方法为什么就不能直接调用getServletContext()而要通过config才能取得ServletContext对象实例
解决方案 »
- ssh:No result defined for action com.v512.blog.action.CommentActio
- Hibernate出错后如何初始化
- 面试中遇到的一个问题,关于jsp中值传递的!哪位教教我!!!
- jsf的问题
- 在线求教bean变化,jsp文件不能检测到的问题!
- 招聘软件开发兼职人员
- ※创业贴※ 让我们掌握自己的命运吧—— 一起创业,期待您的加盟 ※创业贴※
- 我写了一个类是用来将库中的记录在<select></select>中显示!请看一下错的原因!
- 关于jsp和XML问题!
- 急!关于java servlet与数据库连接的问题???
- struts2.0中的标签找不到
- 下拉框问题
init(ServletConfig config) 中会自动调用init()
或者在子类中这样写init(ServletConfig config) {super.init(config);)
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
mvc.servlet.MvcServlet.doPost(MvcServlet.java:16)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.上面是出错信息 如果不重写有参数的init方法或重写有参的init并通过config取得ServletContext对象实例 则不会报错
如果一定要这样,那改成下面的看一下
...
public class MyServlet extends HttpServlet {
ServletConfig config = getServletConfig();
public void init(ServletConfig config) throws Exception {
super.init(config);
// ..................你要初始化的处理
} public void doPost(...)throws ...{
...
this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
...
}
}
public class MyServlet extends HttpServlet {
ServletConfig config;
public void init(ServletConfig config) throws Exception {
this.config = config;
} public void doPost(...)throws ...{
...
this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
...
}
}
又看了一下,问题应该出现在this.config = config;这一句
因为你覆盖了public void init(ServletConfig config) throws Exception ,原本GenericServlet中定义了
一个private ServletConfig config,GenericServlet的getServletContext()方法也是通过传入的config
的getServletContext方法得到ServletContext对象
现在该方法被复写后没有给父类的config赋值,导致在执行继承来的getServletContext(){config.getServletContext}
方法时config(父类的中声明的那个config)为null而抛异常NullPointerException
...
public class MyServlet extends HttpServlet {
ServletConfig config;//虽然这里你声名的和父类中的变量名一样,但是不会覆盖父类的私有config变量,
//父类的那个config只是在子类不可见
public void init(ServletConfig config) throws Exception {
super.init(config);//由于GenericServlet没有定义public方法对config赋值,
//所以只能这样对那个private的config赋值
this.config = config;//其实没有必要在此保存config,随时可以调用继承来的getServletConfig()
//方法获取config
} public void doPost(...)throws ...{
...
this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
...
}
}