HttpServletRequest request = ServletActionContext.getRequest();为null的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpServletRequest request = ServletActionContext.getRequest();为null的问题相关的知识,希望对你有一定的参考价值。
这是一个普通的java类,不是Action。
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("", "");
System.out.println(request+"----");
HttpSession session = request.getSession();
System.out.println(session+"++++");
这段代码开始用的时候很好,但是当我把调用 这段代码中的方法的 那个jsp放到frameset中后,request就为null了,当然后面的session也就为null了,MyEclipse中报错的是HttpSession session = request.getSession();这一行空指针异常,请问是咋搞的呢?
参考资料:如果您的回答是从其他地方引用,请表明出处
过滤器输出返回值
private void returnJson(HttpServletRequest request,ServletResponse response, ResultModel ResultModel) throws IOException //把返回值输出到客户端 HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServletResponse = (HttpServletResponse) response; httpServletResponse.setHeader("Access-control-Allow-Origin", httpServletRequest.getHeader("Origin")); httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE"); httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers")); response.setContentType("application/json; charset=utf-8"); response.setCharacterEncoding("UTF-8"); ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(JsonTools.objToJson(ResultModel).getBytes()); outputStream.flush(); outputStream.close();
以上是关于HttpServletRequest request = ServletActionContext.getRequest();为null的问题的主要内容,如果未能解决你的问题,请参考以下文章
如何向我的 Java Servlet 的现有 HttpServletRequest 添加参数?
通过servletresponse的啥方法可以设置响应所采用的字符编码
HttpServletRequest和ServletRequest的区别以及HttpServletRequest对象方法的用法
转HttpServletRequest.getParameter() &HttpServletRequest.getAttribute() 区别