简体   繁体   中英

How to pass an Object from a jsp page to a servlet without session property

I am trying to pass an object to a servelt with the request.setAttribute method like so :

jsp :

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

java code :

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
}

The variable "respos" contain null after the getAttribute methode. Any ideas how to solve my problem?

Once a jsp has been processed, it is rendered to html and commit to the HttpResponse OutputStream . The jsp no longer exists in the context of the servlet and so you cannot pass anything in the way you might think.

What you can do is make parameters available for the next Http Request you make, either from an anchor <a href="/my/wtv/site?attr=myattribute"> or in a form submission <form action="/my/wtv/site?attr=myattribute"> and input elements which are used as request parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM