This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
hi, I have some problem in sending parameters from one servlet to another. I have a HTML page which sends some data using method=POST to "servlet1". In "servlet1" I am redirecting the servlet to another servlet by appending the values to the target URL say response.sendRedirect("www.foo.com?"+parametersString); It is sending the values by default by GET method. Is there a way to redirect these values by POST method so that the values sent to the second servlet are not visible in the browser's address (URL). Thanks in advance Sonali
Try RequestDispatcher.forward(ServletRequest, ServletResponse) I think you need to make sure you haven't written anything out to the client before you call this, otherwise you'll get an error. [This message has been edited by BJ Grau (edited September 03, 2001).]
Joined: Sep 03, 2001
hi, Thanks for the reply. I tried as given by u.I am not writing on to the response object. Using forward is invoking the second servlet but the parmeters of "servlet1" are not being passed to the "servlet2". I forgot to tell you on thing. The html page from which I am calling "servlet1" is uploading a file with ENCTYPE="multipart/form-data". I am using MultipartRequest to upload the file in the "servlet1".Thank you once again. Sonali
Originally posted by BJ Grau: Try RequestDispatcher.forward(ServletRequest, ServletResponse) I think you need to make sure you haven't written anything out to the client before you call this, otherwise you'll get an error. [This message has been edited by BJ Grau (edited September 03, 2001).]
(in my opinion etc) In general you should not use response.sendRedirect() to communicate directly between two servlets since this results in two sets of HTTP message being sent. The client sends a request to servlet1, servlet1 decides not to handle the request and executes a sendRedierct("servlet2"). This sends a message back to the client saying "don't look to servlet1 one, look to servlet2 for the request (response code 302, moved temporarily) The client changes the url it diplays to "servlet2" and sends another requst for the server for servlet2. The correct way is to use the RequestDispatcher, which allows servlets to communicate within he servlet and share the same request and response objects. In this manner, servlet1 can decide to forward to servlet2, but can place some data on the request using setAttribute() to pass onto servlet2... If you look at the RequestDispatcher, there are two ways to get from one servlet (or jsp) to another, include and forward. include allows the data to be concatenated so that the page returned t the client can be built from several sources, forward attempts to ignore the initial servlet output and require the next to provide the output. In general I prefer forward since the only effect servlet1 can have on the output is based on the data it places on the request. If it has attempted to set header data or write a HTML page this data is lost. (reduces confusion in servlet2 as to what or how much has already been done)