The issue is not with forward()
or sendRedirect()
, it's with how you are sending the data from the HTML
form.
Remember, the <form>
tag uses GET
method as default HTTP method. Since you haven't given any method explicitly, it will use GET
method.
See this link:
<!ATTLIST FORM
%attrs; -- %coreattrs, %i18n, %events --
action %URI; #REQUIRED -- server-side form handler --
method (GET|POST) GET -- HTTP method used to submit the form--
enctype %ContentType; "application/x-www-form-urlencoded"
accept %ContentTypes; #IMPLIED -- list of MIME types for file upload --
name CDATA #IMPLIED -- name of form for scripting --
onsubmit %Script; #IMPLIED -- the form was submitted --
onreset %Script; #IMPLIED -- the form was reset --
accept-charset %Charsets; #IMPLIED -- list of supported charsets --
>
Now, with GET
request, all your form data goes as a part of your query string, that is why you are seeing those data there. You should change the method to POST
.
<form action="loginServlet" method = "POST">
The reason why you didn't see the data while using sendRedirect()
is that, with response.sendRedirect()
the client creates and sends a new request. So, your old request URI is no longer there. With forward()
this is not the case. The URI doesn't changes, and you see the original URI, with the query string.
when i used the Sendredirect i had only the
localhost:8084/WebApplication2/welcome.jsp
As I said, the URI changes, that you can see. Hence you don't see the query string, that came with original URI.
See also: