1. CityQuery.java

package com.xxx.servlet;

import com.google.common.collect.Lists;
import com.xxx.data.HotelInfo; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-15
* Time: 下午7:39
* To change this template use File | Settings | File Templates.
*/
public class CityQuery extends HttpServlet {
private static String PAGE_NO = "pageNo";
private static String PAGE_SIZE = "pageSize";
private static String CITY = "city"; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String city = req.getParameter(CITY);
String pageNo = req.getParameter(PAGE_NO);
String pageSize = req.getParameter(PAGE_SIZE); if (city == null || city.equals("")
|| pageNo == null || pageNo.equals("")
|| pageSize == null || pageSize.equals(""))
resp.getWriter().println("参数缺失"); // 筛选城市
List<String> hotelList = Lists.newArrayList();
HotelInfo hotelInfo = HotelInfo.getInstance();
for (String hotelCode : hotelInfo.getHotelInfo().keySet()) {
if (hotelCode.contains(city))
hotelList.add(hotelInfo.getHotelInfo().get(hotelCode));
} // 分页
int pageSizeInt = Integer.valueOf(pageSize);
int pageNoInt = Integer.valueOf(pageNo);
int startIndex = pageSizeInt * (pageNoInt - 1);
int endIndex = startIndex + pageSizeInt;
if (startIndex < 0)
startIndex = 0;
if (startIndex > hotelList.size() - 1)
startIndex = hotelList.size() - 1;
if (endIndex > hotelList.size() - 1)
endIndex = hotelList.size() - 1; hotelList = hotelList.subList(startIndex, endIndex + 1);
resp.getWriter().println("City: " + city);
resp.getWriter().println("Page: " + pageNo);
resp.getWriter().println("PageSize: " + pageSize);
for (String hotel : hotelList)
resp.getWriter().println(hotel);
}
}

2.HotelQuery.java

package com.xxx.servlet;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import com.xxx.data.HotelInfo; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-15
* Time: 下午5:27
* To change this template use File | Settings | File Templates.
*/
public class HotelQuery extends HttpServlet {
private static String SEQ = "seq"; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String hotelCode = req.getParameter(SEQ);
HotelInfo hotelInfo = HotelInfo.getInstance();
if (hotelCode == null || hotelCode.equals(""))
resp.getWriter().println("参数缺失");
else
resp.getWriter().println(hotelCode + " " + hotelInfo.getHotelInfo().get(hotelCode));
}
}

3.WhiteListFilter

package com.xxx.filter;

import com.google.common.io.Files;
import com.xxx.data.WhiteList;
import com.xxx.util.HttpTools; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-15
* Time: 下午11:19
* To change this template use File | Settings | File Templates.
*/
public class WhiteListFilter implements Filter {
private static String IP = "ip"; @Override
public void init(FilterConfig filterConfig) throws ServletException {
} /**
* 过滤白名单
*
* @param servletRequest
* @param servletResponse
* @param filterChain
* @throws IOException
* @throws ServletException
*/
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse resp = (HttpServletResponse) servletResponse;
String ip = HttpTools.getRemoteAddress(req);
WhiteList whiteList = WhiteList.getInstance();
if (!whiteList.getWhiteList().contains(ip)) {
resp.setCharacterEncoding("UTF-8");
resp.getWriter().println("您的IP受限");
} else {
resp.setCharacterEncoding("UTF-8");
filterChain.doFilter(req, resp);
}
} @Override
public void destroy() {
} }

4.HotelInfo.java

package com.xxx.data;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor; import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-15
* Time: 下午7:40
* To change this template use File | Settings | File Templates.
*/
public class HotelInfo {
private static String INFO_FILENAME = HotelInfo.class.getClassLoader().getResource("hotelinfo.txt").getPath();
private Map<String, String> hotelInfo; // 酒店列表静态数据
private static HotelInfo instance;
private static Object lock = new Object(); // 锁对象 public Map<String, String> getHotelInfo() {
return hotelInfo;
} public static HotelInfo getInstance() {
if (instance == null) {
synchronized (lock) {
if (instance == null) {
instance = new HotelInfo();
instance.hotelInfo = Maps.newHashMap();
// 初始化酒店信息,读入Map中
File file = new File(INFO_FILENAME);
try {
// 读取文件同时处理每行数据
Files.readLines(file, Charsets.UTF_8, new LineProcessor<List<String>>() {
@Override
public boolean processLine(String s) throws IOException {
// 使用正则切分空字符
List<String> data = Lists.newArrayList(
Splitter.onPattern("\\s").trimResults().omitEmptyStrings().split(s));
instance.hotelInfo.put(data.get(0), data.get(1));
return true;
} @Override
public List<String> getResult() {
return null;
}
});
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
return instance;
}
}

5.WhiteList.java

package com.xxx.data;

import com.google.common.base.Charsets;
import com.google.common.io.Files; import java.io.File;
import java.io.IOException;
import java.util.List; /**
* Created with IntelliJ IDEA.
* User: zhenwei.liu
* Date: 13-7-15
* Time: 下午11:39
* To change this template use File | Settings | File Templates.
*/
public class WhiteList {
private static String WHITE_LIST_FILENAME = WhiteList.class.getClassLoader().getResource("whitelist.txt").getPath();
;
private List<String> whiteList; // 白名单静态数据
private static WhiteList instance;
private static Object lock = new Object(); public static WhiteList getInstance() {
if (instance == null) {
synchronized (lock) {
if (instance == null) {
instance = new WhiteList();
File file = new File(WHITE_LIST_FILENAME);
try {
instance.whiteList = Files.readLines(file, Charsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return instance;
} public List<String> getWhiteList() {
return whiteList;
}
}

7.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Servlet Test</display-name>
<description>
Servlet Test
</description>
<servlet>
<servlet-name>hotelQuery</servlet-name>
<servlet-class>com.xxx.servlet.HotelQuery</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hotelQuery</servlet-name>
<url-pattern>/hotelQuery</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cityQuery</servlet-name>
<servlet-class>com.xxx.servlet.CityQuery</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cityQuery</servlet-name>
<url-pattern>/cityQuery</url-pattern>
</servlet-mapping> <!-- 白名单filter配置 -->
<filter>
<filter-name>WhiteListFilter</filter-name>
<filter-class>com.xxx.filter.WhiteListFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WhiteListFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

8.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>ServletTest</groupId>
<artifactId>ServletTest</artifactId>
<version>1.0-SNAPSHOT</version> <parent>
<groupId>xxx.common</groupId>
<artifactId>xxx-supom-generic</artifactId>
<version>1.2.32</version>
</parent> <dependencies>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency> </dependencies> </project>

Servlet Filter 示例的更多相关文章

  1. Servlet Filter 2

    10.Filter常见应用 )统一全站字符编码的过滤器 通过配置参数encoding指明使用何种字符编码,以处理Html Form请求参数的中文问题 案例:编写jsp 输入用户名,在Servlet中获 ...

  2. Servlet Filter 1

    1.Filter简介 )Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图 ...

  3. 【转载】Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门

    Servlet Filter(过滤器).Filter是如何实现拦截的.Filter开发入门 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过F ...

  4. Servlet Filter(过滤器)、Filter是如何实现拦截的、Filter开发入门

    Servlet Filter(过滤器).Filter是如何实现拦截的.Filter开发入门 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过F ...

  5. Introduction of Servlet Filter(了解Servlet之Filter)

    API文档中介绍了public Interface Filter(公共接口过滤器) Servlet API文档中是这样介绍的: ‘A filter is an object that performs ...

  6. servlet/filter/listener/interceptor区别与联系

    转自:http://www.cnblogs.com/doit8791/p/4209442.html servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是 ...

  7. java Servlet Filter 拦截Ajax请求

    /** * 版权:Copyright 2016-2016 AudaqueTech. Co. Ltd. All Rights Reserved. * 描述: * 创建人:赵巍 * 创建时间:2016年1 ...

  8. Java-maven异常-cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突

    使用maven开发web应用程序, 启动的时候报错: jar not loaded. See Servlet Spec . Offending class: javax/servlet/Servlet ...

  9. 理解Servlet过滤器 (javax.servlet.Filter)

    过滤器(Filter)的概念 过滤器位于客户端和web应用程序之间,用于检查和修改两者之间流过的请求和响应. 在请求到达Servlet/JSP之前,过滤器截获请求. 在响应送给客户端之前,过滤器截获响 ...

随机推荐

  1. Android Webview中解决H5的音视频不能自动播放的问题

    在开发webview的时候,当加载有声音的网页的时候,声音不会自动播放, 解决方法:在webview中调用js方法.这个方法需要在webview的setWebViewClient方法之后在onPage ...

  2. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  3. AC日记——[CQOI2009]DANCE跳舞 洛谷 P3153

    [CQOI2009]DANCE跳舞 思路: 二分+最大流: 代码: #include <cstdio> #include <cstring> #include <iost ...

  4. 浅谈BUFF设计

    Buff在游戏中无处不在,比如WOW.DOTA.LOL等等,这些精心设计的BUFF,让我们击节赞叹,沉迷其中. 问:BUFF的本质是什么? BUFF 是对一项或多项数据进行瞬间或持续作用的集合.(持续 ...

  5. Python全栈开发之9、面向对象、元类以及单例

    前面一系列博文讲解的都是面向过程的编程,如今是时候来一波面向对象的讲解了 一.简介 面向对象编程是一种编程方式,使用 “类” 和 “对象” 来实现,所以,面向对象编程其实就是对 “类” 和 “对象” ...

  6. http学习笔记1

    通讯的条件 学前小故事 通过这个故事,我们来理解两台电脑之间的通信,必须具备什么样的条件? 有一天啊,这个小明和小强,一个在山的这头放牛,一个在山的那头割草.但是,由于无聊,这个小明就像找对面的小强聊 ...

  7. c++ 单例模式研究

    一篇博文:C++ 单例模式的几种实现研究 中 看到的几段代码 懒汉模式 class Singleton { public: static Singleton* GetInstance() { if ( ...

  8. 为什么主引导记录的内存地址是0x7C00?

    转自:http://www.ruanyifeng.com/blog/2015/09/0x7c00.html 当时,搭配的操作系统是86-DOS.这个操作系统需要的内存最少是32KB.我们知道,内存地址 ...

  9. 【Python初级】由生成杨辉三角代码所思考的一些问题

    杨辉三角定义如下: 1 / \ 1 1 / \ / \ 1 2 1 / \ / \ / \ 1 3 3 1 / \ / \ / \ / \ 1 4 6 4 1 / \ / \ / \ / \ / \ ...

  10. Scrapy实战篇(七)之Scrapy配合Selenium爬取京东商城信息(下)

    之前我们使用了selenium加Firefox作为下载中间件来实现爬取京东的商品信息.但是在大规模的爬取的时候,Firefox消耗资源比较多,因此我们希望换一种资源消耗更小的方法来爬取相关的信息. 下 ...