Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URL rewriting

 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Alok Kushwah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Ku
Ranch Hand
Posts: 510
 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
URL Rewriting is not for passing parameters around.
It is for embedding the sessionID into URLs for clients that don't accept cookies.
 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
priya kakkar
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I am ok with it was giving wrong path for servlet3

Thankz you all for patience and support
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic