wood burning stoves 2.0*
The moose likes Servlets and the fly likes encodeURL() purpose and best place to use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "encodeURL() purpose and best place to use" Watch "encodeURL() purpose and best place to use" New topic
Author

encodeURL() purpose and best place to use

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
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.


Discussion - the powerfull way to excellence!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

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.


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.


Steve
moha reddy
Greenhorn

Joined: Jul 28, 2008
Posts: 2
response.encodeURL("give your URL here")--This method is used when client donot accept cookies .So the session tracking is not possible with cookies.
This method adds session id to the end of URL.When client makes the next request then session ID can be fetched from the end of the URL.Hence session tracking will be possible.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
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.




Hi DUDE,

Thanks for the response, that was worth a lot.

And your additional information regarding forward and include, is a beneficiary added.

cheers..
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: encodeURL() purpose and best place to use
 
Similar Threads
session lost with window.open()
always only ONE session ?
question regarding maintaining session
response.encodeRedirectURL
listener question