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.
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html