Servlets
Package : javax.servlet.*; javax.servlet.http.*
.Servlets is a technology from Sun which is used to develop Servers Side components. Servlets as a server side component is responsible to recieve the request from the client and process the request and send the response back to client.
.Web browser is a client for servlet. Webbrowser is used to send the request and us responsible to take the response given by the webserver.
.WebServer is responsible to recieve the request given by the webbrowser and sends the response back to webbrowser.
.Webbrowser and webserver can talk each other using http protocol.
.The request what we send from client to server is called HTTPRequest and the response coming back from server to client is called HTTPResponse.
.HTTP is a stateless protocol
.HTTP build upon TCP/IP. TCP is responsible to ctrl the transmission of data. IP is an underlying protocol which is responsible to carry the data.
WebServers :
1) Apache Tomcat
2) net dynamics 3) Lighttd
App Servers :
1)Weblogic
2)WebSphere
3)JBOSS
4)Jetty
5)Glassfish
http://docs.oracle.com/javaee/7/api/overview-summary.html
Difference ?
ApplicationServer contains both web container and EJB Container where as Web Server contains only web container.
A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.
WebServer supports servlets and jsp only. where as Application Server supports all j2EE technologies.
Servlet LifeCycle methods?
1) init()
2) Service()
3) Destroy()
init() will be called by the container. it will be called only once. init() method will be used to initialize the servlet instance and will be called by the container only once in the life cycle.
service() method contains the code to process the client request. This will be called by the container for each request.
destroy() is used to destroy the service instance. Before destroying the instance code clean up will be triggered to close the connections.
HttpRequest : contains 2 parts Headers & Body
HttpResponse : contains 2 parts Header & Body
Request Methods:
Get
Post
Delete
Option
Trace
Put
get() and post() used to send the request request from Client to Server.
ServletContext :
ServletContext is an object which contains the info about entire application. Servlet Context will be created by container at the server startup
1 JVM – 1 tomcat – 5 applications – all will be shared by one context
<context-param>
<param-name>admin-email</param-name>
<param-value>admin@email.com</param-value>
</context-param>
ServletConfig sc = getServletConfig()
ServletContext sctx = sc.getServletContext();
sctx.getInitParameter("admin-email");
ServletConfig :
is an object created by the container when u send the first request to a servlet. Each servlet will have its own servlet config
<init-param>
<param-name>company</param-name>
<param-value>bellinfo</param-value>
</init-param>
getServletConfig().getInitParameter("city");
Servlet Life Cycle in details:
When you give the first request to the servlet the following things will be done by container
1) Container identifies the Servlet Class for the given request with the help of url pattern in web.xml file
2) Identified servlet class will be loaded in to the main memory
3) Servlet instance will be created by the containter by calling the default construtor.
4) ServletConfig object will be created and this object will be initialized with any parameters specified in servlet tag in web.xml
5) ServletContext object will be associated with Servlet config.
note: ServletContext already created by the container at Server startup and initailized with context param specified in web.xml
6) Container invokes init() method by passing servlet config as parameter.
7) A thread will be created to serve the given request (Thread is for service instance)
8) HttpServletRequest and HttpServletResponse objects will be created by the container.
9) Service method will be invoked with Request and Response as parameters
10)The request will be processed and response data will be placed in to the output stream after flushing the stream.
11)Request and Response objects will be destroyed and thread will killed.
From the second request onwards, 1, 7, 8, 9, 10 steps will be triggered.
There are 3 scopes in servlets
1)request scope -> can be accessed with in one request and response scenarios only
2)session scope -> can be accessed with across multiple request and response by the single client. Each client will have its own session
3)context scope -> can be accessed across multiple request and response by multiple clients. i.e all the client will have only one context.
Parameters vs Attributes
Types of parameters:
1)request parameters
2)config parameters
3)context parameters
Setting the parameters will be done by the container
Types of Attributes:
1)Request Attributes
2)Session Attributes
3)Context Attirbutes
– We have to set the attributes explicitly in to request, session and context object, then only you can get the attributes from the object
–> When do you call getSession()?
1) Verify Session is avaliable or not.
2) If avaliable, use the avaliable session
3) if not, create the cookie object
4) generate the session Id
5) set this to cookie
6) add the cookie to the response
Client side persistent object is nothing but a cookie.
If we want to store data in client machine then we can use cookie.
Cookies will be created by the container by default, User can create as many cookies as they want.
We can do the Session Management with the following ?
1.HttpSession
2.Cookies
3.URL Writing
4.Hidden Files
When you call the getSession() method first time session id will be generated by container and will be stored in JSessionId.
Container uses cookies to send this jsession id to client machine
.get the cookie
cookie[] ck = req.getCookie();
.create a cookie
Cookie ck = new Cookie();
.Add to the response.
res.addCookie();
MVC Architecture(modal 2) (Modal View Controller)
MVC is a defacto standard for developing web applications.
In MVC, presentationlogic will be separated & placed in a separate component called JSP(JSP modifiication doesn't required any redeploy or recompile of the application.Server will take care of it)
In MVC, we can easily separate presentation, business and persistent logic and make the components reusable.