Professional Documents
Culture Documents
2) Builder - It builds a complex object using simple objects and using a step by
step approach . end result would be a complex object but in order to create that
complex object using simple objects .
Recall stringBuilder class - mutable class
eg 1)
StringBuilder sb = new StringBuilder();
sb.append("hello");
sop(sb); // hello
class MyNum
{
int k;
MyNum add(int k)
{
this.k=k;
return this; // mutable class - returning same object for modification
return new MyNum(k) // immutable class - new object is created
everytime
}
}
Redirect Forward - what if one web component [servlet or jsp] is not sufficient ?
partially processing of request
This can be achieved by redirect or forward
Real life scenario - scenario 1) Buy a particular thing - one shopkeeper redirects
to another shopkeeper to get object or thing - we know shopkeeper holding object
second scenario - shopkeeper will not have that thing but will manage and bring
that - we will dont know exactly who is serving you.
a) redirect -
client request goes to redirecting serv that servlet recommends client to go for
another servlet and that serves it
b) forward -
client request goes to forwarding serv that servlet forwards that same request to
forwarded serv which processes the request
which servlet should be run first - redirecting servlet - inside output in web
browser we have RedirectedServ in address bar and its content
what is the Sequence of this application?
-> a) We Requested RedirectingServ
b) RedirectingServ recommended us to go to another RedirectedServ(shopkeeper )
c) based on this recommendation , we issued a fresh request to RedirectedServ,
d) RedirectedServ gave us response.
What we did?
-> we requested RedirectingServ and we got the response from RedirectedServ.
compare above two questions - b) and c) in first question are hidden behind
response.sendRedirect("RedirectedServ");
open redirect flow - ppt - redirect can go beyond container to another resource eg.
(www.google.com)
client knows clearly who responded his request
Note- After forward we will not write any code because it will be forwarded and
forward will be used mostly in conditional situations(conditional statments )
forward flow - forward cannot go outside container to any other resources - It is
faster. it uses same request does not create another response.
but for outside project? - we have way to do it -
create two project inside eclipse - first project name 'one' inside that
ForwardingServ1 -
ServletContext context1=context.getContext("/myapp1"); // for this create new
project with the same name
RequestDispatcher rd=context1.getRequestDispatcher("/FirstServ"); // create servlet
with same name as this
run the ForwardingServ1 inside one project - while running server - configured both
servlets and then run it
and
rd.forward(request,response);
include response - second scenario - i can paritally process the request but i know
partial response of other servlet so i will include
everything regarding logic is decided in main servlet - developer of first servlet
.
create a dynamic web project - create two IncluderServ and IncludedServ and then
run IncluderServ
inluderserv - code only using RequestDispatcher will not clear what to do include
or forward
using RequestDispatcher include method - includes the response of other servlet
inside current servlet - response in the sense object
difference imp - forward() - execution completes for first sevlet
include() - execution remains intact and doesn't get terminated and all the
statamenst after that will be printed.
client billserv
shopserv
submit done
when will click on submit - control goes to hiddenServ1 dopost and collected in
str1 and str2. We have Hidden component inside Html tag form action and inside that
we have specified hidden type input tag and create form with two hidden componenets
containing value and when click on done control goes to hiddenServlet 2 and print
these values.
form values are passed to servlet 1 and after that it is send to servlet 2
hidden fields - drawbacks - a) They can be seen with "view source" b) you have to
generate html dynamically inside the servlet
2) custom cookies -
create new project - and add CustomCookieServ servlet and paste from sir's file
discussion - file cookie concept
Cookie is a class Http package, cookies are used to store info , they can store
only textual information and cannot store java objects, cookies can be rejected by
client browser. cookies are always added inside response and retrieved from
request.
cookies drawback - cookies can be rejected by browser , cannot store java objects,
seen by users
customcookie - flow
or
SessionServ1 - Session will be created first and attribute will be set as key and
value so in another servlet we are able to retrieve session values
session life is limited to browser and session is for whole project
Internal working :-
SessionServ1
HttpSession session = request.getSession(); // or get Session(true);
session.setAttribute("book","Value");
sessionServ2
HttpSession sessioon =request.getSession(false);
pw.println(session.getAttribute("book"));
open folder URLRewriting - create a new project and create first servlet