Jsp useBean is NULL via servlet getAttribute

user is null in servlet. Pls let me if I am wrong.

im trying to get the whole html element in bean rateCode.jsp

<%@page import="com.hermes.data.RateCode_" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Rate Code</title> </head> <body> <jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="request" > <jsp:setProperty name="user" property="*"/></jsp:useBean> <form id="f_rateCode" action="/ratePromoCodes" method="post" > <table align="center" border="1" cellspacing="0"> <tr> <td colspan="2" align="center" class="header">Rate Code Administrations</td> </tr> <tr> <td align="right" style="border-style: solid;">Rate Code:</td> <td align="left" style="border-style: solid;"> <input type="text" id="code" name="code" value="${user.code}" size="10" maxlength="32" style="width: 100px"/> </td> </tr> <tr> <td align="right" style="border-style: solid;">Rate Description:</td> <td align="left" style="border-style: solid;"> <input type="text" id="description" name="description" value="<%=user.getDescription()%>" maxlength="128" size="40"></td> </tr> <tr><td><input type="submit" value="ok" /></td> </tr> </table> </form> 

Servlet - ratePromoCodes

 protected void doPost(HttpServletRequest req, HttpServletResponse resp) { RateCode_ rc = (RateCode_) req.getAttribute("user"); Enumeration an = req.getAttributeNames(); Enumeration pn = req.getParameterNames(); Object o = null; while (an.hasMoreElements()) { o = an.nextElement(); System.out.println(o); } while (pn.hasMoreElements()) { o = pn.nextElement(); System.out.println(o); } } 

RateCode.java (javaBean)

 public class RateCode_ { private String code ; private String description; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } 
+4
source share
3 answers

You seem to misunderstand the work and purpose of jsp:useBean .

First of all, you indicated that the bean is in the session area, and you fill it with all the parameters of the current request.

 <jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="session"> <jsp:setProperty name="user" property="*"/> </jsp:useBean> 

This bean is thus saved as a session attribute named user . You need to get it in the servlet as a session attribute, not as a request attribute.

 RateCode_ user = (RateCode_) request.getSession().getAttribute("user"); 

( user is a terrible and confusing attribute name, by the way, I would rename it rateCode or something without this odd _ at the end)

However, it will not contain anything. getCode() and getDescription() will return null . <jsp:setProperty> did not fill it with all request parameters, but at that moment you are trying to access it in the servlet. This will only be done when you forward the request containing the parameters back to the JSP page. However, this happens outside the servlet’s business logic.

You need to collect them as query parameters yourself. First, get rid of the whole <jsp:useBean> thing in JSP and do doPost() in the servlet doPost() as follows:

 RateCode_ user = new RateCode_(); user.setCode(request.getParameter("code")); user.setDescription(request.getParameter("description")); // ... request.setAttribute("user", user); // Do NOT store in session unless really necessary. 

and then you can access it in the JSP, as shown below:

 <input type="text" name="code" value="${user.code}" /> <input type="text" name="description" value="${user.description}" /> 

(this is only sensitive to XSS attacks , you want to install JSTL and use fn:escapeXml )

No, you do not need <jsp:useBean> in JSP. Hold it, it doesn't really matter when you use the MVC approach (level 2) with real servlets. <jsp:useBean> is only useful for MV design (MVC level 1). To save the template code for the collection request parameters, consider using the MVC or Apache Commons BeanUtils structure. See Also links for tips.

See also:

+7
source

The task (and its solution) is as follows:

You create a bean user request scope, but as soon as the page loads, the request terminates and leaves - it is not surprising that this is null in the next request, which is not completely related to this. You would probably like to do the following:

1) Remove <jsp:useBean ...> from the jsp page completely so that it looks like this:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page import="com.hermes.data.RateCode_" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head><title>Rate Code</title></head> <body> <form id="f_rateCode" action="/forwarder.jsp" method="post"> <table align="center" border="1" cellspacing="0"> <tr> <td colspan="2" align="center" class="header">Rate Code Administrations</td> </tr> <tr> <td align="right" style="border-style: solid;">Rate Code:</td> <td align="left" style="border-style: solid;"><input type="text" id="code" name="code" value="" size="10" maxlength="32" style="width: 100px"/></td> </tr> <tr> <td align="right" style="border-style: solid;">Rate Description:</td> <td align="left" style="border-style: solid;"><input type="text" id="description" name="description" value="" maxlength="128" size="40"></td> </tr> <tr> <td><input type="submit" value="ok"/></td> </tr> </table> </form> </body> </html> 

2) Your form is now redirected to another jsp forwarder. It looks like this:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="request"/> <jsp:setProperty name="user" property="*" /> <jsp:forward page="/ratePromoCodes" /> 

What it does: it creates a bean request scope - the request that submitted the form. Then it fills the bean properties with data from the form and finally forwards (IN SAME REQUEST, HERE POINT) to the servlet that does some work.

3) Finally do something in your servlet, I did this for testing:

 public class TestServlet extends javax.servlet.http.HttpServlet { protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException { RateCode_ code = (RateCode_) request.getAttribute("user"); System.out.println(code); } } 
+4
source

JSP uses a bean with query coverage. This JSP is sent and the servlet is responding.

When the servlet is executed, a new "life cycle" is started, and the request area does not contain any bean with the request number used / created in the JSP.

You must provide the bean properties as query parameters and read them one at a time in the servlet.

0
source

Source: https://habr.com/ru/post/1340148/


All Articles