Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You seem to misunderstand the working and purpose of <code>jsp:useBean</code>. </p> <p>First of all, you've declared the bean to be in the session scope and you're filling it with all parameters of the current request.</p> <pre><code>&lt;jsp:useBean id="user" class="com.hermes.data.RateCode_" scope="session"&gt; &lt;jsp:setProperty name="user" property="*"/&gt; &lt;/jsp:useBean&gt; </code></pre> <p>This bean is thus stored as <em>session</em> attribute with the name <code>user</code>. You need to retrieve it in the servlet as session attribute, not as request attribute.</p> <pre><code>RateCode_ user = (RateCode_) request.getSession().getAttribute("user"); </code></pre> <p><sup><em>(<code>user</code> is a terrible and confusing attribute name by the way, I'd rename it <code>rateCode</code> or something, without this odd <code>_</code> in the end)</em></sup></p> <p>However, it'll contain nothing. The <code>getCode()</code> and <code>getDescription()</code> will return <code>null</code>. The <code>&lt;jsp:setProperty&gt;</code> has namely <em>not</em> filled it with all request parameters yet at that point you're attempting to access it in the servlet. It will only do that when you forward the request containing the parameters back to the JSP page. However this takes place beyond the business logic in the servlet.</p> <p>You need to gather them as request parameters yourself. First <strong>get rid</strong> of whole <code>&lt;jsp:useBean&gt;</code> thing in the JSP and do as follows in the servlet's <code>doPost()</code> method:</p> <pre><code>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. </code></pre> <p>and then you can access it in the JSP as below:</p> <pre><code>&lt;input type="text" name="code" value="${user.code}" /&gt; &lt;input type="text" name="description" value="${user.description}" /&gt; </code></pre> <p><sup><em>(this is only sensitive to <a href="https://stackoverflow.com/questions/2658922/xss-prevention-in-jsp-servlet-web-application">XSS attacks</a>, you'd like to install JSTL and use <code>fn:escapeXml</code>)</em></sup></p> <p>No, you do <strong>not</strong> need <code>&lt;jsp:useBean&gt;</code> in JSP. Keep it out, it has practically no value when you're using the MVC (level 2) approach with real servlets. The <code>&lt;jsp:useBean&gt;</code> is only useful for MV design (MVC level 1). To save boilerplate code of collecting request parameters, consider using a MVC framework or Apache Commons BeanUtils. See also below links for hints.</p> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/5096454/easy-way-of-populating-javabeans-based-on-request-parameters/">Easy way of populating Javabeans based on request parameters</a></li> <li><a href="https://stackoverflow.com/questions/8920127/using-beans-in-servlets/">Using beans in servlets</a></li> <li><a href="https://stackoverflow.com/tags/servlets/info">Our Servlets wiki page</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload