At the top of my doPost method I grab a few parameters that I previously set in the JSP using a basic form:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String serverName = request.getParameter("serverName");
String destFileName = request.getParameter("destFileName");
String userName = request.getParameter("userName");
String Message= request.getParameter("Message");
and at the end of the doPost method I add them back to the request object using setAttribute:
request.setAttribute("userName ", userName );
request.setAttribute("destFileName", destFileName);
request.setAttribute("serverName", serverName);
request.setAttribute("Message", Message);
request.getRequestDispatcher(page).forward(request, response);
And then I get them in my JSP via request.getParameter again:
<% /** if the parameters are already in place, grab them **/
String destFileName = request.getParameter("destFileName");
String user = request.getParameter("user");
String serverName = request.getParameter("serverName");
String Message = request.getParameter("Message");
%>
And this works, unless I've rewritten the value on its way through the doPost method. If that is the case, then I have to use request.getAttribute in the JSP to retrieve it since request.getParameter will retrieve the value as it was defined at the top of the doPost method, ignoring any changes that were made between the top and the bottom.
Does anyone have an explanation of this? I've got everything working, but I'd like to understand why I spent a couple of hours of frustration figuring out what was wrong.
getParameter and getAttribute are completely unrelated.
getParameter
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.
getAttribute
Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
In other words, returns a value that was set using setAttribute().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With