猿问

我想把从jsp页面传来的值保存为cookie,结果却抛出了空指针异常 execute函数的代码:

在我的Action的execute()函数中, public String execute(){ System.out.println("action"); if(getUser().equals("liu")&&getPassword().equals("shilei")){ ActionContext.getContext().getSession().put("user", getUser()); Cookie cookie=new Cookie("user",user); cookie.setMaxAge(60*60); response.addCookie(cookie);//就是这里出错了 return SUCCESS; } return ERROR; } 执行后的结果 java.lang.NullPointerException action.LoginAction.execute(LoginAction.java:38) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) java.lang.reflect.Method.invoke(Method.java:597) com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440) com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279) com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242) response我是在Action中声明的一个private HttpServletResponse response;

动漫人物
浏览 93回答 1
1回答

收到一只叮咚

我记得structs2要实现implementsServletResponseAware接口重写@OverridepublicvoidsetServletResponse(HttpServletResponsearg0){this.response=arg0;}privateHttpServletResponseresponse;//添加set方法//这样就可以获得response了不会出现空值了 
随时随地看视频慕课网APP
我要回答