Redisplay of pages with input errors is one of the things that application
frameworks like Struts deal with <http://jakarta.apache.org/struts>.  You
might want to take a look at that, instead of rolling your own for every
single form.

Craig


On Fri, 5 Jul 2002, Kevin HaleBoyes wrote:

> Date: Fri, 5 Jul 2002 15:21:22 +0100 (BST)
> From: Kevin HaleBoyes <[EMAIL PROTECTED]>
> Reply-To: Tomcat Users List <[EMAIL PROTECTED]>
> To: [EMAIL PROTECTED]
> Subject: RequestWrapper question
>
> It is a common problem that I'm tying to solve.
>
> I have a web application that has a fill-in form.  The user
> fills in some of the fields and submits it to a servlet for
> processing.  The servlet verifies the input request parameters
> either directly or using (EJ) Beans.  If the input data is not
> valid then the form is redisplayed with the previously submitted
> values filled in ready for corrections.
>
> It is this last step that I'm now working on.  I'd like to use
> JSTL (core) tags in my form and I'll probably have my servlet
> directly validate the data (acting as the model and controller).
>
> Consider the following simple fill-in form: fif.jsp
>
>     <%@ taglib uri="http://java.sun.com/jstl/core";
>             prefix="c" %>
>
>     <html><head><title>Test Fill-in Forms</title></head>
>     <body>
>     <form method="post" action="TestServlet">
>        <input type="text" name="inputstr"
>                     value="<c:out value='${inputstr}'/>">
>        <input type="submit" value="Do">
>     </form>
>     </body></html>
>
>
> When the servlet receives the request it looks at the value of
> request.getParameter("inputstr") and determines that it is not
> correct so it forwards the request back to the fif.jsp page.
>
>
> public class TestServlet extends HttpServlet
> {
>     public void doPost( HttpServletRequest request,
>                         HttpServletResponse response )
>                         throws ServletException, IOException {
>         String inputstr = request.getParameter("inputstr");
>         // ... but it is not valid
>         getServletContext().getRequestDispatcher("/fif.jsp").
>                 forward(request, response);
>     }
> }
>
> This doesn't work because <c:out> will call
> PageContext.findAttribute() which looks for attributes and
> not parameters.
> Section 3.2 of the JSTL spec says of identifiers:
>   "searches for the named attribute in page, request,
>   session (if valid), and application scope(s) in order
>   and returns the value associated or null"
>
> I don't think I should set the attribute at the application or
> the session levels as they don't seem to belong there.
> I dont' know how to set attributes in the
> page context so I had a look at the request level.
>
> I need to add attributes to the request before I forward it but
> the setAttribute() method isn't defined for HttpServletRequest.
>
> So, I have to wrap the request and override the Attribute
> related method calls.  There seems to be a good example of how
> I might do this in the core code of catalina -
>   org.apache.catalina.core.Application[Http]Request.
>
> Then I can change TestServlet to use the wrapper and set the
> 'inputstr' attribute before forwarding the request.
>
> public class TestServlet extends HttpServlet
> {
>     public void doPost( HttpServletRequest request,
>                         HttpServletResponse response )
>                         throws ServletException, IOException {
>         String inputstr = request.getParameter("inputstr");
>         // ... but it is not valid
>
>         // use my own implementation of catalina's core
>         //  Application[Http]Request classes
>         ApplicationHttpRequest appreq =
>                          new ApplicationHttpRequest(request);
>         appreq.setAttribute("inputstr", inputstr);
>
>
>         getServletContext(). getRequestDispatcher("/fif.jsp").
>                 forward(appreq, response);
>     }
> }
>
>
> That is what I'm thinking of doing but I wanted to ask a few
> questions.
>
> Does any of the proceeding explanation make sense?
>
> It doesn't have to be the best way, but
>  is it one of the possible ways of solving the problem?
>
> Most of the examples that I've found on RequestWrapper have
> been to do with Filters.  Any reason why that would be?
>
> This is a very common problem and many have come before me.
> What improvements could I make?  What are the other solutions?
>
>
>
> Thanks for your help,
> Kevin.
>
>
> __________________________________________________
> Do You Yahoo!?
> Everything you'll ever need on one web page
> from News and Sport to Email and Music Charts
> http://uk.my.yahoo.com
>
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to