java之旅---2.20---(过滤器+String)

String+过滤器


一. String的常用方法


package com.gzq.string;

public class Demo {
	public static void main(String[] args) {
		String a="hello wor ld!1";
		String b="gan";
		System.out.println(a.length());
		System.out.println(a.substring(0,3));//截取前3为字符,包含起点不包括终点
		System.out.println(a.substring(4));//截取从第4位开始到末尾
		System.out.println(a.indexOf("l"));//从第0位开始,找到第一个,就返回下标
		System.out.println(a.indexOf("a"));//找不到就返回-1
		System.out.println(a.indexOf("1"));
		System.out.println(a.lastIndexOf("o"));
		System.out.println(a.indexOf("llo"));//返回第一个的下标
		System.out.println(a.startsWith("wor"));//如果是wor开头就返回true
		System.out.println(a.endsWith("!1"));//如果是!1结尾就返回true
		System.out.println(a.contains("wor"));//如果包含wor,就返回true
		System.out.println(a.replace(" ", "-"));//将碰到的空格换成-,
		System.out.println(a.replaceFirst(" ", "*"));//将碰到的第一个空格换成-,
		System.out.println(a+b);
	}
}


二. 过滤器


1.主要方法


package com.gzq.filter;

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 demoFilter implements Filter {


	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	
		chain.doFilter(request, response);
	}


	public void init(FilterConfig fConfig) throws ServletException {

	}

}


2.方法解读


(1)public void init(FilterConfig fConfig) throws ServletException{}

此方法是过滤器的初始化,他是在tomcat启动时被创建,只被初始化一次

public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

(2)public void destroy() {}

此方法是过滤器的销毁,在tomcat关闭时被销毁

	public void destroy() {

	}


(3)public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {}

此方法是过滤器的核心方法,要实现过滤操作的主要方法应该写在此代码段里。

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	
		chain.doFilter(request, response);
	}

1.关于chain.doFilter(request, response);
在这里插入图片描述
如果过滤完成,就通过此条语句放行给下一个过滤器。


注意: request与response的类型,可能需要转换类型。使用的转换代码如下:

HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse resp=(HttpServletResponse) response;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值