标题:Servlet问题:forward跳转后,getAttribute无法读取
只看楼主
Alen0128
Rank: 4
等 级:业余侠客
帖 子:171
专家分:222
注 册:2009-12-26
结帖率:76.19%
已结贴  问题点数:10 回复次数:1 
Servlet问题:forward跳转后,getAttribute无法读取
我有两个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 编辑 ]
搜索更多相关主题的帖子: request forward 
2011-11-03 08:39
husiwen
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:227
专家分:1125
注 册:2010-5-23
得分:7 
把doget都改成service试试。。
2011-11-03 12:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-354129-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.592920 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved