Filter 过滤程序 过滤非法ip地址

本文详细解析了登录界面的实现原理,包括JSP页面、Servlet处理程序和Filter过滤程序的作用,以及它们如何共同完成登录验证的过程。文章还介绍了如何通过配置文件web.xml设置过滤器来确保字符集的一致性和过滤非法IP地址。


登录界面

login_form.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login_form.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <pre>
  <form action="login_accept" method="post">
   用户名:<input type="text" name="name"/><br/>
   身  份:<input type="text" name="identity"/><br/>
          <input type="submit" value="提交"/><br/>  
 </form>
 </pre>
 ${errorip }
  </body>
</html>

Servlet 处理程序

LoginAcceptServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginAcceptServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        String name=request.getParameter("name");
        String identity=request.getParameter("identity");
        PrintWriter out=response.getWriter();
        System.out.println(name+" "+identity);
        out.println(name+"<br/>");
        out.println(identity);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		 doGet(request,response);
	}
}
Filter 过滤程序

FilterTest1.java 过滤字符集

package 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 FilterTest1 implements Filter {

	
    private String encoding=null;
	public void destroy() {
		// TODO Auto-generated method stub
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		request.setCharacterEncoding(encoding);
		response.setCharacterEncoding(encoding);
		
		response.setContentType("text/html;charset=utf-8");
	
		chain.doFilter(request, response);
	}
	public void init(FilterConfig config) throws ServletException {
		encoding=config.getInitParameter("characterencoding");
	}
   
}
FilterTest2.java 过滤非法ip

package 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 FilterTest2 implements Filter {

	public void destroy() {}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String ip=request.getRemoteAddr();
		System.out.println(ip+"  ");
		if(ip.startsWith("127"))
		{
			System.out.println(ip+"  ");
			chain.doFilter(request, response);
		}
		else
		{
			System.out.println("防止非法ip登陆");
			request.setAttribute("errorip", "地址不正确,不能登录");
			request.getRequestDispatcher("login_form.jsp").forward(request, response);
		}
	}
	public void init(FilterConfig arg0) throws ServletException {}

	

}

配置文件 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>filter.FilterTest1</filter-class>
  <init-param>
   <param-name>characterencoding</param-name>
   <param-value>utf-8</param-value>
  </init-param>
 </filter>
 <filter>
  <filter-name>ipFilter</filter-name>
  <filter-class>filter.FilterTest2</filter-class>
 </filter>
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
  <filter-mapping>
  <filter-name>ipFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginAcceptServlet</servlet-name>
    <servlet-class>servlet.LoginAcceptServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginAcceptServlet</servlet-name>
    <url-pattern>/login_accept</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值