Forums Register Login

URL rewriting

+Pie Number of slices to send: Send
Hi

I am poor at url rewriting and can not clearly understand its significance.

1) Please explain its significance.

2) give an example of redirecting jsp to servlet and using the parameters send attached to the url in the servlet class

3) I have seen a code where servlet2 is called from servlet1 as follows:
res.sendRedirect("CallServlet?param1=value1¶m2=value");
but don't know how to read or use these values in servlet2

Thankz in advance
+Pie Number of slices to send: Send
Hi Priya

1. As HTTP protocol is stateless, so server has to use different technique to maintain conversational state between server and user. This conversational state is called "Session". There are two techniques which may be used by server to maintain session.

a. Cookie
b. URL rewriting

In URL rewriting sever append session information in each URL send in the output page to browser. In case cookie is used, sever sends session data in cookie which is send back by browser with each subsequent request.


2. To send the request from one server resource to other server resource use

a.response.sendRedirect(new resource url)
b.forward request (in jsp use <jsp:forward>
c.include other resource (in jsp use <jsp:inlcude> or <%@include%>


3. request.getParameter(�param1�);

You should study the severlet/jsp basic concepts before asking question.

Regards
Alok S Kushwah
+Pie Number of slices to send: Send
Alok

thankz a lot but in seconf point

2 To send the request from one server resource to other server resource use
I wanted to send some parameter along but getting error each time.
Please help with code which shows parameters being send fom jsp and hence used in servlet
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
This post is for all those who feel some people are here to get there work done.

I have written following code in my jsp
<a href="SessionServlet;$customerId=PRIYANKA">Encode URL</a>

and want to read this value in servlet by writing
String str=request.getParameter("customerId");
but it is always returning "null"

I want to read customer on servlet
+Pie Number of slices to send: Send
 

Originally posted by priya kakkar:

I have written following code in my jsp
<a href="SessionServlet;$customerId=PRIYANKA">Encode URL</a>



Hi Priya ,

Why do not you try the following



to get getParameter work at the server side.

One more thing to add is that this isn't URL Rewriting.
[ August 27, 2007: Message edited by: Rahul Bhattacharjee ]
+Pie Number of slices to send: Send
Rahul
I know what you have written is not url rewriting
but I wanted to use same.
Please help me by modifying my code to perform url rewriting

You can use session id it thats mandatory
+Pie Number of slices to send: Send
 

Originally posted by priya kakkar:
Rahul
I know what you have written is not url rewriting
but I wanted to use same.
Please help me by modifying my code to perform url rewriting

You can use session id it thats mandatory



I thought that you are interested to use getParameter method and get the value of the parameter from the url.Is this working ?

Check for encodeRedirectURL and encodeURL of HTTPServletResponse to make use of URL rewriting for session tracking.
+Pie Number of slices to send: Send
URL Rewriting is not for passing parameters around.
It is for embedding the sessionID into URLs for clients that don't accept cookies.
+Pie Number of slices to send: Send
In that case if I do
<a href="SessionServlet;$sessionId=juwsgdiws7ydiw">Encode URL</a>

How will it help to maintain session in next SessionServlet.
I mean how will I use attributes of jsp in servlet

Does following code give me the sessionid in servlet and hence use attributes.

HttpSession session = request.getSession();
+Pie Number of slices to send: Send
 

Originally posted by priya kakkar:
In that case if I do
<a href="SessionServlet;$sessionId=juwsgdiws7ydiw">Encode URL</a>



How are you planning to put the sessionID ?
I have mentioned methods in my previous post that would take care of appending the sessionid.
+Pie Number of slices to send: Send
The sessionId is not something you build yourself.
The container takes care of that for you.
Look at the methods that Raul posted earlier.
Those methods do nothing more that add the (already existing) sessionID to your url.

None of this is even necessary if the user's browser has cookies enabled or is using SSL and has nothing to do with reading querystring variables.

Rather than trying to guess what these things do, you might want to consider downloading a copy of the servlet spec (link in my signature) and reading it.
It won't take long and doing so will give you a real thorough understanding of concepts like this.
+Pie Number of slices to send: Send
Look, as per my understanding I have made an application where call goes from jsp to servlet1 where code is like following:


RequestDispatcher rd= getServletContext().getRequestDispatcher("/Servlet2");
rd.forward(request,response);
}

From servlet1 it is dispatched to servlet2 as following
session.setAttribute("name","Priyanka");
response.encodeRedirectURL("/package1/Servlet3");

and from here to servlet3 where I want to read values from request assuming that since session is same so I can read values
String str=(String)session.getAttribute("name");

But the control comes back to servlet1 after servlet2 instead of going to servlet3


How can i use urlrewriting and hence use value of attributes of session
+Pie Number of slices to send: Send
priya,
Please go back and re-read your last post.
If there is a mistake in there, fix it so that it says what you really mean.


There are couple contradictions in there:

For Example:


From servlet1 it is dispatched to servlet2 as following
session.setAttribute("name","Priyanka");
response.encodeRedirectURL("/package1/Servlet3");


And:

But the control comes back to servlet1 after servlet2 instead of going to servlet3





As mentioned earlier.
The only thing URL Re-Writing does, is provide an alternate means of retaining the session ID for Browsers that do not allow cookies.

It doesn't directly involve passing request parameters.
[ August 27, 2007: Message edited by: Ben Souther ]
+Pie Number of slices to send: Send
The issue which you say contradictory is actually what is happening and is what is making me wonder

The problem now is that its going back to servlet1 and not going to servlet3
Please help by looking at my code and guide be by identifying where the code is wrong which make code to go back to servlt1 instead of going to servlet3
+Pie Number of slices to send: Send
Well I am ok with it was giving wrong path for servlet3

Thankz you all for patience and support
+Pie Number of slices to send: Send
Please use real words when posting to the forums. Incorrect words such as "thankz" in place of "thanks" only serve to make your posts more difficult to read and less likely to generate useful responses.

Please read this for more information.

thanks,
bear
JavaRanch sheriff
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 7204 times.
Similar Threads
Form Variables
How is jsessionId handled when moving from http --> https?
Serlet request Dispatcher
Filter in servlet
url rewriting
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:04:28.