JSP基础(二)--JavaBean、Servlet、Filter、Listener

一、JavaBean

JavaBean也是java类,属性一般设置为私有,方法一般是setter方法和getter方法,还可以添加其他方法

1、<jsp:useBean>

<jsp:useBean id="自定义对象名" class="包名类名" scope="保存范围"/>
//page指令中不用再导入了
//四种范围:page、request、session、application

2、<jsp:setProperty>

<jsp:setProperty name="对象名" property="属性名" value="内容"/>
//指定属性设置内容
<jsp:setProperty name="对象名" property="属性名" param="参数名"/>
//指定属性指定表单某个参数的内容
<jsp:setProperty name="对象名" property="属性名"/>
//会检索表单所有参数查找与属性名匹配的表单参数,然后传值,与第三个类似
<jsp:setProperty name="对象名" property="*"/>
//会检索表单所有参数查找与对象属性名匹配的表单参数,然后一一传值

3、<jsp:getProperty>

<jsp:getProperty name="对象名" property="属性名"/>
//直接输出,类似<%=%>

4、删除JavaBean

设置JavaBean保存的范围.removeAttribute("对象名")//看作属性

二、Servlet

1、方式一:使用注解

导入import javax.servlet.annotation.WebServlet;
然后在servlet的上一行添加@WebServlet("Servlet的自定义名称")

2、方式二:配置web.xml文件

//修改完web.xml文件需重启服务器
<servlet>
<servlet-name>自定义一个名字</servlet-name>
<servlet-class>包名类名</servlet-class>
<init-param>
<param-name>初始化参数(可以不设)</param-name>
<param-value>参数值</param-value>
</init-param>
</filter>
<servlet-mapping>
<servlet-name>与上面自定义的名字相同</servlet-name>
<url-pattern>/自定义访问名</url-pattern>//(/*)(*.do)(/xxx/*)等等
</servlet-mapping>
//可以映射多个url<servlet-mapping>多写几个即可

3、简介

自定义的servlet可以继承自HttpServlet,也可以继承自GenericServlet,一般继承HttpServlet
*需要导入几个必要的类
java.io.IOException;
java.io.PrintWriter;//response.getWriter()生成其对象,调用out.print(),out.println()方法向客户端输出一些东西,System.out.println()是向控制台输出
javax.servlet.ServletException;
javax.servlet.annotation.WebServlet;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
javax.servlet.http.HttpSession;
*重写doPost()或者doGet()方法,一般重写doPost()方法,然后在doGet()里面调用doPost()方法
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{主体}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{do.Post(request,response)}
*生命周期
public void init() throws ServletException//初始化方式一
public void init(ServletConfig config) throws ServletException//方式二
public abstract void serives(ServletRequest req,ServletResponse res) throws ServletException,IOException
//一般不会重写此方法,而是重写doPost()或者doGet()方法
public void destroy()//销毁

4、获取session对象和application对象

*获取session对象,通过HttpServletRequest接口实现
HttpSession session=request.getSession()
*获取application对象,通过ServletContext接口实现
ServletContext application=this.getServletContext()

5、跳转

*客户端跳转
response.sendRedirect("url")
*服务器端跳转
方式一:导入javax.servlet.RequestDispatcher
RequestDispatcher rd=request.getRequestDispatcher("url")
rd.forward(req,res)
方式二:直接调用request.getRequestDispatcher("url").forward(req,res)

三、Filter(过滤器)

1、使用需配置web.xml文件

//配置web.xml文件类似servlet

//修改完web.xml文件需重启服务器
<filter>
<filter-name>自定义一个名字</filter-name>
<filter-class>包名类名</filter-class>
<init-param>
<param-name>初始化参数(可以不设)</param-name>
<param-value>参数值</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>与上面自定义的名字相同</filter-name>
<url-pattern>/自定义访问名</url-pattern>
</filter-mapping>

2、Filter接口定义的方法

public void init(FilterConfig filterConfig) throws ServletException
public void doFilter(ServletRequest req,ServletResponse res,FilterChain chain) throws ServletException,IOException//过滤器链
public void destroy()

3、FilterChain接口定义的方法

public void doFilter(ServletRequest req,ServletResponse res) throws ServletException,IOException

4、主要应用

编码过滤,开启某页面之前预先做一些事情等等

四、Listerer(监听器)

//配置web.xml文件
<listener>
<listener-class>包名类名</listener-class>
</listener>

request的监听与application的监听比较相似

1、监听request的状态

监听请求状态
*导入相关类
javax.servlet.ServletRequestListener
javax.servlet.ServletRequestEvent
编写一个类,实现ServletRequestListener接口
*ServletRequestListener接口定义的方法
public void requestInitialized(ServletRequestEvent sre)//容器启动触发
public void requestDestroyed(ServletRequestEvent sre)//容器销毁触发
*ServletRequestEvent接口定义的方法
public ServletRequest getServletRequest()//获取ServletRequest对象
public ServletContext getServletContext()//获取ServletContext对象

2、监听request的属性

*导入相关类
javax.servlet.ServletRequestAttributeListener
javax.servlet.ServletRequestAttributeEvent
编写一个类,实现ServletRequestAttributeListener接口
*ServletRequestAttributeListener接口定义的方法
public void attributeAdded(ServletRequestAttributeEvent srae)//增加属性触发
public void attributeRemoved(ServletRequestAttributeEvent srae)//删除属性触发
public void attributeReplaced(ServletRequestAttributeEvent srae)//修改(或重复添加)属性触发
*ServletRequestAttributeEvent接口定义的方法
public String getName()//获取属性名称
public Object getValue()//获取属性内容

3、监听application的状态

监听上下文
*导入相关类
javax.servlet.ServletContextListener
javax.servlet.ServletContextEvent
编写一个类,实现ServletContextListener接口
*ServletContextListener接口定义的方法
public void contextInitialized(ServletContextEvent sce)//容器启动触发
public void contextDestroyed(ServletContextEvent sce)//容器销毁触发
*ServletContextEvent接口定义的方法
public ServletContext getServletContext()//获取ServletContext对象

4、监听application的属性

*导入相关类
javax.servlet.ServletContextAttributeListener
javax.servlet.ServletContextAttributeEvent
编写一个类,实现ServletContextAttributeListener接口
*ServletContextAttributeListener接口定义的方法
public void attributeAdded(ServletContextAttributeEvent scae)//增加属性触发
public void attributeRemoved(ServletContextAttributeEvent scae)//删除属性触发
public void attributeReplaced(ServletContextAttributeEvent scae)//修改(或重复添加)属性触发
*ServletContextAttributeEvent接口定义的方法
public String getName()//获取属性名称
public Object getValue()//获取属性内容

5、监听session的状态

监听用户上线、离线
*导入相关类
javax.servlet.http.HttpSessionListener
javax.servlet.http.HttpSessionEvent
编写一个类,实现HttpSessionListener接口
*HttpSessionListener接口定义的方法
public void sessionCreated(HttpSessionEvent se)//session创建触发
public void sessionDestroyed(HttpSessionEvent se)//session销毁触发
*HttpSessionEvent接口定义的方法
public HttpSession getSession()//获取HttpSession对象

6、监听session的属性

javax.servlet.http.HttpSessionAttributeListener
javax.servlet.http.HttpSessionBindingEvent
编写一个类,实现HttpSessionAttributeListener接口
*HttpSessionAttributeListener接口定义的方法
public void attributeAdded(HttpSessionBindingEvent sbe)//增加属性触发
public void attributeRemoved(HttpSessionBindingEvent sbe)//删除属性触发
public void attributeReplaced(HttpSessionBindingEvent sbe)//修改(或重复添加)属性触发
*HttpSessionBindingEvent接口定义的方法
public HttpSession getSession()//获取session对象
public String getName()//获取属性名称
public Object getValue()//获取属性内容

猜你喜欢

转载自www.cnblogs.com/baochao/p/13172495.html