Servlet is J2EE server driven technology to create web applications in java. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets.
The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services
#
CGI vs Servlet
Earilier we have the CGI (Common Gateway Interface), but its have some drwback. Its create a new seperate process for each an every request.so its hgh weight componanent. So its get slow performance.
This drwaback overcome by servlet. because for each and every request its create seperate thread. So all thread running under same process. so we no need to create seperate process for each and every request.so its ligh weight and provide fast performance.
Servlet technology was introduced to overcome the shortcomings of CGI technology.
* Servlets provide better performance that CGI in terms of processing time, memory utilization because servlets uses benefits of multithreading and for each request a new thread is created, that is faster than loading creating new Object for each request with CGI.
* Servlets and platform and system independent, the web application developed with Servlet can be run on any standard web container such as Tomcat, JBoss, Glassfish servers and on operating systems such as Windows, Linux, Unix, Solaris, Mac etc.
* Servlets are robust because container takes care of life cycle of servlet and we don’t need to worry about memory leaks, security, garbage collection etc.
* Servlets are maintainable and learning curve is small because all we need to take care is business logic for our application.
Client - Its request something request. That request called HTTP request. HTTP is protocol. everything via web. So this request called HTTP request. Ex: Web Browser
Server= Its a machine serve the request which made by client. its responsible for response.The server search the request page stored in repository. Then server picks one of the static page from repository and provide the reponse to the client. This is called HTTP response. Everything via web so its called HTTP response.
Key elements of HTTP Request
-> HTTP Method (Action to be performed) ( Get() or Post())
-> The Page to access(a URL) (Ex. gmail.com)
-> Form Parameters -> what ever you send across server along with request is called Form Parameters.(Ex; user name and password)
Key elements of HTTP Response
-> Status code( whether the request success or not. ex Status code 200- sucessful, 401 - unsuccessful)
-> Content type (this for what kind of content type (type of file)server respond to client. so that client working accordingly. if content type is .jar, so browser (client) says to save the file to user)
-> content (server sent actual content file. Ex: .Jar file)
when the servlet comes into picture?
-> Normally web server provide the reponse only form of static page. its not in dynamic page.
(Login yahoo page is static page for all user. so this is static page. But we have inbox page differently for every users.so this is dynamic page.
-> client need dynamic content always. so servlet handle this request.Servlet is form of java class, but without main()
what is Web container (serve Engine)?
-> The web server always provide the static page. Its not have the capable to provide the response for dynamic pages. So web server provide the dynamic web pages with help of the servlet. So we need communication between web browser and servlet. The web container(Web Engine) provide interface between and provide the communication between web browser and servlet.
Client --> Get(request) -> Web Browser -> <- Web container -> <- servlet
-> The servlet lives in web container always. Web container is responsible for invoking methods in servlet.
-> Normally the client send the request to web browser -> wen browser send request to web container -> the web container is decide which servlet needs to be invoked to provide the response.
How the web container handle a request?
-> The client send the HTTP request to the web browser then the web browser send HTTP request to the web container.
-> Here the web container should not send same HTTP request to servlet. Becasue the sevlet is java program. its understand only object, not HTTP request.
-> so web container change the HTTP request to request object and responnse object and form the seperate thread for each and every HTTP request.
-> Then web container is responsible for which method we need to invoke in the servlet. its may doGet() or doPost().
-> we write the code and all buisness logic in doPost and doGet() for create the dynamic web pages. sevlet the prcoess according to create the code in doPost() and doGet() to create dynamic webpage.
-> Then finally the servlet send the response object into the web container. Then web container change the response object into HTTP request and send to the web browser.
-> So web container is have below two responsible
a) invoke the method in servlet
b) provide communication between server and servlet.
Roles of web container:
Jsp and servlets ar live and die with in web container.
-> Communication Support - provide communication between server and servelt
-> lifecycle management -> its call method in servelt (init(), destroy())
-> Multi threading support - > For every request its form as thread to servlet.
-> Security-> client cannot contact with servelt deirectly. we can give number of security in web container.So valid request only goes to servlet.
-> JSP support
How container knows which servlets needs to be invoked
-> We hav the file web.xml is called web container. This is also called Deployment Descriptor.
-> The all servlet should have entry in this web.xml file. its have below format
<web-app>
<servlet>
<servlet-name>login_serv</servlet-name>
<servlet-class>com.login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login_serv</servlet-name>
<url-pattern>/logon</url-pattern>
</servlet-mapping>
</web-app>
-> its identify the servlet by using below method
a) client send the URL pattern for all page. Ex:gmil/logon. here /logon is url pattern send by client.
b) using <servlet-mapping> tag its identify the servlet name with match with url pattern.
c) Then its find the servlet name in <servlet> and execute the mehod present in servlets.
No comments:
Post a Comment