Skip to main content

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&param2=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
  1. pageContext setAttribute("attr1","val1");
  2. -->Creates pageScope "attr1" attribute
  3. pageContext.setAttribute("attr2","val2",pageContext.SESSION_SCOPE);
  4. -->Creates sessionScope "attr2" attribute
OtherScopes
PAGE_SCOPE, REQUEST_SCOPE, SESSION_SCOPE ,APPLICATION _SCOPE
Modifying attributes
  1. pageContext.setAttribute("attr1","val1");
  2. -->modifies pageScope "attr1" value
  3. pageContext.setAttribute("attr2","val2", pageContext.SESSION_SCOPE);
  4. -->Modifies sessionScope "attr2" value
Reading attributes
  1. String s1=(String)pageContext.getAttribute("attr1");
  2. -->Reads the pageScope "attr1" valye
  3. String s2=(String)pageContext.getAttribute("attr2", pageContext.SESSION_SCOPE);
  4. -->Reads the sessionScope "attr2" value
Find attributes
  1. String s1=(String)pageContext.findAttribute("attr2");
  2. -->Searches for the attribute in multiple scope and gets the attribute
To remove attributes
  1. pageContex.removeAttribute("attr1");
  2. -->Removes attributes from page scope
  3. pageContext.removeAttribute("attr2", pageContext.SESSION_SCOPE);
  4. -->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: 
  1. <jsp:plugin
  2. code="<Applet/java bean class name>"
  3. codeBase="<location of above class>"
  4. type="applet/bean"
  5. width="100"
  6. 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:
  1. <jsp:plugin code="..." codeBase="..." width="..." height="...">
  2.  <jsp:fallback>browser does not support applets</jsp:fallback>
  3. </jsp:plugin>

Comments

Popular posts from this blog

JSP Comments

Comments in JSP:- -->Compiler /Interpreter doesn't take commented code for compilation /Interpretation, so the commented code doesn't participate in execution JSP supports 3 types of comments:- a.)HTML comments /Template text comments/ Output comments Syn :- <!---text---> -->Recognized by html interpreter of browser b.)JSP comments /Hidden comments Syn :- <%--text--%> -->JSP page compiler recognize these comments c.)Java comments /Scripting comments Syn :- //-->For single line /*-- -----*/-->For multiple line -->Java compiler(java) recognize these comments -->In Eclipse IDE JES class for first.jsp comes in our(workspace)folder/.metadata/.plugins/.org eclipse Comments (1)JSP comment<%--%> (2)Java comment(// or /*--*/) (3)HTML comments(<!---> JES Source Code (1)No (2)Yes (3)Yes In JES compiled code (1)No (2)No (3)Yes In the code going to browser (1)No (2)No (3)Yes Output (1)No (2)No (3)No -->Jsp comments are not visible in any phase...

Scripting Tags

JSP Tags/ Elements (1)Scriptlet     Standard syn:-         <%.........%>     xml syn:-        <jsp:scriptlet>...</jsp:scriptlet> Note: All scripting tags allows us to place script code(java code) 1.)The code placed in scriptlet go to _jspService(-,-) of JES class 2.)In a jsp page we can have zero or more scriptlets 3.)We place request processing logics in scriptlets 4.)Variables declared in scriptlets becomes the local variables in _jspService(-,-) of JES class in first.jsp In first.jsp: <% int a=10;  out.println("square:"+(a*a));%> In first_jsp.java(JES): public class first_jsp extends..{     public void _jspService(-,-){        int t=30;        out.println(“Square:”+(t*t));     }  } The code placed in scriptlet can use implicit objects of jsp because implicit objects and the code placed scriptlet goes to _jspService(-...

Project Architecture's

1.) Functional flow/ Architecture a.) Only for 6+/7+years b.) Explain the flow of process/business 2.) Technical Flow/Architecture a.) Upto 5+ years b.) Explain technologies /components that are involved in the project Servlet & JSP Project Architecture:- 1.)What is need of Business Deligate to convert VO class object to DTO class object ? a.) If Business Deligate is not there servlet component directly prepares DTO class object by receiving and translating form data and passes to service class, if service class excepts certain input in other format like numeric employee number as string employee number then we need to modify servlet component code i.e For the changes that happened in business tier service component we have to modify servlet component of presentation tier b.) If Business Deligate is taken then it gets original form data as VO class object having string inputs and converts VO class object to DTO class object to pass to service class c.) If service class excepts ...