本人在JSP里设置了
Moive moive = new Moive();//Moive类
moive.setName("111");
request.setAttribute("moive_up",moive)
Moive mo = new Moive();//Moive类
mo = request.getAttribute("moive_up");
System.out.println("mo.getName()="+mo.getName());在后台Sevlet里设置了
Moive moive = request.getAttribute("moive_up");
System.out.println(moive.getName());怎么在JSP里的mo = request.getAttribute("moive_up")能够输出数据,反而在后台里却输出不了呢?
好像后台的request.setAttribute能够在JSP里读取,而前台的 request.setAttribute不能在后台getAttribute.有这样的说法吗?还是我错了?
谢谢!

解决方案 »

  1.   

    你有从这个jsp发送请求到servlet吗?
      

  2.   

    request中的东西只能在一次请求中使用
      

  3.   

    回1楼,有的,在servlet那里可以输出其他信息
      

  4.   

    回2楼:我将request中的东西设置在一次请求中使用了,还是不行
      

  5.   

    mo = request.getAttribute("moive_up");
    System.out.println("mo.getName()="+mo.getName());
    把这二句注掉看看  是不是上面set完这里就将他载取了?
      

  6.   

    回7楼:已经注释掉了,还是不行,一直在后台的System.out.println(moive.getName())这里
    抛出java.lang.NullPointerException
      

  7.   

    类型转化一下行不行?
    Moive moive = (Movie)request.getAttribute("moive_up");
      

  8.   

    JSP如何跳到servlet的?重定向不等用request带值,转发可以。
      

  9.   

    回11楼:通过Form表单的action来跳到servlet的
      

  10.   

    JSP
    <%@ page language="java" pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
    <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
    <%@ page import="java.util.*,cinema.moive.*" %><html>
      <head>
      </head>
      
      <body>
        <html:form action="/moiveManage">
       <% 
        Moive moive = new Moive();
        moive.setName("qqqq");
        request.setAttribute("moive_up",moive);

       %>
         <input type=submit value="Delete" name="submit">
         </html:form>
      </body>
    </html>后台                       
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
            if(request.getParameter("submit").equals("Delete"))
           {
    moive = (Moive)request.getAttribute("moive_up");
    System.out.println("11111");
    System.out.println(moive.getName());
    return mapping.findForward("showall");
    }   
    return null;
    }
      

  11.   

    有没有在 <form action="???" >里面加上 method="post"?
      

  12.   

    加上 加上 method="post"  试试?
      

  13.   

    回15、16:已经加了,还是老问题,感觉没有加的话默认是post吧
      

  14.   

    System.out.println("11111");
    能输出吗?
      

  15.   

    确定 进入 servlet里面了?
      

  16.   

    哦 敲错了 是Action、 <html:form action="moiveManage.do">
    行不
      

  17.   

    回18,19:能输出11111,就他奶奶的抛不出System.out.println(moive.getName()); 
      

  18.   

    maction  里面 movie为空吗?
      

  19.   

    Moive moive = request.getAttribute("moive_up");getAttribute 改成 getParameter 试试。
      

  20.   

    后台
    Movie moive = (Moive)request.getAttribute("moive_up");
      

  21.   

    回23:getParameter好像返回的是String,getAttribute返回的是Object,所以我采用了getAttribute
      

  22.   

    噢,不好意思- -我贴上去的前不小心把定义Moive moive = new Moive();这行删了,后面改回来了
      

  23.   

    回28:14楼的代码中已是request的一次请求中使用了
      

  24.   

    如果我没理解错
    jsp中request是jsp收到的,
    jsp在运行时也被编译成servlet
    servlet中的request是你jsp发给他的,这是两个request
    直接在request中setAttribute她只存在与同一个request中,而你这里是两个
    如果想让Attribute长时间存在是不是可以在session中加入
    比如request.getSession().setAttribute(name,object);
      

  25.   

    直接改成 session.setAttribute("moive_up",moive);试试
      

  26.   

    嗯,问题终于解决了:
    我查到了以下资料:
    Request范围下的属性只有在一个请求紧接另一个请求的情况下,值才存在 
    如过你在第一个页面设置值,然后用foward标签或用与foward方法 
    将他转到第2个页面,就能得到值 
    而你的第一个页面先设置值,然后等到用户的输入后才转到第2个页面, 
    在这段时间这个值早已被系统淡忘了 所以我在前台改成了使用
    session.setAttribute("moive_up",moive);
    后台
    HttpSession session  = request.getSession();
    moive = (Moive)session.getAttribute("moive_up");
    这样就解决了,谢谢大家~!
      

  27.   

    用action跳转到servlet的时候已经是一个新的请求了
    也就是一个新的request对象,当然不会有值了