• Servlet là một chương trình được viết bằng Java chạy trên máy chủ Web. • Nó được thực thi theo yêu cầu HTTP của khách hàng (tức là của trình duyệt) và tạo một tài liệu (thường là tài liệu HTML) sẽ được trả lại cho khách hàng bởi người phục vụ.
Trang 1Chapter 8
Servlets
Contents
•Servlet basics
•Setting up the servlet API
•Creating a Web Application
•The Servlet URL and the Invoking web page
•A servlet is a program written in Java that runs on a Web server
•It is executed in response to a client’s (i.e., a browser’s) HTTP request and creates
a document (usually an HTML document) to be returned to the client by the
server
•It extends the functionality of the server, without the performance limitations
associated with CGI (Common Gateway Interface) programs
•A servlet is Java code that is executed on the server, while an applet is Java code
that is executed on the client As such, a servlet may be considered to be the
server- side equivalent of an applet
•However, Java’s servlet API is not part of Java SE (Standard Edition), though it is
included in Java EE (Enterprise Edition)
This means that non-Enterprise users must download an implementation of the
Java servlet API
Trang 28.2 Setting up the servlet API
•Install Apache Tomcat web server
8.2 Setting up the servlet API
•Eclipse IDE for Enterprise Java Developers
•https://www.eclipse.org/downloads/packages/
8.3 Creating a Web Application
Trang 38.3 Creating a Web Application
•Note: The web.xml must have <servlet> and <servlet-mapping> tags
that identify the associated Java class file and the servlet’s URL
location (relative to the web application) respectively
•These <servlet> and <servlet-mapping> tags will have exactly the
same structure for any other servlet
8.4 The Servlet URL and the Invoking web page
•Recall that a servlet will be executed on a Web server only in
response to a request from a user’s browser
•As noted in the previous section, each servlet must be held in folder
8.4 The Servlet URL and the Invoking web page
•But it is much more common for a servlet to be called from a
preceding HTML page
•This is usually achieved by the use of an HTML form, with the form’s
METHOD attribute specifying either ‘GET’ or ‘POST’ and its ACTION
attribute specifying the address of the servlet
•The servlet in last slide may then be invoked via the ACTION attribute
of a FORM tag in a preceding HTML page as follows:
<FORM METHOD=GET ACTION="FirstServlet">
•Note that the URL for the servlet is relative to the Web application
that contains both the servlet and the HTML page
Trang 48.4 The Servlet URL and the Invoking web page
• To keep things as simple as possible for the time being, we shall start off with a Web page that calls up a servlet without actually
sending it any data.
<FORM METHOD=GET ACTION="FirstServlet">
<INPUT TYPE="Submit" VALUE = "Click me!">
•Servlets that use the HTTP protocol (which means all servlets, at the
present time) must extend class HttpServlet from package
8.5 Servlet structure
•The two most common HTTP requests (as specified in the HTML
pages that make use of servlets) are GET and POST
•At the servlet end, method service will dispatch either method
doGet or method doPost in response to these requests The
programmer should override (at least) one of these two methods
•All three methods ( doGet , doPost and service ) have a void return
type and take the following two arguments:
an HttpServletRequest object;
an HttpServletResponse object.
Trang 58.5 Servlet structure
•All three methods ( doGet , doPost and service ) have a void return type and take
the following two arguments:
an HttpServletRequest object;
an HttpServletResponse object.
•The former (the first parameter) encapsulates the HTTP request from the browser
and has several methods, but none will be required by our first servlet
•The second argument holds the servlet’s response to the client’s request
•There are just two methods of this HttpServletResponse object that are of
interest to us at present :
• void setContentType(String <type>): This specifies the data type of the
response Normally, this will be “ text/HTML”
• PrintWriter getWriter(): Returns the output stream object to which the servlet
can write character data to the client (using method println ).
8.5 Servlet structure
•There are four basic steps in a servlet:
1 Execute the setContentType method with an argument of “ text/HTML ”.
2 Execute the getWriter method to generate a PrintWriter object.
3 Retrieve any parameter(s) from the initial Web page (Not required in our first
servlet.)
4 Use the println method of the above PrintWriter object to create elements of the
Web page to be ‘served up’ by our Web server.
•The above steps are normally carried out by doGet or doPost
•Note that these methods may generate IOException s and ServletException
s, which are checked exceptions (and so must be either thrown or handled
locally)
•Note also that step 4 involves a lot of tedious outputting of the required
HTML tags
8.5 Servlet structure
•Finally, as of Tomcat 7, a WebServlet annotation tag is required before
the opening line of the servlet class This tag indicates the name of
the servlet and the path to it (relative to the classes folder) and has
the following format:
•@WebServlet(“/<Path>/<ServletName>”)
Trang 6•The previous example was very artificial, since no data was passed by the
initial form and so there was no unpredictability about the contents of the
page generated by the servlet
•Let’s modify the initial form a little now, in order to make the example
rather more realistic…
<FORM METHOD=GET ACTION="PersonalServlet">
Enter your first name:
<INPUT TYPE="Text" NAME="FirstName" VALUE="">
<BR><BR>
<INPUT TYPE="Submit" VALUE="Submit">
</FORM>
Trang 78.7 Passing data
•It is now appropriate to consider the methods of HttpServletRequest that
are responsible for handling values/parameters received by servlets
•There are three such methods:
• String getParameter(String <name>)
Returns the value of a single parameter sent with GET or POST
• Enumeration getParameterNames()
Returns the names of all parameters sent with POST
• String[] getParameterValues(String <name>)
Returns the values for a parameter that may have more than one value
8.7 Passing data
•Sửa lại ví dụ phần 8.4
• Code: page 235 (347 OF 389), file PersonalServlet.java
8.7 Passing data
•One potential problem with this method is that, if the browser’s
‘Back’ button is clicked to return to the opening Web page, the initial
name entered is still visible
•This doesn’t really matter in this particular example, but, for other
(repeated) data entry, it probably would
•In order to overcome this problem, we need to force the browser to
reload the original page, rather than retrieve it from its cache, when a
return is made to this page
Trang 88.7 Passing data
•There is an HTML META tag that will do this, but the tag varies from
browser to browser However, the following set of tags will satisfy
most of the major browsers:
<META HTTP-EQUIV="Pragma" CONTENT="no cache">
<META HTTP-EQUIV="Cache-control" CONTENT="no cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
•These should be placed immediately after the <HEAD> tag on the
initial Web page
8.7 Passing data
•Force page reload when back button is used
8.7 Passing data
•Explanation
Trang 98.7 Passing data
•Continuing now with the approach of gradually adding to the
complexity of our servlets, the next step is to carry out some
processing of the data entered and display the results of such
processing
•The next example accepts two numbers, adds them and then displays
the result
•Since there are multiple inputs, we shall use the POST method.
•In addition, an HTML table has been used for laying out the page
elements neatly
8.7 Passing data
• Code for html: page 237 (249 of 389)
• Code for servlet: page 238 (250 of 389)
• AdderServlet
8.7 Passing data
•Explanation
Trang 108.8 Sessions
•One fundamental restriction of HTTP is that it is a stateless protocol
That is to say, each request and each response is a self-contained and
independent transaction
•However, different parts of a Web site often need to know about data
gathered in other parts
•For example, the contents of a customer’s electronic cart on an
e-commerce shopping site need to be updated as the customer visits
various pages and selects purchases
•To cater for this and a great number of other applications, servlets
implement the concept of a session
8.8 Sessions
•A session is a container where data about a client’s activities may be
stored and accessed by any of the servlets that have access to the
session object
•The session expires automatically after a prescribed timeout period
(30 min for Tomcat) has elapsed or may be invalidated explicitly by
the servlet (by execution of method invalidate ).
8.8 Sessions
•A session object is created by means of the getSession method of
class HttpServletRequest This method is overloaded:
HttpSession getSession()
HttpSession getSession(boolean create)
•If the first version is used or the second version is used with an
argument of true , then the server returns the current session if there
is one; otherwise, it creates a new session object For example:
HttpSession cart = request.getSession();
•If the second version is used with an argument of false , then the
current session is returned if there is one, but null is returned
otherwise
Trang 118.8 Sessions
•A session object contains a set of name-value pairs
•Each name is of type String and each value is of type Object
•Note that objects added to a session must implement the
Serializable interface (This is true for the String class and for the type
wrapper classes such as Integer )
•A servlet may add information to a session object via the following
method:
void setAttribute(String <name>, Object <value>)
8.8 Sessions
•Example
String currentProduct = request.getParameter("Product");
HttpSession cart = request.getSession();
Trang 128.8 Sessions
•To retrieve a value, use:
Object getAttribute(String <name>)
•Note that a typecast will usually be necessary after retrieval For
•Full example application: This example involves a simplified shopping
cart into which the user may place a specified weight of apples and/or
a specified weight of pears
•Three servlets are used, for the following purposes:
•selection of apples/pears;
•entry of required weight;
•checking out
•If one servlet needs to transfer execution to another, then method
sendRedirect of class HttpServletResponse may be used
Trang 138.8 Sessions
•Html code: 242 (254 of 389)
8.8 Sessions
•When a selection has been made and the user has clicked ‘Submit’,
the Selection servlet is executed Before we look at the code for this
servlet, there is an apparent minor problem (that turns out not to be
a problem at all) that needs to be considered…
•As you are aware by now, a servlet builds up a Web page by
outputting the required HTML tags in string form via method println
of class PrintWriter This means, of course, that any string literals
must be enclosed by speech marks For example:
println("<HTML>");
8.8 Sessions
•However, the next servlet needs to output a FORM tag with an ACTION
attribute specifying the address of another servlet
•This address, if we follow the convention from our previous examples, will
already be enclosed by speech marks
•If we try to include both sets of speech marks, then an error will be
generated, since what is intended to be opening of the inner speech marks
will be taken as closure of the outer speech marks Here is an example of
such invalid code:
out.println("<FORM METHOD=POST ACTION="AnyServlet"");
•One solution to this apparent problem is to use inverted commas, instead
of speech marks, for the inner enclosure:
out.println("<FORM METHOD=POST ACTION=AnyServlet");
Trang 148.8 Sessions
•However, provided that we have no spaces within the address that we
are using, we do not actually need either speech marks or inverted
commas to enclose the address, so the following is perfectly
acceptable:
•out.println("<FORM METHOD=POST ACTION=AnyServlet");
•If we wish to enclose any attributes explicitly, though, we must use
inverted commas
8.8 Sessions
•The code for the Selection servlet is at page 244 (256 of 389)
8.8 Sessions
•As an alternative to the use of sendRedirect to transfer control to another
servlet (or HTML page), we can create a RequestDispatcher object and call
its forward method.
•Example
RequestDispatcher requestDispatcher =
request.getRequestDispatcher("Checkout");
requestDispatcher.forward(request, response);
•Provided that the user did not select ‘Checkout’ on the initial page [See
later for coverage of this], the Web page shown in Fig 8.8 is presented As
can be seen, a weight has been entered by the user
Trang 15•Once all product selections have been made and the ‘Checkout’
option has been taken, the Checkout servlet will be executed
•Before we look at the code for this servlet, though, we need to
consider the issue of formatting decimal output , since the Checkout
servlet needs to show costs to precisely two decimal places and to
allow a sensible maximum field size
•We can’t use printf , since this is a member of the PrintStream class,
not of the PrintWriter class
•However, the PrintWriter class does have the equivalent method
format and it is this method that we shall use.
8.8 Sessions
•Checkout servlet code is at page 249 (261 of 389)
Trang 168.8 Sessions (Flow explanation)
Weight
8.8 Sessions (session variable)
•Biến này chứa các cặp string – object, ngoài ra thì nó xử lý phần trọng
lượng hàng như thế nào?
•Session ở ví dụ này chính là giỏ hang (cart) với string là tên loại quả và
object là số lượng (tính bằng kg)
•Việc thêm và bớt được thực hiện trong servlet Weight sau khi nhập
trọng lượng vào Selection
8.8 Sessions
•Session variables allow much more interesting and dynamic Web sites
to be created
•However, they do not allow a user’s personal details/preferences to
be maintained between visits to the same site The next section will
show how this may be done
Trang 178.9 Cookies
•Cookies provide another means of storing a user’s data for use whilst
he/she is navigating a Web site
•Whereas sessions provide data only for the duration of one visit to the site,
though, cookies store information that may be retrieved on subsequent
visits to the site (In actual fact, Session objects make use of Cookie
objects.)
•They can be used to personalize pages for the user and/or select his/her
preferences
•Cookies have been used by CGI programmers for years and the developers
of Java’s servlet API incorporated this de facto standard into the servlet
specification
8.9 Cookies
•A cookie is an associated name-value pair in which both name and value
are strings (E.g., “username” and “Bill Johnson”.)
•It is possible to maintain a cookie simply for the duration of a browsing
session, but it is usually stored on the client computer for future use
•Each cookie is held in a small file sent by the server to the client machine
and retrieved by the server on subsequent visits by the user to the site
•The constructor for a Java Cookie object must have this signature:
Cookie(String <name>, String <name>)
(Note that there is no default constructor.)
8.9 Cookies
•Once a cookie has been created, it must be added to the
HttpServletResponse object via the following HttpServletResponse
method :
void addCookie(Cookie <name>)
•For example:
response.addCookie(myCookie);