如题所述
ä¸ãservletçé ç½®
servletæ两ç§é
ç½®çæ¹æ³
1.å¨web.xmlæ件ä¸é
ç½®
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.hwua.servlet.LoginServlet</servlet-class></servlet><servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/login</url-pattern></servlet-mapping>
2.å¨servletä¸ç¨Java注解çæ¹å¼é
ç½®
使ç¨æ³¨è§£é
ç½®Servletçæ¥éª¤:
1. å¨Servletç±»ä¸è´´@WebServlet注解2. å¨web.xmlæ件ä¸è¿è¡å¦ä¸çé
ç½®
metadata-complete="true":表示æ¯å¦å¿½ç¥Servletä¸é¢ç注解
metadata-complete="false"表示æ¯å¦ä¸å¿½ç¥Servletä¸é¢ç注解,缺çå¼
äºãservletçå½å¨æ
public void init(ServletConfig config):åå§åæ¹æ³,å¨ç¬¬ä¸æ¬¡è¯·æ±æ¶è°ç¨,åªå¨æåçæ¶åè°ç¨ä¸æ¬¡.
public void service(ServletRequest req, ServletResponse res):æå¡æ¹æ³
public void destroy():éæ¯æ¹æ³
ä¸ãå¨web.xmlæ件ä¸è®¾ç½®/è·ååå§ååæ°
1.ä½ç¨åå½åçservlet åå¨servletæ ç¾ä¸
<init-param><param-name>name</param-name>
<param-value>admin</param-value></init-param>
è¿å»åæ° å¨initæ¹æ³ä¸
String name = config.getInitParameter("name");2.ä¸ä¸æåæ° åå¨web-appæ ç¾ä¸
<context-param><param-name>driver</param-name>
<param-value>oracle.jdbc.OracleDriver</param-value></context-param>
è·ååæ° å¨initæ¹æ³ä¸
ServletContext servletContext = config.getServletContext();String initParameter = servletContext.getInitParameter("driver");
åã设置servletå¯å¨çæ¶é´
é»è®¤æ
åµä¸servletæ¯å¨ç¬¬ä¸æ¬¡è¢«è¯·æ±æ¶å¯å¨ç
éè¿å¨web.xmlæ件ä¸è®¾ç½®
注æï¼
load-on-startup çåæ°æ¯å¤§äºçäº0ç è´æ°æ²¡æææload-on-startup å¿ é¡»å¨åæ°init-paramæ ç¾çä¸é¢ å¦åinit-paramä¼å¤±æäºã常ç¨æ¹æ³:
String getContextPath():è·åä¸ä¸æè·¯å¾,<Context path="ä¸ä¸æ" ../>String getHeader(String headName):æ ¹æ®æå®ç请æ±å¤´è·å对åºç请æ±å¤´çå¼. String getRequestURI():è¿åå½æ请æ±çèµæºå称. ä¸ä¸æè·¯å¾/èµæºå
StringBuffer getRequestURL():è¿åæµè§å¨å°åæ çå 容 String getRemoteAddr():è¿å请æ±æå¡å¨ç客æ·ç«¯çIP
å ãè·å请æ±åæ°çæ¹æ³:
String getParameter(String name):æ ¹æ®åæ°å称,è·å对åºåæ°çå¼.String[] getParameterValues(String name):æ ¹æ®åæ°å称,è·å该åæ°çå¤ä¸ªå¼.Enumeration<String> getParameterNames():è·åææ请æ±åæ°çååMap<String,String[]> getParameterMap():è¿å请æ±åæ°ç»æçMapéå.
key:åæ°å称
value:åæ°å¼,å°è£ å¨Stringæ°ç»ä¸.