javaee学习日记之Servlet,过滤器,拦截器,jstl,EL,jsp

1 篇文章 0 订阅
1 篇文章 0 订阅

Servlet
在web.xml容器中的加载

    <servlet>
        <servlet-name>servlets</servlet-name><!--一般用类名首字母小写表示-->
        <servlet-class>cn.ii.user.web.UserServlet</servlet-class><!--全限定类名(包+类名)-->
    </servlet>
    <!--Servlet映射-->
    <servlet-mapping>
        <servlet-name>servlets</servlet-name><!--与上名同-->
        <url-pattern>/servlets</url-pattern><!--访问时所用名-->
    </servlet-mapping>

我的理解是:让web容器加载这个类到tomcat上,通过映射,定义的名字,进行访问!
Servlet:java代码
一,实现Servlet接口

import javax.servlet.*;
import java.io.IOException;
public class BServlet implements Servlet {
    //初始化即产生
    public void init(ServletConfig servletConfig) throws ServletException {
    System.out.println("Servlet出生");
    }
    //ServletConfig:上下文对象的获取
    public ServletConfig getServletConfig() {
        return null;
    }
    //请求方法
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
    System.out.println("Servlet正在处理请求");
    }
    //servlet的一些信息
    public String getServletInfo() {
        return null;
    }
    //销毁即死去
    public void destroy() {
    System.out.println("可怜的Servlet去世了");
    }

二.继承GenericServlet

import javax.servlet.*;
import java.io.IOException;

public class BServlet extends GenericServlet {
    //初始化即产生
    public void init(ServletConfig servletConfig) throws ServletException {

    }
    //ServletConfig:上下文对象的获取
    public ServletConfig getServletConfig() {
        return null;
    }
    //请求方法
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

    }
    //Servlet的一些信息
    public String getServletInfo() {
        return null;
    }
    //销毁即死去
    public void destroy() {

    }
}

三.继承HttpServlet(用的最多)

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 将service请求细化成get与post请求,
 */
public class BServlet extends HttpServlet {
    //get请求(明文)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         req.setAttribute("x","");//Request域,有效范围:单次请求内有效
        req.getSession().setAttribute("x","");//Session域,有效范围:浏览器与服务器的一次会话中有效,不同浏览器Session保留时间不同!
        req.getRequestDispatcher("index.jsp").forward(req,resp);//转发,配合Request域使用!地址栏不变,还是一次请求。
        resp.sendRedirect("index.jsp");//重定向,请求改变,地址栏改变!
        resp.setContentType("text/html;charset=UTF-8");//设置响应类型
        resp.getWriter().print("IO");//响应流
        //applicationcontext域:服务器运行中有效,重启或关闭后失效
        //page域:在本页面内有效(jsp中)
        req.getHeaderNames();//请求头信息(全部)
        //jsp九大内置对象:四个域 + response响应对象 + out输出对象 + config配置对象 + exception异常对象 + page页面对象
    }
    //post请求(隐藏的)
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

jsp = Servlet

jstl:标签库(为了更好的让前后端分离)

<%--设置本页编码,后端语言,响应编码--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%--导入jar包--%>
<%@ page import="java.util.List"%>
<%--导入jstl标签库,前缀为c,由':'分离--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<head>
    <title>bug</title>
</head>
<body>
<!--forEach遍历List集合中的User对象-->
    <c:forEach items="${list}" var="l">
        ${l.username}<br/>
    </c:forEach>
<% //往四大域中添加值!
   //用El表达式取(优先级,由以下顺序,由大到小)
    pageContext.setAttribute("message","0");
    request.setAttribute("message","1");
    session.setAttribute("message","2");
    application.setAttribute("message","3");
%>
<!--EL表达式-->
${message}
<!--if语句,test中是判断条件-->
<c:if test="${w} == 0">
    </c:if>
  <!--switch语句-->  
<c:choose>
    <c:when test="${x} == 1">1</c:when>
    <c:when test="${x} == 2">2</c:when>
    <c:otherwise>3</c:otherwise>
</c:choose>
<!--jsp内置标签转发-->
<jsp:forward page="login.html"></jsp:forward>
</body>
</html>

过滤器在web容器中的加载

    <filter>
        <filter-name>userFilter</filter-name>
        <filter-class>cn.ii.user.UserFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>userFilter</filter-name>
        <url-pattern>/*</url-pattern><!--过滤范围(这里是全部资源)-->
    </filter-mapping>

Filter:过滤器

import javax.servlet.*;
import java.io.IOException;

public class BBFilter implements Filter {
    public void destroy() {
        System.out.println("Filter销毁");
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("过滤内容");
        //可以在这里实现,解决中文乱码问题!
    }

    public void init(FilterConfig config) throws ServletException {
        System.out.println("Filter出生");
    }
}

拦截器在web容器中的加载

    <listener>
        <listener-class>cn.ii.user.ListenerS</listener-class>
    </listener>

拦截器实现
(可以实现一些资源监控,用户权限监控)

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class ListenerS implements ServletContextListener,
        HttpSessionListener, HttpSessionAttributeListener {

    // Public constructor is required by servlet spec
    public ListenerS() {
    }

    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    //监控context:上下文对象的创建
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed). 
         You can initialize servlet context related data here.
      */
    }
    //监控context:上下文对象的销毁
    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context 
         (the Web application) is undeployed or 
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    //监控session:会话对象的创建
    public void sessionCreated(HttpSessionEvent se) {
      /* Session is created. */
    }
    //监控session:会话对象的销毁
    public void sessionDestroyed(HttpSessionEvent se) {
      /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------
    //监控session:会话中的属性添加
    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }
    //监控session:会话中的属性移除
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }
    //监控session:会话中的属性取代
    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
}

Servlet3.0以后Servlet等在web.xml中的配置实现注解取代
我认为TomCat等服务器,底层实现了注解扫描器!读取整个web项目中的class文件,来加载!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值