Download presentation
Presentation is loading. Please wait.
Published byGillian Willis Modified over 8 years ago
1
Servlet V.B.Sanghavi
2
Overview of Servlet to create web application resides at server side and generates dynamic web page V.B.Sanghavi
4
What is a Servlet? Technology – to create web application API -- many interfaces and classes. interface -- implemented for creating any servlet. class-- extend the capabilities of the servers and respond to the incoming request web component -- deployed on the server to create dynamic web page. V.B.Sanghavi
5
CGI(Commmon Gateway Interface) V.B.Sanghavi
6
Disadvantage of CGI If number of clients increases, it takes more time for sending response. For each request, it starts a process and Web server is limited to start processes. It uses platform dependent language e.g. C, C++, perl. V.B.Sanghavi
7
Servlet Model V.B.Sanghavi
8
Advantages of Servlet The web container creates threads for handling the multiple requests to the servlet. better performance: because it creates a thread for each request not process. Portable and secure: because it uses java language. Robust: Servlets are managed by JVM so no need to worry about memory management, garbage collection etc. V.B.Sanghavi
9
Servlet Terminology V.B.Sanghavi
10
(1) HTTP Allows web servers and browsers to exchange data over the web. It is a request response and application-level protocol. Http uses reliable TCP connections by default on TCP port 80. It is stateless server doesn't recognize the user bydefault. V.B.Sanghavi
11
(2) Http Request Methods GET POST HEAD PUT DELETE OPTIONS TRACE Method Structure V.B.Sanghavi
13
(3) Web Container and Web Server It provides runtime environment for JavaEE (j2ee) applications. Responsible for initializing, invoking and managing the lifecycle of Servlet and JSP. Web Server is a running program or software that provides service. V.B.Sanghavi
14
(4) Deployment Descriptors Manage the configuration of web application. web.xml USE: 1.Initialize the parameters for servlets and web applications. 2.Servlet/JSP Definitions 3.Servlet/JSP mappings 4.Session Config 5.welcome-page, error-pages V.B.Sanghavi
15
(5) Content-type text/html application/msword application/jar application/pdf application/x-zip images/jpeg V.B.Sanghavi
16
Servlet public void init(ServletConfig config) public void service(ServletRequest request,ServletResponse response) public void destroy() public ServletConfig getServletConfig() public String getServletInfo() V.B.Sanghavi
17
Generic Servlet implements Servlet, ServletConfig and Serializable interfaces previous mathods public ServletContext getServletContext() public String getInitParameter(String name) public Enumeration getInitParameterNames() public String getServletName() public void log(String msg) V.B.Sanghavi
18
Http Servlet extends the GenericServlet class and implements Serializable interface previous methods protected void doGet(HttpServletRequest req, HttpServletResponse res) protected void doPost(HttpServletRequest req, HttpServletResponse res) V.B.Sanghavi
19
Program There are three ways to create the servlet. By implementing the Servlet interface By inheriting the GenericServlet class By inheriting the HttpServlet class V.B.Sanghavi
20
Servlet Config and Sevlet Context V.B.Sanghavi
21
Config public interface ServletConfig -implemented by the servlet container to initialize a single servlet using init() -initialization parameters are set to the servlet using the web.xml V.B.Sanghavi
22
Example Hello Topic Advance JAVA V.B.Sanghavi
23
Advantage you don't need to edit the servlet file. it is easier to manage the web application if any specific content is modified from time to time. V.B.Sanghavi
24
How to get the object of ServletConfig public ServletConfig getServletConfig(); Ex: ServletConfig config=getServletConfig(); V.B.Sanghavi
25
Methods public String getInitParameter(String name) public Enumeration getInitParameterNames() public String getServletName() public ServletContext getServletContext() public ServletConfig getServletConfig() V.B.Sanghavi
26
Example For one parameter : String s1=getServletConfig.getInitParameter (“topic"); V.B.Sanghavi
27
Context public interface ServletContext implemented by the servlet container for all servlet to communicate with its servlet engine like an application global variable mechanism for a single web application The ServletContext object is contained within the ServletConfig object. V.B.Sanghavi
28
Topic Advance JAVA V.B.Sanghavi
29
Advantage Easy to maintain if any information is shared to all the servlet. we don't need to modify the servlet. V.B.Sanghavi
30
Usage to get configuration information from the web.xml file to set, get or remove attribute from the web.xml file provide inter-application communication V.B.Sanghavi
31
ServletContext Method public void setAttribute(String name,Object object) public Object getAttribute(String name) public void removeAttribute(String name) V.B.Sanghavi
32
How to get the object of ServletContext interface public ServletContext getServletContext() EX: ServletContext s = config.getServletContext(); ServletContext s=getServletContext(); String s1=getServletContext.getInitParameter (“topic"); V.B.Sanghavi
33
HttpServletRequest and HttpServletResponse interface V.B.Sanghavi
34
HttpServletRequest GET and POST method Servlet container recognizes request and passes to particular method. To encapsulate all Http-based Req. info., including headers and req.methods. V.B.Sanghavi
35
It includes : Role of Form data Form data and parameters Headers V.B.Sanghavi
36
Role of Form data http://commerce.rediff.com/commerce/v2/dis playbag.jsp?carttype=books http://commerce.rediff.com/commerce/v2/dis playbag.jsp?carttype=books Carttype=books ------- Form data V.B.Sanghavi
37
Form data and parameters public String getParameter(String name) public String[] getParameterValues(String name) public Enumeration getParameterNames() V.B.Sanghavi
38
Http Header Client - send info. Regarding which software is being used, what file types the browser is capable of receiving etc. -getHeader(String name) -getHeaders(String name) -getHeaderNames() EX: Accept, Accept-Language, Host, User- Agent, Connection, etc. V.B.Sanghavi
39
HttpServletResponse Set Http Response header, set content –type or redirect Http requests to another URL. 1) setHeader( String name, String value) 2) getWriter () or getOutputStream() to write the content. 3) setContentType(String name) 4) sendRedirect (Stirng url) V.B.Sanghavi
40
sendRedirect() and Request Dispatcher Request Dispatcher only when the other servlet to which the request is being forwarded lies in the same application. Send Redirect can be used in both the cases if the two servlets resides in a same application or in different applications. V.B.Sanghavi
41
sendRedirect() response.sendRedirect("http://www. google.com"); V.B.Sanghavi
42
Request Dispatcher RequestDispatcher rd=req.getRequestDi spatcher("servlet2"); rd.forward(request, response); //method may be include or forward V.B.Sanghavi
43
RequestDispatcher Methods: public void forward(ServletRequest request,ServletResponse response)throws ServletException, IOException public void include(ServletRequest request,ServletResponse response)throws ServletException, IOException V.B.Sanghavi
46
Example V.B.Sanghavi
47
Session Handling V.B.Sanghavi
48
Session Collection of HTTP requests shared between a client and web server over a period of time. Set the lifetime of a session. Stateless protocol :HTTP V.B.Sanghavi
49
Session Tracking Techniques Cookies Hidden Form Field URL Rewriting HttpSession V.B.Sanghavi
50
Cookie small piece of information that is persisted between the multiple client requests. a name, a single value, and optional attributes such as a comment, path,a maximum age, and a version number V.B.Sanghavi
51
HTTP Header HTTP/1.1 200 OK Date: Fri, 04 Feb 2000 21:03:38 GMT Server: Apache/1.3.9 (UNIX) PHP/4.0b3 Set-Cookie: name=xyz; expires=Friday, 04-Feb- 07 22:03:38 GMT; path=/; domain=tutorialspoint.com Connection: close Content-Type: text/html V.B.Sanghavi
52
How Cookie Works? public void addCookie(Cookie ck) public Cookie[] getCookies(): V.B.Sanghavi
53
Constructor and Methods Cookie() Cookie(String name, String value) public void setName(String name) public void setValue(String value) public void setMaxAge(int expiry) public String getName() public String getValue() V.B.Sanghavi
54
Create Cookie Cookie ck=new Cookie("user",“abc"); response.addCookie(ck); //adding cookie in the response V.B.Sanghavi
55
Get Cookies Cookie ck[]=request.getCookies(); for(int i=0;i<ck.length;i++) { out.print(" "+ck[i].getName()+" "+ck[i].get Value()); } V.B.Sanghavi
56
Servlet Cookie Program V.B.Sanghavi
57
URL Rewriting we append a token or identifier to the URL of the next Servlet or the next resource. It will work only with links. url ? name1=value1 & name2=value2 V.B.Sanghavi
59
Ex: out.print(" vi sit "); V.B.Sanghavi
60
HttpSession interface container creates a session id for each user. The container uses this id to identify the particular user V.B.Sanghavi
62
Methods public HttpSession getSession() public HttpSession getSession(boolean create) public String getId() public long getCreationTime() public long getLastAccessedTime() public void invalidate() V.B.Sanghavi
63
Ex: In first servlet--- HttpSession session=request.getSession(); session.setAttribute("uname",n); In second servlet--- HttpSession session=request.getSession(); String n=(String)session.getAttribute("uname"); V.B.Sanghavi
64
Hidden Form Field hidden (invisible) textfield is used for maintaining the state of an user Better---need not to depend on browser.(maintained at server) V.B.Sanghavi
66
out.print(" "); V.B.Sanghavi
67
ServletFilter object that is invoked at the preprocessing and postprocessing of a request. to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc. servlet filter is pluggable ---> less maintenance V.B.Sanghavi
69
Filter API Filter FilterChain FilterConfig V.B.Sanghavi
70
1) Filter interface 1)public void init(FilterConfig config) 2) public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain) 3) public void destroy() V.B.Sanghavi
71
doFilter method Examining the request Carrying out i/p filtering by wrapping the req. object Carrying out o/p filtering by wrapping the res. Object Invoking the next filter in the filter chain by calling chain.doFilter() or forwarding. V.B.Sanghavi
72
2) Filter Config public String getFilterName() public String getInitParameter(String name) public Enumeration getInitParameterNames() public ServletContext getServletContext() V.B.Sanghavi
73
3) Filter Chain responsible to invoke the next filter or resource in the chain. public void doFilter(HttpServletRequest request, HttpServletResponse response) V.B.Sanghavi
74
How to define a filter?......... //servlet-name V.B.Sanghavi
75
public class SimpleServletFilter implements Filter { public void init(FilterConfig c) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain fobj) throws IOException, ServletException { } public void destroy() { } } V.B.Sanghavi
76
THANK YOU V.B.Sanghavi
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.