L03 - Servlet
L03 - Servlet
Servlet
JSP/SERVLET
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
1 2
Servlet Servlet
̵ Java Servlets are programs that run on a Web ̵ Easy to create content
or Application server and act as a middle ̵ Easy to build big application
layer between a request coming from a Web
browser or other HTTP client and databases or ̵ Easy to upgrade
applications on the HTTP server. ̵ Easy to use (client)
̵ Run in server tier.
̵ Managed by the Web container
3 4
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
3 4
10/9/2019
SERVLET
ENGINE
HTML
CONTENT
HTTP RESPONSE
5 6
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
5 6
Servlet Servlet
̵ The lifecycle of a servlet is controlled by the ̵ The container invokes the service method,
container in which the servlet has been passing request and response objects. Service
deployed. When a request is mapped to a methods are discussed in Writing Service
servlet, the container performs the following Methods.
steps. ̵ If it needs to remove the servlet, the container
̵ If an instance of the servlet does not exist, the finalizes the servlet by calling the servlet's
web container: destroy method. For more information, see
Loads the servlet class Finalizing a Servlet.
Creates an instance of the servlet class ̵ When a servlet is first started up, its init(
Initializes the servlet instance by calling the init ServletConfig config) method is called
method (initialization is covered in Creating and init should perform any necessary initializations
Initializing a Servlet) 7
init is called only once, and does not need to be thread-safe 8
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
7 8
10/9/2019
Servlet Servlet
̵ Every servlet request results in a call to
service(ServletRequest request, ServletResponse
response)
service calls another method depending on the type of service
requested
Usually you would override the called methods of interest, not
service itself
service handles multiple simultaneous requests, so it and the
methods it calls must be thread safe
̵ When the servlet is shut down, destroy() is called
destroy is called only once, but must be thread safe (because
other threads may still be running)
9 10
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
9 10
11 12
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
11 12
10/9/2019
13 14
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
13 14
15 16
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
15 16
10/9/2019
17 18
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
17 18
19 20
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
19 20
10/9/2019
21 22
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
21 22
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
23 24
10/9/2019
HTTP POST requests supply additional data from of a URL is limited (maximum URL length
is 2048 characters)
the client (browser) to the server in the message Restrictions on Only ASCII characters allowed No restrictions. Binary data is also allowed
body data type
Security GET is less secure compared to POST POST is a little safer than GET because the
because data sent is part of the URL parameters are not stored in browser history or
Never use GET when sending passwords in web server logs
or other sensitive information!
25 26
Visibility Data is visible to everyone in the URL Data is not displayed in the URL
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
25 26
Servlet Servlet
̵ Every servlet must implement the
javax.servlet.Servlet interface.
̵ HTTP servlet usually overrides doGet() to
handle GET requests and doPost() to handle
POST requests. An HTTP servlet can override
either or both of these methods, depending on
the type of requests it needs to handle.
27 28
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
27 28
10/9/2019
Servlet HttpServletRequest
̵ doGet(HttpServletRequest req, ̵ The HttpServletRequest represents the client’s
HttpServletResponse resp) - Called by the request. This object gives a servlet access to
server (via the service method) to allow a information about the client, the parameters
servlet to handle a GET request. for this request, the HTTP headers passed
along with the request
̵ doPost(HttpServletRequest req,
HttpServletResponse resp) - Called by the
server (via the service method) to allow a
servlet to handle a POST request.
29 30
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
29 30
HttpServletRequest HttpServletRequest
̵ getAttribute(String name) - Returns the ̵ getRequestDispatcher(String path) -
value of the named attribute as an Object, or Returns a RequestDispatcher object that acts
null if no attribute of the given name exists. as a wrapper for the resource located at the
̵ getParameter(String name) - Returns the given path.
value of a request parameter as a String, or ̵ setCharacterEncoding(String env) -
null if the parameter does not exist. Overrides the name of the character encoding
̵ getParameterValues(String name) - used in the body of this request.
Returns an array of String objects containing ̵ getSession() - Returns the current session
all of the values the given request parameter associated with this request, or if the request
has, or null if the parameter does not exist. does not have a session, creates one.
31 32
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
31 32
10/9/2019
33 34
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
33 34
35 36
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
35 36
10/9/2019
37 38
39 40
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
39 40
10/9/2019
41 42
43 44
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
43 44
10/9/2019
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
45 46
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
47 48
10/9/2019
Servlet Fitter
̵ Value using in all page ̵ A filter is an object that is invoked at the
<context-param>
preprocessing and postprocessing of a request.
<param-name>title</param-name> ̵ It can also intercept the response and do post-
<param-value>Shop đồ trẻ em</param-value>
</context-param> processing before sending to the client in web
application.
̵ Using value ̵ Servlet Filters are pluggable java components
Servlet: that we can use to intercept and process
ServletConfig config = getServletConfig(); requests before they are sent to servlets and
ServletContext context = config.getServletContext();
String value = context.getInitParameter("title"); response after servlet code is finished and
before container sends the response back to
In jsp page the client.
String value = application.getInitParameter("title"); 49 50
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
49 50
̵ void destroy()
51 52
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
51 52
10/9/2019
request.setCharacterEncoding("utf8");
response.setCharacterEncoding("utf8");
arg2.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws
ServletException {}
} 53 54
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
53 54
Q&A
DEMO
55 56
08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56 08/2019 KHOA CNTT - NLU LẬP TRÌNH WEB PHAN ĐÌNH LONG 56
55 56