Preview
In this article we discuss:
- MVC
- JSP(JSTL)
- Servlet
- Test
MVC
- Both Servlet(print HTML) and JSP(embed Java in HTML: expression, scriptlet, declaration) can create web pages without the other.
- In MVC pattern, JSP handles the presentation view, generate dynamic web pages, while Servlet focuses on the business logic.
- Comparsion of JSP and Serlet when creating pages alone:
JSP | Servlet |
---|---|
HTML file with .jsp extension. Contains static HTML. JSP to generate HTML. Has built-in JSP objects |
Java class file. Generate all HTML. More steps to access web objects. |
Benefits of MVC
- Minimize the HTML code in Servlet.
- Minimize the business logic in JSP.
Interaction between Controller and Model
- Define an object together with helper classes to fetch or modify data from the database as data model.
- Pass the dataSource to DB model in the controller to perform database pooling injection.
- Call method of DB model in controller to fetch or modify data from the database, avoid the direct interaction between controller and database. This achieves good code logic, make code easily maintained and ensure the safety of database.
Interaction between Controller and View
- Servlet access the data from
request
. - Servlet can forward the data to JSP using
Dispatcher
. - Servlet can add data to request object.
- JSP access the data through
request
or JSTL(brief grammar).${data}$
- JSP can redirect to Servlet using
response.sendRedirect(<thePageUrl>)
. - User can not see the change of url when using
forward
, but can see when usingredirect
.
1 | // Servlet |
JSP(Java Server Pages)
JSP Tags
- Custom JSP Tags(Another way of avoiding heavy business logic in JSP) and JSTL.
- JSP Tags are reusable.
- JSP Tags minimize the scriptlet in JSP.
JSTL(Java Standard Tag Library), I18N
JSTL Core Tags(prefix c)
- Include a reference for core tags. (uri not need to connect to the Internet)
- Loop over an array variable:
1 | <c:forEach var="tmpCity" items="${myCity}"> |
JSTL Function Tags
- Include a reference for function tags.
- Length of a string:
1 | <c:set var="data" value="ericsayshi"/> |
- Split string into array:
1 | <c:set var="citiesArray" value="${fn:split(data, ',')}"/> |
I18N
- JSTL uses Formatting Message labels and locale to achieve internationalization:
fmt:setLocale
,fmt:bundle
,fmt:message
- Resource Files(translated version of labels)
- Achieve locale from user’s selection or
pageContext.request.locale
1 | <!-->JSP will append locale after the basename automatically.<--> |
State Management
Session
is stored in the server whileCookie
is stored in user’s web browser.
1 | response.addCookie(...); |
Servlet
Form Processing
- HTML Form:
name="xxx"
,method="GET"
ormethod="POST"
. - Servlet access data through:
request.getParameter("xxx")
.
doGet() and doPost()
- Form data appears as
name-value
pair in the URL ofdoGet()
method: theUrl?field1=value1&field2=value2… - Form data is stored in the body of
request
(header and body) indoPost()
method.
doGet() | doPost() |
---|---|
URL can be bookmarked or emailed. Good for debugging. Limitations on data length. |
URL can not be bookmarked or emailed. No limitations on data length. Can send binary data. Form data won’t appear in the url. |
Configuration Parameters Reading
- Store parameters in
WEB-INF/web.xml
.
1 | ServletContext context = getServletContext(); |
- Define parameter per Servlet: use
<servlet>
,<servlet-mappings>
tags in web.xml; do not use Annotations@WebServlet
in servlet file.