General Summary
– Servlets are efficient, portable, powerful, and widely
accepted in industry
– Regardless of deployment server, run a free server on
your desktop for development
– Using Eclipse (or another IDE like NetBeans or IntelliJ
IDEA) greatly simplifies development and deployment
– Strongly consider JSF 2 as an alternative for new projects
Servlet Basic Summary
Main servlet code goes in doGet or doPost:
– The HttpServletRequest contains the incoming
information
– The HttpServletResponse lets you set outgoing
information
• Call setContentType to specify MIME type
• Call getWriter to obtain a Writer pointing to client (browser)
• Make sure output is legal HTML
• Give address with @WebServlet or web.xml
@WebServlet("/some-address")
public class SomeServlet extends HttpServlet { … }
• Resulting URL
30
Form Data Summary
• Make a form: <form …> … </form>
– Relative URL for “action”. Textfields need “name”.
Should always have submit button.
• Read data: request.getParameter("name")
– Results in value as entered into form, not necessarily as
sent over network. I.e., not URL-encoded.
• Check for missing or malformed data
– Missing: null or empty string
– Special case: query data that contains special HTML
characters
• Need to be filtered if query data will be placed into
resultant HTML page
48
Request Header Summary
HTTP is important
– Many servlet tasks can only be accomplished by making use of
HTTP headers coming from the browser
• Use request.getHeader for arbitrary header
– Remember to check for null
• Shortcuts discussed later
– Cookies, authorization info, content length, and content type
have shortcut methods
• Most important headers you read directly
– Accept
– Accept-Encoding
– Connection
– Referer
– User-Agent 26
Status Code Summary
HTTP is important
– Many servlet tasks can only be accomplished with HTTP
status codes
• Setting status codes
– Redirect user with response.sendRedirect(someURL)
• If you insert user-supplied data into the URL, encode with
URLEncoder.encode
– Send 404 error pages with sendError
– In general, set via response.setStatus
• Most important status codes
– 200 (default)
– 302 (forwarding; set with sendRedirect)
– 401 (password needed)
– 404 (not found; set with sendError)
Response Header Summary
HTTP is important
– Many servlet tasks can only be accomplished through use of HTTP
response headers
• Setting response headers
– In general, set with response.setHeader
– In special cases, set with response.setContentType,
response.setContentLength, response.addCookie, and
response.sendRedirect
• Most important response headers you set directly
– Cache-Control and Pragma
– Content-Disposition
– Content-Encoding
– Content-Length
– Expires
– Refresh
– WWW-Authenticate
23
Cookie Summary
Basic functionality
– Cookies involve name/value pairs sent from server to
browser and automatically returned when the same page
(or possibly same site or domain) is visited later
• Cookies let you
– Track sessions (use higher-level session-tracking API)
– Permit users to avoid logging in at low-security sites
– Customize sites for different users
– Focus content or advertising
• Setting cookies
– Call Cookie constructor, set age, call response.addCookie
• Reading cookies
– Call request.getCookies, check for null, look through
array for matching name, use associated value
39
Session Tracking Summary
Summary
• Sessions do not travel across network
– Only unique identifier does
• Get the session
– request.getSession
• Extract data from session
– session.getAttribute
• Do typecast and check for null
• If you cast to a generic type, use @SuppressWarnings
• Put data in session
– session.setAttribute
• Custom classes in sessions
– Should implement Serializable
35
JSP Summary
JSP is more convenient, not more powerful
– JSP makes it easier to create and maintain HTML, while
still providing full access to servlet code
• JSP pages get translated into servlets
– It is the servlets that run at request time
– Client does not see anything JSP-related
• You still need to understand servlets
– Understanding how JSP really works
– Servlet code called from JSP
– Knowing when servlets are better than JSP
– Mixing servlets and JSP
• Other technologies use similar approach,
– But aren’t as portable and don’t let you use Java for the
“real code”
31
pdfs from coreservelts.com
No comments:
Post a Comment