Discussion - the powerfull way to excellence!
Originally posted by ram kumar:
Would some one help me with
encodeURL()
sendredirect()
RequestDispathcer()
the purpose of that
the best place to use these
searched some materials in net, was not satisfied.
Please give me a good link if possible
thanks.
Steve
Originally posted by Steve Luke:
encodeURL Use it to ensure session management is handled properly. It takes a URL in, and if the user has cookies turned off, it attaches the jsessionid to the URL in a proper format to be recognized as the session identifier.
When to use it? Every time you have a link, form action, sendRedirect or other URL that goes to the client and your application requires maintenance of a server-side session. You do not need it for server-side forwards and includes.
sendRedirect Sends a header response to the client telling them to request a new page. Pass it the URL to which you want the user to be sent to.
When to use it? When you want the user to go to a new page, and see the URL of this new page. A typical use is after the user submits a POST request to a servlet, the servlet does some work, and then wants to show the client a JSP with the results. If you don't want the user to be able to refresh and re-submit the form, then use a sendRedirect. Now if the user refreshes the page then only the display gets refreshed and the form is not reposted (making the Servlet work again), which is especially important when the servlet makes important changes (like saves data to a database).
You can't use a sendRedirect after you have written content to the client.
Example: User fills out a New User form and submits. Servlet process the form and adds a new user to the database. Servlet does a sendRedirect to a Welcome page. The user navigates forward, then hits the back button and comes to the Welcome page - but does not cause the new user to be added to the database again. Pressing back one more time brings him back to the New User form.
RequestDispatcherA class used to control server-side redirection of request and response. There are two distinct methods in the request dispatcher:
Forward Pass the complete handling of the Request and Response from the current Servlet to another.
When to use? When the current servlet is done its job, and hasn't done anything with the response, you Forward the request and response to another servlet (or JSP) to finish any work and to display results.
The servlet which calls the Forward can not have written any content to the response before making the forward, and will not be able to access the response when the forward method is completed.
Example: A servlet retrieves a bunch of information from a database and stores it in the request object. It then forwards the request to a JSP to display the data. User presses refresh, and the control goes to the servlet, which again retrieves the data from the database and puts it in the request, forwards to the JSP, and the JSP sees the data.
Include Temporarily pass the request and response cycle to another servlet.
When to use? When the current servlet has done some of its job is using another servlet (or JSP) to help with display or other work. If the servlet using the RequestDispatcher has already written to the response, then you will need to use the include to send control to another servlet, not the forward. Also, if you want to spread the display of a page across multiple pages, use an Include for each part of a page.
Example: Servlet handles a request from the client and does the necessary work. It then uses RequestDispatcher#include to add a Header response to all pages, since the header and banner on a page is the same for all pages on the the site. It then includes a second JSP which displays the content that is specific to this request. It finally includes a third page that acts as a Footer for all pages on the site.
Discussion - the powerfull way to excellence!
There is no greater crime than stealing somebody's best friend. I miss you tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|