注册 登录
编程论坛 J2EE论坛

Servlet问题:forward跳转后,getAttribute无法读取

Alen0128 发布于 2011-11-03 08:39, 806 次点击
我有两个Servlet文件:req和rep;  在req中,用  request.getRequestDispatcher("rep").forward(request, response),跳转到了rep;  但是在rep中无法取出request中的数据。
在req中,代码如下:
程序代码:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");

        request.setAttribute("aa","dp");
        request.getRequestDispatcher("rep").forward(request, response);

        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }


在req中,代码如下:
程序代码:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
        out.println("  <BODY>");

        out.println("跳转到RepServlet");        
        String ss = (String) request.getAttribute("aa");
        System.out.println(ss);
        if (ss == null) {
            out.println("跳转不能从请求获得数据");
        }else {
            out.println(ss);
        }
        
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }


[ 本帖最后由 Alen0128 于 2011-11-3 19:20 编辑 ]
1 回复
#2
husiwen2011-11-03 12:56
把doget都改成service试试。。
1