1. Implement Servlet interface
2. Extends GenericServlet
3. Extends HttpServlet
Servlet Functions:
void | destroy() Called by the servlet container to indicate to a servlet that the servlet is being taken out of service. |
ServletConfig | getServletConfig() Returns a ServletConfig object, which contains initialization and startup parameters for this servlet. |
java.lang.String | getServletInfo() Returns information about the servlet, such as author, version, and copyright. |
void | init(ServletConfig config) Called by the servlet container to indicate to a servlet that the servlet is being placed into service. |
void | service(ServletRequest req, ServletResponse res) Called by the servlet container to allow the servlet to respond to a request. |
init: only run one time when the very first request
destory: only run once when web server stop webapp
service: run once per request
HttpServlet Functions:
doGet
doPost
urlPatterns:
using wildcard '*' character
1: start with "/" ends with "/*"
2. "*.extension" (no "/" at beginning)
url mapping rules:
1. looking for more specific mapping first (only count from left to right)
2. "*.extension" have lower priority than others(based on 1's left to right rule)
HttpServletResponse
setContentType("text/html")
setCharacterEncoding("utf-8")
setCharacterEncoding()
Servlet Config xml
<servlet>
<init-param>
<param-name></param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup> : servlet init() when load, value is call sequence
</servlet>
<context-param> </context-param> :
Or
@WebServlet(name="InitServlet", urlPatterns="/InitServlet", loadOnStartup=1)
To override init or destroy function, for site open/close actions,
remember to set that servlet loadOnStartup, in case that servlet never been called
ServletConfig object
this.getServletConfig().getInitParameter("encoding")
Enumeration<String> names = getServletConfig().getInitParameterNames();
while(names.hasMoreElements()){
String name = names.nextElement();
...
}
Redirection
Sendredirect
request.getRequestDispatcher("/Login").forward(request, response);
No comments:
Post a Comment