首页 技术 正文
技术 2022年11月14日
0 收藏 941 点赞 4,166 浏览 8172 个字

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>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845