[英]How to pass an Object from a jsp page to a servlet without session property
我正在尝试使用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
}
变量“ respos”在getAttribute方法之后包含null。 有什么想法可以解决我的问题吗?
处理完jsp
,将其呈现为html并提交到HttpResponse
OutputStream
。 jsp
不再存在于servlet的上下文中,因此您无法以您认为的方式传递任何信息。
您可以做的是使参数对于下一个发出的Http请求可用,可以从锚点<a href="/my/wtv/site?attr=myattribute">
或表单提交<form action="/my/wtv/site?attr=myattribute">
以及用作请求参数的输入元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.