4. Client server architecture
Server: typically a application for large
computers which can handle multiple request
simultaneously, can process much faster. The
application of server is written in such a way
that it cannot be access by the user directly
but it waits for another program to connect and
then it process the request as need by the
user.
5. Client server architecture
Client: an application that runs on simple
desktop computers. It has an attractive user
interface but itself does not have the data to
manipulate instead it takes input from the
user, connects the server and the server
responds to the request as need by the client
using this application.
6. HTTP protocol
HTTP(Hyper Text Transfer Protocol ) is a
communication protocol for displaying HTML
pages inside web browser.
HTTP are stateless protocol because it treats
each request independently and have no
knowledge of where the request have came
from.
A stateless protocol treats each request and
response pair as an independent transaction.
Default port no of HTTP is 80.
7. HTTP protocol
The communication between the web browser
and the server are usually done using two
methods:-
1. GET.
2. POST.
GET method is used to request data from a
specified resource.
POST method is used to submit data from a
specified resource.
8. HTTP protocol
GET methods are cached but POST method
cannot be cached.
GET methods can be bookmarked but POST
methods cannot be bookmarked.
GET methods have length restrictions where as
POST methods have no length restrictions.
GET methods remains in history of the web
browser whereas the POST methods are not
present in the history of the web browser.
GET methods are idempotent but POST methods
are non-idempotent.
9. HTTP protocol
The other HTTP methods are:-
1. Trace
2. Put
3. Options
4. Head
5. Delete
10. Introduction to Servlet
Servlet are Java class files which resides on
the web server which produces dynamic
output web pages when a client request for the
particular Servlet.
So to execute a Servlet we need a compiler
and a JVM machine inside a web server same
as when we need to compile and run a simple
java program inside our desktop.
11. Introduction to Servlet
To deploy our servlet inside the web server we
need a servlet container.
Servlet container is a java virtual machine
inside web server which provides compilation
and run time execution of servlet.
“Apache tomcat” is an example of popular
servlet container.
13. J2ee container and architecture
Containers are used for deploying and
execution service provided by the component.
The various types of container in J2EE:-
A. Applet container
B. Application client container
C. EJB container
D. Servlet container
14. Servlet Life Cycle
When the client request for the servlet through
the web browser the Servlet container loads
the servlet for the first time, it calls the init()
method. Before the servlet can respond to any
request the init() method need to be finished or
the servlet need to be initialize. After the init()
method the servlet container marked the
servlet as available.
15. Servlet Life Cycle
For each request form the client the servlet
container calls the service method on the
servlet which is going to process the request.
The service method can use all the resources
which are marked as available in the init()
method.
The destroy() method is called to clean up the
resources when the server shuts down or
there is a lack of resources. It also calls the
save the current state information of the
servlet which will be used when the servlet
again called for the next time.
16. Functions of servlet container
Manage servlet life cycles.
Register a servlet for one or more URLS.
Encode MIME base response.
Decode MIME base request.
Handles HTTP protocols along with other
protocols(ftp,pop,smtp,telnet,etc).
Provide network services with request and
responses cycles.
17. Servlet API
The java servlet API consist of two Packages
which helps in implementation of the servlet:-
1. javax.servlet
2. javax.servlet.http
18. Servlet API
javax.servlet:- It provides the core functionality
of the servlet, this API consist of classes and
interfaces which is generic and protocol
independent.
javax.servlet.http:- This API consist of classes
and interfaces which are HTTP protocol
specific.
20. SERVLET API
There are two types of servlet available by default
which has been defined in servlet API
1. GenericServlet
2. HttpServlet
GenericServlet : it defines the classes and
interfaces which are protocol independent and
generic which is define on the javax.servlet
package. It defines simple life cycle methods
such as init, service, destroy. To write generic
servlet we simply have to override the abstract
service() method.
21. SERVLET API
Signature of GenericServlet:-
public abstract class GenericServlet extends
java.io.lang implements
Servlet, ServletConfig, java.io.seriliziable
22. SERVLET API
HttpServlet :- it defines classes and interfaces
which are http protocol specific. It extends the
GenericServlet class therfore it inherits the
GenericServlet methods.
Signature of HttpServlet:-
Public abstract class HttpServlet extends
GenericServlet implements java.io.sereliziable
23. Example of servlet
To write our own servlet we have to extend the
HttpServlet class.
Public class our_servlet extends HttpServlet {
Public void doGet(HttpServletRequest req,HttpServletResponse
resp)throws IOException,ServletException{
//code for servlet
//the logic of the application
}
}
24. Example of servlet
teddy_test.java
Public void display extends HttpServlet{
Public void doGet(HttpServlet response, HttpSrvlet request)throws
IOException,ServletException{
out.println(“<HTML>”);
out.println(“< BODY>”);
response.setContentType(“text/html”);
PrintWriter out=resp.getWriter();
out.println(“<p>Hello World</p>”);
out.println(“</ BODY>”);
out.println(“< /HTML>”);
}
}
25. Example of servlet
Reading Form Data using Servlet:-
Servlets handles form data parsing automatically using the following
methods depending on the situation:
getParameter(): -You call request.getParameter() method to get the
value of a form parameter.
getParameterValues(): -Call this method if the parameter appears
more than once and returns multiple values, for example checkbox.
getParameterNames(): -Call this method if you want a complete
list of all parameters in the current request.
26. Example of servlet
To get information from an html page
Let assume there are two text box present in
html page name “teddy_test.html”
<form ACTION=“teddy_servlet.java”>
Name:<input type=“text” name=“nme”>
Age:<input type=“text” name=“age”>
<input type=“submit”>
27. Example of servlet
teddy_servlet.java
Public void display extends HttpServlet{
Public void doGet(HttpServlet response, HttpSrvlet request)throws
IOException,ServletException{
out.println(“<HTML>”);
out.println(“< BODY>”);
response.setContentType(“text/html”);
Printwriter out=resp.getWriter();
String name=req.getParameter(“name”);
String age=req.getParameter(“age”);
out.println(“<p>name:-”+name+”age:-”+age+”</p>”);
out.println(“</ BODY>”);
out.println(“< /HTML>”);
}
}
28. Session Management
Techniques
As HTTP is a stateless protocol it cannot
remember the same user over multiple page
request and treats the same user as different
user.
So, if a user request for a page the server
responds when the same user request for the
another page the server thinks as another
user.
The value or the state of the page need to be
retained when we purchasing goods from
merchant sites or keep us out of daily login to
a particular sites.
29. Session Management
Techniques
The are methods to retain this value over
many pages:-
1. Cookies.
2. Hidden from fields
3. Url rewriting
4. Http Session
30. Session Management
Techniques
Cookies are textual information which are
stored on the client file system.
Cookies are send by the web server to the
web browser.
Cookies contain cookie name and cookie
value.
Advantages:-
1. They are simple to maintain
2. They are maintained by the client no over
head of the server.
31. Session Management
Techniques
Disadvantage:-
1. It will not work if it is disabled by the web
browser.
2. Easy to tamper and one person can
impersonate as another user.
Syntax:-
Cookie ck_object=new
Cookie(“cookie_name”,cookie_value);
32. Session Management
Techniques
Hidden from fields, in HTML we have to type
an extra field called “hidden” which will not be
shown to the user by the web browser but it
will pass information to the web server. It
contains value from the previous request
which will going to process the future request.
33. Session Management
Techniques
Advantage:-
1. To process it we do not require any special
type of server.
2. It will work where cookies will be disable by
the web browser.
Disadvantage:-
1. If an error occurred before the values are
saved then the values are lost.
35. Session Management
Techniques
Url rewriting, it a technique of appending
values to the end of the url, so that when the
user clicks on the link the values of the current
page will be passed through the url.
Advantage:-
1. No extra form submission is required as in
hidden from fields.
36. Session Management
Techniques
Disadvantage:-
1. It can only pass textual information.
2. Work with link only.
Syntax:-
http://url? name1=value1&name2=value2
Appends value on the end of the url where
parameters are separated by „&‟ and
name, value pairs are separated by „=„.
37. Session Management
Techniques
HttpSession object is completely maintained
and created by the Web server. To achieve this
there is HttpRequest.getSession() method.
This method returns true if there the session is
created or creates a session before returning.
It is used to identify the user across multiple
pages and they can live up to specific time
bound by the server.
38. Session Management
Techniques
Syntax:-
HttpSession ses_object=request.getSession();
ses_object.setAttribute(“ses_name”,ses_value);
//to set the session on the web server.
ses_object.getAttribute(“ses_name”);
// to get the value of the session from the web server.
39. Servlet Collaboration
Servlet collaboration is a technique of sharing
information between the servlets.
In servlet collaboration on servlet pass the
information directly to the another servlet.
To pass the information directly one servlet
need to know about the other.
40. Servlet Collaboration
The servlet collaboration can be achieved by
following methods :-
1. forward
2. include
3. sendRedirect
41. Servlet Collaboration
Forward and include is the method of
RequestDispatcher interface.
Syntax:-
RequestDispatcher
rd=response.getRequestDispatcher(“url”);
rd.forward(request,reponse);
sendRedirect is the method of
HttpServletResponse interface
Syntax:-
response.sendRedirect(“url of the redirect
servlet”);
42. Servlet Collaboration
Difference between sendRedirect and forward:-
The sendRedirect method is used to send the
forwarded address to resources in a new domain
or server where as the forward method is used to
send the forwarded address to resources inside
the server.
In case of forward method the servlet container
takes cares of everything, the client and browser
is not involved, whereas in case of sendRedirect
method the forwarded address is sent to the
browser of the client.
43. Servlet Collaboration
In forward, the old request and response is
send along with the forwarded address, the old
request is going to process the new request. In
case of sendRedirect the old request and
response is lost and a new response is
created by the web server and treated as a
new request by the browser.
44. Servlet Collaboration
In forward the forwarded address in not seen
by the client; its is transparent whereas in
sendRedirect the url is present in the url bar of
the web browser.
The forward method is fast than the
sendRedirect as it does not require an extra
round trip.
45. CGI(Common Gateway Interface) allows the application server to
call an external program which is going to process the client http
request and respond to it. It creates a new process for each
request made by the client.
What is CGI?
46. Servlet Vs. CGI
CGI(Common Gateway Interface) scripts
creates a separate process for each request
made by the client where as servlet is initialize
only once and creates threads for each
request, so CGI scripts are lead to overhead
when there is rise in user.
CGI are more prone to attacks than servlet.
CGI scripts are executed to native to operating
system where as servlet are compiled to java
byte code which can run anywhere.
47. Servlet Vs. CGI
CGI are platform dependent whereas servlet
are platform independent.
Servlet handling of request us much faster
than CGI.