Send Redirection
(Q)What is the difference between <jsp:forward> & response.sendRedirect ?
<jsp:forward>
-->Performs forwarding request mode of jsp communication
-->Source jsp component directly interacts with destination component
-->Source jsp component & destination component uses same request and response objects
-->Source jsp can pass additional data to destination component as request attributes
-->Destination component must be local to source jsp
-->Destination component must be taken as html or jsp or servlet component
-->While forwarding request the url in browser address bar will not be changed
response.sendRedirect
-->Performs sendRedirection mode of jsp communication
-->Source jsp component interacts with destination component after having network round trip with browser
-->Will not uses same request and response objects
-->Source jsp can pass additional data by passing query string to the url of response.sendRedirect("url")
-->Destination component can be a local or remote component
-->Destination component can be there in any technology
-->While forwarding request the url in browser address bar will be changed
sendRedirection use cases:
a.)To redirect the request coming to 1 website to another website
e.g:request given to sun.com will be redirected to oracle.com
b.)To use the servlets of external websites from local websites
e.g:In Search box any website uses google search servlet
(Q)How can we pass data from source jsp component to destination component?
If source jsp component and destination component resides in the same web application
a.)request attributes/<jsp:param> (request scope->specific to each request)
-->If source jsp component and destination component uses same req, res objects
b.)session attributes(session scope->specific to each browser)
-->If source jsp component and destination component gets request from same browser
c.)application attributes (application scope->visible in the entire web application)
-->Irrespective of any condition
d.)pageContext attributes(page,request, session,application scope attributes)
If source jsp component and destination component resides in two different web application of same server
-->append query String to the url of response.sendRedirect(-)
<% response.sendRedirect(url?param1=val1¶m2=val2...);%>
Note:
-->Attribute is a name that holds value having scope by residing in request, session, application objects
-->Using 1 pageContext object we can create different scopes of attributes, so we never use request, session, application objects separately to create different scopes of attributes
JspApp12-Attributes
|-->webcontent
|-->A.jsp
|-->B.jsp
|-->C.jsp
|-->D.jsp
|-->WEB-INF
|-->web.xml
-->request attribute created in A.jsp is visible in B.jsp & C.jsp but not in D.jsp
-->Session attribute created in A.jsp by getting request from browser (IE) is visible in all web components of web application but they should also get request from same browser(IE)
-->Application attributes created in A.jsp is visible in all web components of application irrespective of any condition
Creating different scopes of attributes using pageContext object
Creating Attributes
- pageContext setAttribute("attr1","val1");
- -->Creates pageScope "attr1" attribute
- pageContext.setAttribute("attr2","val2",pageContext.SESSION_SCOPE);
- -->Creates sessionScope "attr2" attribute
OtherScopes
PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE ,APPLICATION _SCOPE
Modifying attributes
- pageContext.setAttribute("attr1","val1");
- -->modifies pageScope "attr1" value
- pageContext.setAttribute("attr2","val2", pageContext.SESSION_SCOPE);
- -->Modifies sessionScope "attr2" value
Reading attributes
- String s1=(String)pageContext.getAttribute("attr1");
- -->Reads the pageScope "attr1" valye
- String s2=(String)pageContext.getAttribute("attr2", pageContext.SESSION_SCOPE);
- -->Reads the sessionScope "attr2" value
Find attributes
- String s1=(String)pageContext.findAttribute("attr2");
- -->Searches for the attribute in multiple scope and gets the attribute
To remove attributes
- pageContex.removeAttribute("attr1");
- -->Removes attributes from page scope
- pageContext.removeAttribute("attr2", pageContext.SESSION_SCOPE);
- -->Removes attributes from sessionScope
(Q) What is the difference between getAttribute & findAttribute methods ?
getAttribute(-,-) method reads the given attribute value from the specified scope whereas findAttribute(-) reads the given attribute value by searching in multiple scopes in a order
pageScope->request scope->session scope->application scope
Note: If attribute is available in one scope then it reads the attribute value and doesn't search in other scopes
Session Tracking in JSP:-
-->Same as servlet programming based session tracking
<jsp:plugin>(outdated)
-->Given to display GUI java beans or applets on the browser through jsp component
-->Since applets and GUI java beans are outdated, it is not a recommended tag to use
E.g:
- <jsp:plugin
- code="<Applet/java bean class name>"
- codeBase="<location of above class>"
- type="applet/bean"
- width="100"
- height="200">
-->This tag internally uses <object>/<embed> to display applets/gui java bean
<jsp:fallback>(outdated)
-->It is sub tag <jsp:plugin>tag to display fallback messages when browser does not display applets/gui java beans
-->It internally uses <noembed> tag for displaying messages
E.g:
- <jsp:plugin code="..." codeBase="..." width="..." height="...">
- <jsp:fallback>browser does not support applets</jsp:fallback>
- </jsp:plugin>
Comments
Post a Comment
Express your feelings