2013-02-20 63 views
0

我想传递一个对象到的servelt用了request.setAttribute方法,像这样:如何从一个jsp页面传递一个对象到一个servlet没有会话属性

的jsp:

<%request.setAttribute(ResponsibilitiesCollectionRdg.RESPONSIBILITIES_COLLECTION, new ResponsibilitiesCollectionRdg());%> 

Java代码:

public class ResponsabilityHtmlCommand extends FrontCommand { 


@Override 
public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException 
{ 

    int id = new Integer(request.getParameter("ID")); //get the id of the module 
    boolean toAdd = new Boolean(request.getParameter("toAdd")); // get if we need to add or remove the responsibilities 
    ResponsibilitiesCollectionRdg respos = (ResponsibilitiesCollectionRdg) request.getAttribute(ResponsibilitiesCollectionRdg.RESPONSIBILITIES_COLLECTION); 
    //add or remove the responsibilities 
    if(id != -1) 
    { 
     if(toAdd) 
      respos.addResp(id); 
     else 
      respos.removeResp(id); 
    } 

    response.getWriter().write(ResponsabilityFinder.findHtmlForRespDropDown(respos.getListOfResp())); //send the tag 
} 

变量 “respos” 的的getAttribute梅索德后包含空。任何想法如何解决我的问题?

+0

您是否正在尝试在处理一个请求期间设置该值,并在处理下一个请求期间检索该值? – 2013-02-20 19:09:22

+0

当用户选中一个复选框时,复选框的ID将存储在request.setAttribute中,然后它将调用发送到ajax函数以使用java类。 – Riri 2013-02-20 19:12:00

回答

1

处理完jsp后,它会呈现为html并致力于HttpResponseOutputStreamjsp不再存在于servlet的上下文中,因此无法以您想象的方式传递任何内容。

你可以做的是为下一个Http请求提供参数,你可以通过<a href="/my/wtv/site?attr=myattribute">或者<form action="/my/wtv/site?attr=myattribute">的表单提交以及用作请求参数的输入元素。

相关问题