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

jsp:forward doubt

 
sanla palati
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I got this question.

---------------------------------------------------------------

Assume that in your JSP page, you need to forward the request to another page. The url to this page is given by a request parameter named 'redirectto'.

Which of the following code snippets do the above task?



1.<jsp:forward page="request.getParameter('redirectto')" />

2.<% request.getRequestDispatcher( request.getParameter("redirectto") ).forward(request, response); %>

3.<jsp:forward page="%=request.getParameter('redirectto')%" />

4.<jsp:forward page="<%=request.getParameter('redirectto')%>" />

5.<jsp:forward page="%request.getParameter('redirectto')%" />

i thought 2& 3 would be correct answeres. But 2,3,&4 are right. Any comments ? please

Thanks,
Sanla
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see why 2 & 4 are correct. In 4 you are getting the value of a parameter and then you use it in jsp:forward, the expression <%= %> will print the value of our parameter.

now I can't get 3
%=request.getParameter('redirectto')% doesn't look like a valid expression, where are the < & >, shouldn't we use them here (like number 4)?
 
sanla palati
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even i am thinking same. but dont have any idea why the answere 3 i right
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jsp 2.0 spec. 6.2.2.

 
Gurukant Desai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, So you mean to say that only option 3 "<jsp:forward page="%=request.getParameter('redirectto')%" />
)" is right and option 4 "<jsp:forward page="<%=request.getParameter('redirectto')%>" />" is wrong?
Because I think both 3 and 4 are right..
Please clarify...
 
sanla palati
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

Thanks or your clarification.

Thanks,
Sanla
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Because I think both 3 and 4 are right..

Both should be right,.. but 3 should be OK only in JSP documents (JSP with XML syntax)
 
Vijay Bharghav bheemineni
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks John, I would have never thought 3 would be right answer, If I wouldn't had seen this post.

Vijay Bheemineni.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic