public class MyServletContextListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContext对象被创建了...");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContext对象被销毁了...");
}
}
<listener>
<listener-class>com.itheima.weblistener.MyServletContextListener</listener-class>
</listener>
public class MyHttpSessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("HttpSession被创建了...");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("HttpSession被销毁了...");
}
}
<listener>
<listener-class>com.itheima.weblistener.MyHttpSessionListener</listener-class>
</listener>
public class MyServletRequestListener implements ServletRequestListener{
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("ServletRequest被销毁了...");
}
@Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("ServletRequest被创建了...");
}
}
<listener>
<listener-class>com.itheima.weblistener.MyServletRequestListener</listener-class>
</listener>
public void init(FilterConfig filterConfig) throws ServletException {
// 获得当前的Filter的名称:
String filterName = filterConfig.getFilterName();
System.out.println(filterName);
// 获得初始化参数:
String username = filterConfig.getInitParameter("username");
String password = filterConfig.getInitParameter("password");
System.out.println(username+" "+password);
// 获得所有的初始化参数的名称:
Enumeration<String> en = filterConfig.getInitParameterNames();
while(en.hasMoreElements()){
String name = en.nextElement();
String value = filterConfig.getInitParameter(name);
System.out.println(name+" "+value);
}
}
create database web_16;
use web_16;
create table user(
id int primary key auto_increment,
username varchar(20),
password varchar(20),
nickname varchar(20),
type varchar(10)
);
insert into user values (null,'aaa','111','张凤','user');
insert into user values (null,'bbb','111','如花','user');
insert into user values (null,'ccc','111','张芙蓉','user');
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper{
private HttpServletRequest request;
public MyHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String getParameter(String name) {
// 根据请求方式不同,去处理:
// 获得请求方式:
String method = request.getMethod();
if("get".equalsIgnoreCase(method)){
String value = null;
try {
value = new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}else if("post".equalsIgnoreCase(method)){
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return super.getParameter(name);
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |