J2EE|servletè????¤??¨
servletè???¤?¨
1. Servletè???¤?¨?o??
Servletè???¤?¨?ˉServlet????§?1???¨?3?????è|?¨?¥??????oé?¨??????ˉ?|??? ?è???¤???¤?-?¨?·???é????-?-?Servletè???¤?¨?é?¨?o???
A??è?¤èˉè???¤
B??????????? ?è???¤
C?????è???¢è???¤
D???°??????è???¤
E??? ?ˉè???¤
F????¤?è???¤
G??èμ?oè??é?è§|??o???è???¤
Servletè???¤?¨?¥?£?????
???Servletè???¤?¨?±?é???é?????°javax.servlet.Filter?¥?£?è???a?¥?£???3??aè???¤?¨?±???é?????°??1?3??
?1?3 èˉ′?
init(FilterConfig cfg) è??ˉServletè???¤?¨???§??1?3???§è′¨?-???servlet?init?1?3?
doFilter(ServletRequest,ServletResponse,FilterChain) ?????é?è???¤???????èˉ·?±è??é?è???¤?¨?3è?URL????Servlet??1?¨?°?è°?¨è???¤?¨?doFilter?1?3?FilterChain??°?¨?oè??é????-è???¤?¨
destroy() Servlet??1?¨?¨é?ˉè???¤?¨?????è°?¨èˉ¥?1?3??è???a?1?3??-?ˉ??¥é??Servletè???¤?¨? ?¨?èμ?o??§è′¨?-???servlet?destory()?1?3?
Servletè???¤?¨????o?-¥éa¤??
A?????°javax.servlet.Filter?¥?£?servlet?±?
B?????°init?1?3??èˉ??è???¤?¨???§????°
C?????°doFilter?1?3??????ˉ1èˉ·?±?è???¤???o
D??è°?¨FilterChain?¥?£?ˉ1è±??doFilter?1?3??????-?è???¤?¨?? éèˉ·?±???o
F.?¨web.xml??-é???Filter
2.????¨è???¤?¨?¤???-?é?é¢
???¨?¨?·??éé?μé¢è??¥???·?????|?è??¥?ˉ??-?????°servlet????è??oè???a???1?????ˉè??°±???ˉ?1±? ??è??ˉ? ??oserlvet??-é?è?¤?ˉ??¥ISO-8859-1? ?????? ????|???°??¤??aServlet,?¤??a??°??è?? ·?°±???é??è???aé?é¢???????ˉ??¥éè?????aè???¤?¨????解?3???????°?è??oè??oé??ˉ???-????°ISO-8859-1è??? ??oGBK?é£??μ??£? ??
3.????¨è???¤?¨è?¤èˉ?¨?·??
?ˉ??aè???¤?¨?1?ˉ??¥é?????§???°???ˉ??¥?°??éè|è???¤??°?é????°è???aFilter?é?????°??-??è???¤?????|?èˉ·?±?°??¨é?????°??-?????è???è?? · ?°±é???o?¨?¨?o??-?????? ??ˉ??aFilter??-??§?????é??ˉ??¥???°é????ˉ1è±????¨Filter??-é????o??a??éè|è???¤??°???????a?ˉ??éé?μé¢??????a?ˉ?§è???é è?¤èˉ?servlet??
4.Servlet????¨
?±??????Swing?é¢?o?¨?????Servlet?1?ˉ??¥???o????¨????¥?ˉ1Servlet??1?¨???Servlet??-??¥è±???o?????o??o?Servlet????¨???è|???¥??? ?§??
ServletRequestListener ,ServletRequestAttributeListener??
HttpSessionActivationListener ,HttpSessionBindingListener ,
HttpSessionAttributeListener,HttpSessionListener,
ServletContextListener?-?-?
è??o????¨???è|?¨?¥???session,request,applicationè?????a?ˉ1è±?é?-??°??????
----------------------------------------------------------------------------------------------------------------
servlet API??-?éè|?????a?è??°±?ˉè??¤??oservlet?JSPé?μé¢???1è???¤?¨?è???¤?¨????o??o???????¨??ˉ??é? ?“servleté??¥”????§?è???o?¤§??? ??????£??
è???¤?¨?ˉ????a?¨?o??????o???1???3?servlet?JSPé?μé¢è?è??¨????¨???è???¤?¨?ˉé? ?°????a??¤??aservlet?JSPé?μé¢?????1????ˉ??¥?£?¥è??¥è??oèμ?o?èˉ·?±????ˉ??¨è??1???è???¤?¨?ˉ??¥???|???é????
1. ??¥???è§??1??è°?¨èμ?o???3??è°?¨servlet?JSPé?μé¢???
2.???¨????1è??èˉ·?±????ˉè°?¨èμ?o?
3. è°?¨èμ?o?????¨?é??o?°??¢?·?o??ˉ1??è?è?????1
4. é??-¢èˉ¥èμ?oè°?¨????£?1??¥è???°?????èμ?o??è??????a?1????????£? ??????¢è??o?
è???¤?¨????o? ??aéè|?¥??¤ ??
é|???????¥???§?¨??????ˉé?¨??1???°è£???±?è???o??? ?30??a????serlvet?JSPé?μé¢??éè|?????????????1??¥??°??è????é′????2?é?é¢???é ????a????è???¤?¨??????°???o?¨?°30??aèμ?o???3?ˉ?
??????????¨??è??¤?°é??o§è??é??3?-??è?¨?°??£? ????|??è??ˉ1?oJSP?1?????·????????-??è?????°? ?1?′??aé?μé¢é??-?¨è?¨?°????è???ˉé??-?¨????é?è???????|???? ?é????¥èa??o???1?è??é?è???¨????1?é?μé¢??è??oé?μé¢??°è??é?é????????2?é?é¢????o??????aè??é?é??è???¤?¨?1?????o?¨?°?3è|é??è??é??é?μé¢???3?ˉ?
????è???¤?¨????? è??¤?ˉ1è???¤????èμ?oè?è??1é?§??′?1??? ?è???¤?°?-èμ?o??è??oèμ?oé¤?o?????è|?′?1?¤?????????????è???°?1???2?é?é¢???é ????a??2???¢è???¤?¨???aè|?é?°±????¨???
??è|?3¨???è???¤?¨?a?¨??servletè§è2.3?????1?????¨??????¨??|??? ?Web?o?¨éè|?ˉ??§?????¨???°±??è?????¨è???¤?¨?
1????o???o??è???¤?¨
??o??????aè???¤?¨???????o??a?-¥éa¤??
1????o??????a???°Filter?¥?£??±??è???a?±?éè|????a?1?3??????ˉ??doFilter?init?destroy?
doFilter?1?3??????è|?è???¤??£? ??è§???2?-¥????init?1?3??o??è??????????èdestroy?1?3è?è????¥?
2???¨doFilter?1?3??-???¥è???¤è???o?doFilter?1?3????????a??°??oServletRequest?ˉ1è±???-¤?ˉ1è±???è???¤?¨????o?ˉ1è??¥?????ˉ ?????è?¨??°???cookie?HTTPèˉ·?±?¤′??????¨è??é??????o??a??°??oServletResponse??é????¨????è???¤?¨??-????¥?-¤??°???????a? ?°??oFilterChain???|?????-¥?è?°???-¤??°?¨?¥è°?¨servlet?JSPé?μ?
3??è°?¨FilterChain?ˉ1è±??doFilter?1?3?Filter?¥?£?doFilter?1?3?????aFilterChain?ˉ1è±?????o???????a? ?°??¨è°?¨?-¤?ˉ1è±??doFilter?1?3???????′???????a???3?è???¤?¨??|??2???|????aè???¤?¨??servlet?JSPé?μé¢?3è???servlet?JSP é?μé¢è¢????′??
4???ˉ1???o?servlet?JSPé?μé¢?3¨?è???¤?¨??¨é¨??2?è?°??|??????web.xml????-????¨filter?filter-mapping??′ ?
5???|?¨???′??¨servlet?é2?-¢?¨?·???¨??o?servlet URL??è?è???¤?¨è??????
1.1??o??????a???°Filter?¥?£??±?
??è???¤?¨é???é?????°javax.servlet.Filter?è???a?¥?£???????a?1?3???????odoFilter?init?destroy?
public void doFilter(ServletRequset request,
ServletResponse response,
FilterChain chain)
thows ServletException, IOException
?ˉ??è°?¨????aè???¤?¨???3???ˉ???èˉ·?±???-¤è???¤?¨???3?servlet?JSPé?μé¢???????°±?§è???doFilter?1?3??-£?ˉè???a?1?3????o?¤§é¨?è???¤é?è?? ???????a??°??o???? ?¥èˉ·?±??3?ServletRequest??ˉ1?o????è???¤?¨???¤§?¤?°è???¤é?è??ˉ?o?oè???a?ˉ1è±????|??¤?HTTPèˉ·?±???1???éè|è??é?èˉ? ?|getHeader?getCookies?-?¨ServletRequest??-? ?3???°??1?3???°±è|??-¤?ˉ1è±??é ? HttpServletRequest?
????o??a??°??oServletResponse?é¤?o?¨??¤??a???¢??è|????¨????¥?¤??é???????¥è???a??°?é|????|???????¨é????ˉ1???3servlet?JSPé?μ é¢?è??é???ˉè°?¨response.getWriter?1??′?¥?é????a??o?°??¢?·?o?????????|????????1???3?servlet?JSPé?μé¢?è??o???ˉ?? ?o????¨????a??é???é?°???è??o??ˉ1è±???-???????¨è°?¨serlvet?JSPé?μé¢???è???¤?¨?ˉ?£?¥è??o???|??é?°±????1?????1??é?°??¢?·?o?
DoFilter???????a??°??oFilterChain?ˉ1è±???ˉ1?-¤?ˉ1è±?è°?¨doFilter??¥???′???servlet?JSPé?μé¢???3???????aè???¤?¨??|??2???|????a???3?è???¤?¨????ˉ1doFilter?è°?¨???′?servlet?JSP??èo??
public void init(FilterConfig config)thows ServletException
init?1?3?a?¨?-¤è???¤?¨??????????§????§è??????ˉ?ˉ???è°?¨è???¤?¨é??§è?????ˉ1?o????è???¤?¨???ˉ????-¤?1?3?????a??o?????????¤??a?? éè|????¨init? é|???FilterConfig?ˉ1è±?????ˉ1servlet?ˉ?¢?web.xml??????-??′??è???¤?¨??è??é??? ?-¤????é???3?ˉ???¨init?° FilterConfig?ˉ1è±??-???¨????a?-??μ??-????¥???doFilter?1?3è??¤è??é?servlet?ˉ?¢?è???¤?¨?.???????FilterConfig?ˉ1è±??· ?????agetInitParameter?1?3????è??¤è??é?é¨??2?è?°??|??????web.xml????-?é?è???¤?¨??§???°?
public void destroy( )
?¤§?¤?°è???¤?¨????°??o?-¤?1?3???????a??o??????è????ˉ???¨???¥???èˉ??|?3é-è???¤?¨????¨???????°???oè??¥?± ?-??é¤??????
1.2?°è???¤è???o???¥doFilter?1?3
doFilter?1?3??o?¤§?¤?°è???¤?¨?°?3é?é¨???ˉ??è°?¨????aè???¤?¨????é?è|?§è?doFilter??ˉ1?o?¤§?¤?°è???¤?¨?¥èˉ′??doFilter?§è???-¥éa¤?ˉ ?o?o?? ?¥?????ˉ??? ?-¤???ˉè?è|???¨????odoFilter????????a??°????ServletRequest?è???a?ˉ1è±????????é ??o HttpServletRequest?±??????¥????ˉ1èˉ¥?±???′?1???1?3?è??é??
1.3è°?¨FilterChain?ˉ1è±??doFilter?1?3
Filter?¥?£?doFilter?1?3??¥????aFilterChain?ˉ1è±?????o??????????a??°??¨è°?¨èˉ¥?ˉ1è±??doFilter?1?3???????′???????a???3? è???¤?¨?è???aè??¨??è????-?°é???-??????aè???¤?¨??o?-¢??¨??????aè???¤?¨è°?¨??FilterChain?ˉ1è±??doFilter?1?3???????′?servlet? é?μé¢èaèo??
???ˉ??é???-?????è???¤?¨é??ˉ??¥éè???è°?¨??FilterChain?doFilter?1?3??-?-è???aè??¨??¨è?? ·???μ???????è°?¨JSPé?μé¢?serlvet???1?????-?-?-¤è°?¨è??¨?è???¤?¨è′è′£?°è??o???????¢?·?o?
1.4?ˉ1é???servlet?JSPé?μé¢?3¨?è???¤?¨
é¨??2?è?°??|?????2.3??????¥?o??¤??a?¨?oè???¤?¨???′ ??????ˉ??filter?filter-mapping?filter??′ ??3????3¨?????aè???¤?ˉ1è±???filter-mapping??′ ???èˉ¥è???¤?ˉ1è±???o?¨?URL?
1.filter??′
filter??′ ???oé¨??2?è?°??|??????web.xml????é¨????filter-mapping?servlet?servlet-mapping??′ ?1??filter??′ ?·??|???-??a?ˉè???-??′ ??
1? iconè??ˉ????a?ˉé???′ ?????£°?IDEè??¤????¨?????a??è±??????
2?filter-nameè??ˉ????a??é???′ ??????è???¤?¨?é????aé?????-?
3?display-nameè??ˉ????a?ˉé???′ ???????oIDE????¨??-??§°?
4? descriptionè??1?ˉ????a?ˉé???′ ???????oIDE?????ˉ???????????£?
5? filter-classè??ˉ????a??é???′ ???????è???¤?¨???°?±?????¨é????
6? init-paramè??ˉ????a?ˉé???′ ???????1?ˉ???¨FilterConfig?getInitParameter?1?3èˉ?????§???°????aè???¤?¨??′ ?ˉ????¤??ainit-param??′ ?
èˉ·?3¨???è???¤?ˉ?¨serlvetè§è2.3???-???????¥??? ?-¤??web.xml??????é??????¨DTD?2.3??????é¢????????a???????-??
<xml version="1.0" encoding="ISO-8859-1"?>
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
filter>
<web-app>
2.filter-mapping??′
filter-mapping??′ ???oweb.xml??????-filter??′ ?1?serlvet??′ ?1????????|??????a?ˉè???-??′ ??
1? filter-nameè???a??é???′ ??é?????¨filter??′ ?£°??????oè???¤?¨???§°???1é?
2? url-pattern?-¤??′ ?£°?????a??¥?? ??/?????§??¨??????????è???¤?¨?o?¨?URL???filter-mapping??′ ??-??é?????url-pattern? servlet-name?????è??ˉ1???afilter-mapping??′ ????¤??aurl-pattern??′ é?1??|????è???¤?¨é?¨?o?¤??a?¨??????ˉé?¤ ?′??afilter-mapping??′ ?
3? servlet-name?-¤??′ ???o????a??§°???-¤??§°??é???????¨servlet??′ ???oservlet?JSPé?μé¢???§°???1é???è??????afilter-mapping??′ ??? ?¤??aservlet-name??′ é?1??|????è???¤?¨é??o?¤??aservlet????ˉé?¤è???afilter-mapping??′ ?
??�??????a???-??
xml version="1.0" encoding="ISO-8859-1"?>
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
filter>
filter-mapping>
web-app>
1.5?|?¨???′??¨servlet
?¨?ˉ1èμ?o?o?¨è???¤?¨?????ˉéè????è|?o?¨è???¤?¨?URL?¨????servlet??¥?????|????servlet?????-¤??§°??é????web.xml? servlet??′ ??-???o???§°???1é??|?????¨?o?¨?°????aserlvet?URL?¨???????-¤?¨?????é???????¨web.xml???′ servlet- mapping?????¨??????1é????ˉ???¤?°????¨????¨“???′??¨servlet”??oservlet????????a??o??URL??http: //host/WebAppPrefix/servlet/ServletName?éè|??èˉ?¨?·?????¨è???aURLè??é?servlet??è?? ·????è?è???¤?¨ è????????
???|????|???¨filter?filter-mapping??¤o???oSomeFilter?è???¤?¨?o?¨?°???oSomeServlet?servlet????|????
<filter>
<filter-mapping>
?¥????¨servlet?servlet-mappingè§??URLhttp://host/webAppPrefix/Blah ?oèˉ¥è°?¨SomeSerlvet???|????¤o??
filter>
<filter-mapping>
?°?¨???¨??¢?·?o????¨URLhttp://host/webAppPrefix/Blah ???°±??è°?¨è???¤?¨?è???¤?¨???o?¨?°
http://host/webAppPrefix/servlet/SomePackage.SomeServletClass?
?°??????3é-???′??¨?????¨???¨?1?3????ˉ???ˉ?§??¤???o??1?3??é?°? ?°Web?o?¨é?/servlet?¨?????è?? ·?????????-¤?¨????èˉ·?±è¢?é?°???? servlet??-???o?oé?°? ?°?-¤?¨?????é|??oèˉ¥??o??????a????servlet??????°????éèˉˉ???ˉ???é????¨?·?°é???±é?μ??????????¨servlet? servlet-mapping??′ ?é???/servlet?¨????èˉ·?±?°èˉ¥servlet??¨?o???9-1???o?o????a???-????-?
?¨?o???9-1 web.xml??é?????o?servlet URL??????
xml version="1.0" encoding="ISO-8859-1"?>
DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
servlet>
servlet-mapping>
<web-app>
-------------------------------------------------------------------------------------------
解?3?1±?
web.xml? é???
com.util.EncodingFilter
Encoding
gb2312
EncodingFilter.java
【J2EE|servletè????¤??¨】package com.hibernate.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
protected FilterConfig config;
protected String Encoding = null;
public void init(FilterConfig config) throws ServletException {
this.config = config;
this.Encoding = config.getInitParameter("Encoding");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request.getCharacterEncoding() == null) {
if (Encoding != null) {
request.setCharacterEncoding(Encoding);
response.setCharacterEncoding(Encoding);
}
}
chain.doFilter(request,response);
}
public void destroy() {}
}
OK!!!
----------------------------------------------------------------------------------------------------
??????è???¤?¨???°±?ˉ??o?oè?°??????aurl?èˉ·?±??é′ filter:
?°±????°filter?1??2??è?????1?°±?ˉèˉ′
?1??2???§è?. ?? ?ˉ:
chain.doFilter(request, response);
?§è??°è?é??????è?éè°?¨?????filter?servlet?????¥è???aè°?¨?°???ˉ????a??é??è??¨?
?¨è???aè°?¨é???°?????¨éè?request?resonse?????è??¥?????/?é?°?????|?è??¥?o?°????????°?′?¥??1?oException
?? ???£? é?2???è·??????????¥?|??o?°???????chain.doFilter?é¢??°±?????§è??
?ˉ??¥?chain.doFilter???°try finally?????-????èˉ???-??è¢??§è?
推荐阅读
- javaweb|基于Servlet+jsp+mysql开发javaWeb学生成绩管理系统
- =======j2ee|spring用注解实现注入的@resource,@autowired,@inject区别
- Servlet原理|Servlet原理 二(Web应用与创建Servlet实例)
- 缺失|缺失 javax.servlet 包及sts导入servlet源码
- intellij-idea|JavaWeb: IntelliJ IDEA集成开发环境下开发第一个servlet程序(JDBC)Tomcat
- servlet总结|设置session失效时间(不使用框架)----使用shiro设置session失效时间(使用shiro框架)
- J2EE系列(Tomcat内存释放不了、Tomcat内存溢出原因、Tomcat内存管理[整理])
- 获取(HttpServletRequest)
- Servlet-跳转
- idea解决Error:(6, 50) java: 程序包javax.servlet.http不存在问题