1. Concept
    1. container
      1. Communications support
      2. Lifecycle Management
        1. load
        2. instance
        3. invoke
        4. garbage collection
      3. Multithreading Support
        1. create new java thread for every servlet request
        2. when the servlet's done running the http service method,the thread completes
      4. Declarative Security
        1. XML config
      5. JSP Support
        1. translate JSP to real Java
    2. CGI
    3. HTTP
      1. Method
        1. POST
          1. complex request
          2. hide parameters in the message body not url
          3. message can be large
          4. not idempotent
          5. to change something on the server
        2. GET
          1. just simple request
          2. has not body,only headers
          3. exposed
          4. length limited
          5. idempotent
          6. just getting information
      2. MIME type
        1. Content-Type:text/html
    4. URL
    5. Servlet Life Cycle
    6. listeners
      1. ServletContextAttributeListener
      2. ServletRequestListener
      3. ServletRequestAttributeListener
      4. ServletContextListener
      5. HttpSessionListener
      6. HttpSessionBindingListener
      7. HttpSessionAttributeListener
      8. HttpSessionActivationListener
    7. attribute
      1. servletContext.setAttribute("dog",d);
      2. request.setAttribute("userList",userList);
      3. an attribute is an object set into one of the three other servlet API objects
      4. 返回Object类型,必须转换
      5. API
        1. Object getAttribute(String name)
        2. void setAttribute(String name,Object value)
        3. void removeAttribute(String name)
        4. Enumeration getAttributeNames()
    8. Scopes
      1. Context
        1. everyone in the application has access
        2. isn't thread-safe
          1. synchronize the ServletContext
      2. Request
        1. accessible to only those with access to a specific ServletRequest
        2. only request attributes and local variables are tread-safe
      3. Session
        1. accessible to only those with access to a specific HttpSession
        2. the client could open a new browser window
        3. SingleThreadModel
  2. Servlet
    1. container handle request
      1. see a request for servlet
      2. create HttpResponse,HttpRequest
      3. find the requested servlet
        1. real name and internal name
          1. <servlet>
        2. internal name and url
          1. <servlet-mapping>
      4. create or allocate a thread for the request
      5. pass the request and response to the servlet thread
      6. call the servlet's service() method
        1. call doGet()
        2. or call doPost()
        3. support both get and post
      7. put something in the response
      8. thread completes
      9. container converts response object to HTTP response
      10. send it back to client
      11. delete request and response object
    2. parameters
      1. jsp get from serlvet
        1. request.getAttribute("userList")
      2. servlet get from jsp
        1. request.getParameter("color")
          1. RequestDispatcher
        2. request.setAttribute("userList",userList)
        3. String one=request.getParameterValues("sizes")[0]
        4. String[] sizes=request.getParameterValues("sizes");
    3. servlet's life
      1. load class
      2. instantiate servlet
      3. init()--call only once
        1. get database connection
        2. register your own object
        3. a servlet’s service() method will not run until the servlet is fully initialized.
        4. access
          1. ServletConfig
          2. used to access ServletContext
          3. ServletContext
          4. used to access web app parameters
      4. service()--each request runs in a separate thread
        1. thread
      5. destroy()--call only once
    4. request object
      1. getParameter
        1. String[] sizes=request.getParameterValues("sizes");
        2. request.getParameter("color")
        3. String one=request.getParameterValues("sizes")[0]
      2. getHeader
        1. String client=request.getHeader("User-Agent");
      3. getCookies
        1. Cookie[] cookies=request.getCookies();
      4. getSession
        1. HttpSession session=request.getSession();
      5. getMethod
        1. String httpMethod=request.getMethod();
      6. getInputStream
        1. InputStream input=request.getInputStream();
      7. getHeader
        1. String forwards=request.getHeader("Max-Forwards");
        2. int forwardsNum=request.getIntHeader("Max-Forwards");
      8. getRemotePort
        1. get the client's port
      9. getServerPort
        1. to which port was the request originally SENT
      10. getLocalPort
        1. on which port did the request END UP
      11. handle multiple clients
    5. response object
      1. setContentType
        1. text/html
        2. application/pdf
        3. video/quicktime
        4. application/java
        5. image/jpeg
        6. application/jar
        7. application/octet-stream
        8. application/x-zip
      2. getOutputStream
        1. ServletOutputStream out=response.getOutputStream();
        2. out.write(aByteArray);
      3. getWriter
        1. PrintWriter writer=response.getWriter();
        2. writer.println("some text and HTML");
        3. has a reference to OutputStream,and decorates to character
      4. setHeader
        1. overwrites the existing value
      5. addHeader
        1. adds an additional value
      6. sendRedirect
        1. response.sendRedirect("http://www.oreilly.com");
        2. http://www.wickedlysmart.com/myApp/cool/bar.do
          1. http://www.wickedlysmart.com/myApp/cool/foo/stuff.html
          2. http://www.wickedlysmart.com/foo/stuff.html
          3. relative to the root of this web container
          4. foo is a web app,separate from the myapp web app
        3. you can't write to the response and then call sendRedirect()
        4. sendRedirect() takes a String, not a URL object
        5. it's like asking the client(the browser) to call someone else instead
      7. request Dispatch
        1. do the work on the server
        2. RequestDispatcher view=request.getRequestDispatcher("result.jsp"); view.forward(request,response);
        3. it's like asking a co-worker to take over working with a client
    6. deployment descriptor
      1. servlet initialization parameters
        1. <init-param> <param-name>adminEmail</param-name> <param-value>likewecare@wickedlysmart.com</param-value> </init-param>
        2. getServletConfig().getInitParameter("adminEmail");
        3. only for servlet
      2. the servlet init parameters are read only once
      3. many init parameters
        1. Enumeration e=getServletConfig().getInitParameterName();
        2. while(e.hasMoreElements()){ e.nextElement(); }
      4. setting a request attribute only for the jsp to which you forwarded the request
    7. context init parameter
      1. <context-param> <param-name>adminEmail</param-name> <param-value>clientheaderror@wickedlysmart.com</param-value> </context-param>
      2. getServletContext().getInitParameter("adminEmail");
      3. getServletConfig().getServletContext().getInitParameter("adminEmail");
      4. available to JSP and Servlet
      5. ServletContextListener
        1. setting
          1. put it in your WEB-INF/classes
          2. config in dd to tell the container
    8. thread-safe
      1. yes
        1. Request-scoped attributes
        2. Loacl variables in service methods
      2. no
        1. Context-scoped attributes
        2. Session-scoped attributes
        3. Instance variables in the servlet
        4. Static variables in the servlet
    9. RequestDispatcher
      1. get from ServletRequest
        1. RequestDispatcher view=request.getRequestDispatcher("result.jsp");
      2. get from ServletContext
        1. RequestDispatcher view=getServletContext().getRequestDispatcher("/result.jsp");
    10. HttpSession
      1. hold conversational state across multiple requests from the same client
      2. session ID
        1. exchange Session ID info
          1. server set-cookie in http header
          2. client send cookie in http header
      3. HttpSession session=request.getSession();
        1. sending a session cookie in the response
          1. generate the session id and cookie for the response
          2. cause a cookie to be sent with the response
        2. gettting the session id from the request
          1. if the request includes a session id cookie
          2. else if there's no session id cookie or don't match
        3. session.isNew()
          1. true-->the client has not yet responded with this session id
      4. HttpSession session=request.getSession(false);
        1. return a pre-existing session or null if there was no session associated with this client
        2. if (session==null) session=request.getSession();
      5. if the client doesn't accept cookies
        1. it ignores set-cookie response header
        2. use URL rewriting as a backup
          1. tell the response to encode the URL
          2. HttpSession session=request.getSession(); response.encodeURL("/test.do");
          3. response.encodeRedirectURL("/test.do");
          4. URL rewriting will happen automatically if cookies don't work with the client
      6. methods
        1. getCeationTime()
        2. getLastAccessedTime()
        3. setMaxInactiveInterval()
        4. getMaxInactiveInterval()
        5. invalidate()
          1. remove all attributes
      7. session die
        1. it times out
          1. <session-config> <session-timeou>15</session-timeout> </session-config>
          2. setting for specific session
          3. session.isNew()
        2. call invalidate() on the session object
          1. session.invalidate(); you can't call session.getAttribute("foo");
        3. the application goes down(crashes or is undeployed
        4. session cookies vanish when the client's browser quits,but you can tell a cookie to persist on the client even after the browser shuts down
      8. distributed
        1. move from one vm to another, not copy
        2. HttpSessionActivationListener lets attributes prepare for the big move
      9. listeners
        1. HttpSessionBindingListener
          1. valueBound
          2. valueUnbound
          3. binding for class attribute
        2. HttpSessionListener
          1. sessionCreated
          2. sessionDestroyed
        3. HttpSessionAttributeListener
          1. attributeAdded
          2. attributeRemoved
          3. attributeReplaced
          4. use HttpSessionBindingEvent
          5. binding for attribute
        4. HttpSessionActivationListener
          1. sessionWillPassivate
          2. sessionDidActivate
    11. Cookie
      1. create
        1. Cookie cookie=new Cookie("username",name);
      2. setting
        1. cookie.setMaxAge(30*60);30 mimutes
        2. while -1 for session will never expired
      3. send to client
        1. response.addCookie(cookie);
        2. 对比addHeader() add new value if exists ;setHeader() replace the exist value
      4. get from client request
        1. Cookie[] cookies=request.getCookies();
  3. JSP
    1. syntax
      1. comment
        1. <!--html comment -->
        2. <%-- jsp comment --%>
      2. init parameters
        1. <jsp-file>/TestInit.jsp</jsp-file>
          1. config
          2. get
        2. application.getInitParameter("javax.sql.DataSource");
      3. Directive
        1. page
          1. <%@ page import="foo.*,java.util.*" %>
          2. for special instructions to container
          3. attributes
          4. import
          5. import java.lang.*,javax.servlet,javax.servlet.http for free
          6. isThreadSafe
          7. true--implement the SingleThreadModel
          8. contentType
          9. defines the MIME type
          10. isELIgnored
          11. to ignore EL expressions or not
          12. isErrorPage
          13. defines whether the current page represents another JSP's error page
          14. default false,if true, the page has access to the implicit exception object
          15. errorPage
          16. defines a URL to the resource to which uncaught Throwables should be sent
          17. others
          18. language
          19. extends
          20. session
          21. defines whether the page will have an implicit session object
          22. buffer
          23. defines how buffering is handled by the implicit JspWriter
          24. autoFlush
          25. defines whether the buffered output is flushed automatically
          26. info
          27. defines a string that gets put into the translated page
          28. pageEncoding
          29. defines the character encoding for the JSP
        2. taglib
          1. <%@ taglib tagdir="/WEB-INF/tags/cool" prefix="cool" %>
        3. include
          1. <%@ include file="wickedHeader.html" %>
      4. Scriptlet
        1. <% out.println(Counter.getCount());%>
          1. within the service method
      5. Expression
        1. <%=Counter.getCount()%>
          1. it become the argument to an out.print();
      6. Declaration
        1. <%! int count=0; %>
          1. for declaring members of the generated servlet class
          2. add to the class outside the service method
          3. variable
          4. method
      7. Action
        1. <jsp:include page="foo.html" />
    2. translate to servlet
      1. work
      2. variable
      3. what the container do
        1. looks at the directives
        2. create an HttpServlet subclass
          1. for tomcat
        3. if import write import statements
        4. if declarations,write into the class file
        5. build service method
          1. it's called by the servlet superclass's overridden service() method
          2. declares and initializes all the implicit objects
          3. out
          4. JspWriter
          5. request
          6. HttpServletRequest
          7. response
          8. HttpServletResponse
          9. session
          10. HttpSession
          11. application
          12. ServletContext
          13. config
          14. ServletConfig
          15. exception
          16. Throwable
          17. pageContext
          18. PageContext
          19. page
          20. Object
          21. combines the html,scriptlets and expressions into the service method
        6. translate to java code
        7. compile to class code
      4. API
        1. jspInit()
          1. called from the init() method
        2. jspDestroy()
          1. called from the servlet destroy() method
        3. _jspService()
          1. called from the servlet's service() method
        4. can be overridden
    3. life cycle
      1. translation and compilation
      2. load jsp class
      3. instantiates the servlet and invoke jspInit()
      4. create a new thread to handle request
      5. invoke _jspService() method
      6. send back response or forwards
    4. attributes
      1. scope
      2. pageContext
    5. standard actions
      1. bean
        1. declare and initialize a bean attribute
          1. if not found create
          2. polymorphic bean references
          3. if type is used without class,the bean must already exist
          4. if class is used the class must not be abstract,and must have a public no-arg constructor
          5. type x=new class()
        2. get bean property
        3. set bean property
          1. set bean only not found
          2. it convert the string to destination type for you
        4. param
          1. html与bean不一致
          2. 如果一致则无需写param
          3. 如果所有属性均赋值则
      2. <jsp:include page="Header.jsp" />
        1. don't put opening and closing HTML and body tags within your reusable pieces
        2. param
          1. pic
        3. position
          1. Topic
        4. difference to directive
          1. 每次获得都是最新的,但是会影响性能
          2. the container is creating a RequestDispatcher from the page attribute and applying the include() method
          3. difference with directive include
        5. /
      3. <jsp:forward>
        1. <jsp:forward page="HandleIt.jsp" />
        2. nothing you write before before the forward will appear if the forward happens,it cleared the buffer
    6. EL Expression
      1. config
        1. <%@ page isELIgnored="true" %>
      2. ${} <c:out value="${user.phone}" />
        1. detail
        2. array
        3. string index
      3. get other info
        1. ${header["host"]}
        2. ${pageContext.request.method}
        3. request.setAttribute("foo.person",p)
        4. ${cookie.username.value}
        5. ${initParam.mainEmail}
      4. function
        1. write a java class with a public static method
        2. write a Tag Library Descriptor(TLD) file
        3. put a taglib directive in your JSP
        4. use EL to invoke the function
        5. 概要
      5. operator
        1. pic
      6. null-friendly
        1. in arithmetic-->treat null as zero
        2. in logical -->treat null as false
        3. if not found still display
    7. JSP Standard Tag Library
      1. install
        1. jstl.jar and standard.jar
      2. <c:out>
        1. <c:out value='${pageContent.currentTip}' escapeXml='false' />
        2. html special characters
        3. <c:out value='${user}' default='guest' />
      3. <c:forEach>
      4. <c:if>
      5. <c:choose>
      6. <c:set>
        1. setting attribute variables
        2. setting bean properties or Map values
        3. defalut scope is page
      7. <c:remove>
      8. <c:import>
        1. difference
          1. pic
        2. customer
      9. <c:url>
        1. for url rewriting
        2. query string
          1. pic
      10. <c:catch>
      11. customer
        1. URI
          1. a name,not a location
          2. container look for
        2. TLD
          1. <rtexprvalue>
          2. tag file
          3. tag
          4. tag-body
          5. tag-file-body
          6. find
          7. difference: tags and tag file
          8. pic
          9. Topic
          10. Topic
          11. Topic
        3. tag handler
          1. simple
          2. extends SimpleTagSupport
          3. sequence
          4. invoke null
          5. dynamic attribute
          6. classic
          7. lifecycle
          8. pic
          9. BodyTagSupport
    8. Error handle
      1. config in dd
        1. pic
      2. config in jsp page
        1. pic
        2. error page
          1. get ${pageContext.exception}
      3. <c:catch>
  4. Deploy
    1. structure
      1. pic
    2. config
      1. index
        1. pic
      2. error
        1. pic
      3. servlet initialization
        1. pic
    3. security
      1. authentication
        1. define roles
        2. define resource/method constraints
          1. <auth-constraint>
        3. types
          1. BASIC
          2. DIGEST
          3. CLIENT-CERT
          4. FORM
          5. pic
      2. authorization
      3. confidentiality
      4. data integrity
    4. filter
      1. example
        1. pic