I want to pass value to servlet but I keep get null 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");
}
Is there anything wrong with my code?
Regardless of the scope,
jsp:useBeanwill not work if you usemultipart/form-dataform encoding. This encoding is required to handle file uploads and the support to parse this encoding is not built in JSP/Servlet in such a transparent manner thatrequest.getParameter()and consorts would return the desired value. The new Servlet 3.0 API has however support for this form encoding, but since you would needrequest.getPart()for this instead, this will not work withjsp:useBeanas well.As answered several times in related questions you posted before, you need Apache Commons FileUpload to parse a
multipart/form-datarequest. You can however create aFilterwhich transparently parses and replaces the originalrequestin case ofmultipart/form-datarequests. You can find here a blog article about it, complete with code examples.