如何在没有session属性的情况下将对象从jsp页面传递给servlet

时间:2013-02-20 19:05:12

标签: java ajax jsp

我正在尝试使用request.setAttribute方法将对象传递给servelt,如下所示:

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
}

变量&#34; respos&#34;在getAttribute方法之后包含null。任何想法如何解决我的问题?

1 个答案:

答案 0 :(得分:1)

处理完jsp后,会将其呈现为html并提交至HttpResponse OutputStreamjsp不再存在于servlet的上下文中,因此您无法以您想象的方式传递任何内容。

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