您的位置:首页 > 编程语言 > Java开发

在servlet的init方法中使用getServletConfig、getServletContext时发生nullpointerexception

2016-07-14 10:11 1126 查看
在servlet的init、destroy方法中使用getServletConfig、getServletContext时发生nullpointerexception???

(转载)

java.lang.NullPointerException when calling getServletContext

Hi All,

When calling getServletContext from my servlet, i'm getting the following exception to my web client:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException

javax.servlet.GenericServlet.getServletContext(GenericServlet.java:159)

amdocs.checklist.UserPage.service(UserPage.java:126)

javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

amdocs.checklist.LoginFilter.doFilter(LoginFilter.java:65)

note The full stack trace of the root cause is available in the Apache Tomcat/5.0.27 logs.

My code is:

ServletContext sc = getServletContext();

RequestDispatcher rd = getServletContext().getRequestDispatcher("/Header");

rd.include(request, response);

It couldn't pass the call of getServletContext, Any idea why?

Thanks.

——————————————————————
Wild guess, but did you override init(ServletConfig) in your servlet?

If you did, rather override the init() method.

——————————————————————

Yes i did override it, but after removing it it worked! Thanks.

Can you explain why?

——————————————————————

I haven't looked at the source yet, but I remember the api specs talking about the init(ServletConfig) method being called by the container.

The servlet will the set up it's references to objects (eg. the ServletContext) and calls the init() method.

So, if you override the init(ServletConfig) method the references will never be set up, and you will get null calling getServletContext().

So always use the init() method for any initialization of servlets.

——————————————————————

Or, alternatively, if you do override init(ServletConfig), make sure your first line in this method is:

super(config);

Matt

——————————————————————

Thanks.

——————————————————————

This is how you overide the init method:

public void init(ServletConfig servletConfig) throws ServletException

{

super.init(servletConfig);

// your code

// ............

}

注:当你要覆盖基类中本来已有的方法时,一定要记得在新方法中先调用父类的该方法,特别是那些被封装好了的而自己又不了解其细节的类。

当你打算调用getInitParameter(String name)方法时,需要考虑以下问题:

这个parameter叫什么名字?

是谁的parameter?这一点很重要,搞不好又会发生NullPointerException,比如说,如果是在web.xml中的servlet配置项那里设置的初始化参数,则要用servletConfig来调用此方法,而不是用application~~~~~

特别提示:

在servlet类中,可以通过getServletConfig()来得到servletConfig,然后就可以有ServletContext application = servletConfig.getServletContext();

参考:

How to use application object in Servlet init method

Application object can be used in servlet init method by using ServletContext object. Application object is property of JSP, we have to use ServletContext. This will work same with application scope to application context. This object is available globally
to whole application.

Init method of servlet passes parameter of ServletConfig. This ServletConfig object uses getServletConfig method.

public void init() throws ServletException

{

ServletConfig config = getServletConfig();

ServletContext sc = config.getServletContext();

}

收藏于 2010-04-23
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  servlet java