Total Pageviews

Wednesday 4 April 2012

best way to keep data in the form when failing to submit in JSP

Problem:
When submiting the page, sometime it fails because of the validation. At this time, the data in the page  are lost, and we have to type everything again.
Solution:
Declare the varaible at the beginning, and assign the value from the request. But if it's first fime calling the page, assign it as empty string.

        <%
        String xmlStr=request.getParameter("xmlStr");if(xmlStr==null)xmlStr="";
        %>
         <form method=GET >
                <tr>
                    <td><textarea name="xmlStr" rows="12" cols="100"><%=xmlStr%></textarea></td>                   
                </tr>              
               <tr><td><input type=submit name=submit value='Post'/></td></tr>
            </table>           
        </form>
        <%
         if(request.getParameter("submit") != null){
          
                  if(xmlStr == null || xmlStr.equals("")) {
                  out.println("<p>xmlStr empty!</p>");  
                 return;
           }         
           // do whatever
         }
         %>
 

No comments:

Post a Comment