Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between page scope and request scope in Servlet & JSP?

I have gone through below links so far-:
Page scope- scope in jsp
What are the different scopes in JSP?
difference between page and request

I want to know about what are the differences between page scope and request scope? If I consider use of RequestDispatcher.forward() & HttpServletResponse.sendRedirect() to same or different page, then how these scopes will work?

If request is forwarded to same page then how these two scopes will differ?

like image 840
Ashish Kumar Avatar asked Mar 09 '23 15:03

Ashish Kumar


2 Answers

Page scope is a scope that in valid only while processing one single JSP. Typically, if one JSP forwards to itself, the second instanciation will share the original request scope but will receive a brand new page scope. This can make sense if you include 2 instances of the same fragment in on page: all will share the request scope with the caller, but each instance will use its own page scope.

sendRedirect is quite a different thing! Redirecting is returning to the client a special response containing the new URL to fetch in its headers. But that also means that the redirect will use a different HTTP request and as such will have a different request scope. Said differently only session scope is shared between redirected pages - and only when you redirect to the same web application.

like image 145
Serge Ballesta Avatar answered Mar 11 '23 04:03

Serge Ballesta


Notice more than one page can serve a single request. see article.

You also can see in example that you can forward a parameter with request scope to another page by forward.

page

‘page’ scope means, the JSP object can be accessed only from within the same page where it was created. The default scope for JSP objects created using tag is page. JSP implicit objects out, exception, response, pageContext, config and page have ‘page’ scope.

request

A JSP object created using the ‘request’ scope can be accessed from any pages that serves that request. More than one page can serve a single request. The JSP object will be bound to the request object. Implicit object request has the ‘request’ scope.

like image 41
user7294900 Avatar answered Mar 11 '23 05:03

user7294900