JSP doPost getAtribute空值
我想将值传递给servlet,但我保留了get null值JSP doPost getAtribute空值,jsp,post,get,attributes,Jsp,Post,Get,Attributes,我想将值传递给servlet,但我保留了get null值 <jsp:useBean id="Helper" class="model.Registration" scope="request"/> <form action="/Project/Registration" method="post" enctype="multipart/form-data"> <input type="text" size="20" name="name" value="&
<jsp:useBean id="Helper" class="model.Registration" scope="request"/>
<form action="/Project/Registration" method="post" enctype="multipart/form-data">
<input type="text" size="20" name="name" value="<%=Helper.getName()%>">
<input type="submit">
</form>
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Registrationh2 = (Registration) request.getAttribute("Helper");
if(h2!=null){
System. out.println(h2.getName());
}
else
System. out.println("NULL");
}
受保护的void doPost(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
Registrationh2=(Registration)request.getAttribute(“Helper”);
如果(h2!=null){
System.out.println(h2.getName());
}
其他的
System.out.println(“空”);
}
我的代码有什么问题吗?我假设您想从注册模型传递名称字符串。如果是这种情况,则需要使用以下代码获取
String registrationName = request.getParameter("name");
另外,将enctype作为多部分删除。当您从客户端上载任何文件时,都会使用此选项 注册对象的范围是请求。因此,它在新请求的servlet中不可用。
您可以通过在会话中设置来获取bean。或者,将作用域设置为会话。如果使用
多部分/表单数据
表单编码,则无论作用域是什么,jsp:useBean
都将不起作用。处理文件上传需要这种编码,而JSP/Servlet中对解析这种编码的支持并不是以透明的方式构建的,request.getParameter()
和consorts将返回所需的值。然而,新的Servlet3.0API支持这种表单编码,但是由于您需要使用request.getPart()
来实现这种编码,因此jsp:useBean
也不能使用这种编码
正如您在之前发布的相关问题中多次回答的那样,您需要解析一个多部分/表单数据
请求。但是,您可以创建一个过滤器
,在多部分/表单数据
请求的情况下,该过滤器透明地解析并替换原始的请求
。您可以在这里找到关于它的详细信息,以及完整的代码示例