解决项目字符乱码
乱码问题说明:基本上在每个Servlet中都要处理乱码问题,所以应该把这个工作放到过滤器中来完成。
获取请求参数中的乱码问题:POST请求:request.setCharacterEncoding("UTF-8");
GET请求:new String(request.getParamter("xxx").getBytes("ISO-8859-1","UTF-8");
响应的乱码问题:
response.setContextType("text/html;charset=UTF-8");
代码实现:
EncodingServlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class EncodingServlet
*/
@WebServlet(name="EncodingServlet",urlPatterns={"/EncodingServlet"})
public class EncodingServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public EncodingServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//响应乱码问题
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
PrintWriter out =response.getWriter();
out.println(username);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//响应乱码问题
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
PrintWriter out =response.getWriter();
out.println(username);
}
}
EncoddingRequest.java
package com.filter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* 装饰request
* @author Administrator
* @date 2016年8月13日
*/
public class EncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest req;
public EncodingRequest(HttpServletRequest request) {
super(request);
this.req = request;
}
public String getParameter(String name) {
String value = req.getParameter(name);
//处理编码问题
try {
value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return value;
}
}
EncodingFilter.java
package com.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;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
/**
* Servlet Filter implementation class EncodingFilter
*/
@WebFilter(filterName="EncodingFilter",urlPatterns={"/encode.jsp"})
public class EncodingFilter implements Filter {
/**
* Default constructor.
*/
public EncodingFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//处理POST请求编码问题
request.setCharacterEncoding("UTF-8");
//处理GET请求编码问题
HttpServletRequest req = (HttpServletRequest)request;
if(req.getMethod().equals("GET")){
EncodingRequest er = new EncodingRequest(req);
chain.doFilter(er, response);
}else if(req.getMethod().equals("POST")) {
chain.doFilter(request, response);
}
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
encode.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>字符乱码问题</title>
</head>
<body>
<a href="EncodingServlet?username=张三">GET请求</a>
<form action="EncodingServlet" method="post">
用户名:<input type="text" name="username" value="李四"/>
<input type="submit" value="登录" />
</form>
</body>
</html>
运行结果:
使用GET请求:
使用POST请求:
分IP统计网站的访问次数
网站统计每个IP地址访问本网站的次数说明:因为一个网站可能有多个页面,无论哪个页面被访问,都要统计访
问次数,所以使用过滤器最为方便。
统计工作需要在所有资源之前都执行,那么就可以放到Filter中了。我们这个过滤器不打算做拦截操作,因为我们
只是用来做统计的。
装载统计数据的容器:Map<String,Object>,整个网站只需要一个Map即可。
Map需要在Filter中用来保存数据。
Map需要在页面使用,打印Map中的数据。
Map需要使用ServletContextListener在服务器启动时完成创建,并只保存到ServletContext中。
代码实现:
IPListener.java
package com.listener;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
/**
* Application Lifecycle Listener implementation class IPListener
*
*/
@WebListener
public class IPListener implements ServletContextListener {
/**
* Default constructor.
*/
public IPListener() {
// TODO Auto-generated constructor stub
}
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
/**
* 在服务器启动时创建Map,保存到ServletContext
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent servletContextEvent) {
//创建Map
Map<String, Integer> map = new LinkedHashMap<String,Integer>();
//得到ServletContext
ServletContext application = servletContextEvent.getServletContext();
//把map保存到application中
application.setAttribute("map", map);
}
}
IPFilter.java
package com.filter;
import java.io.IOException;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* 从application中获取Map
* 从request中得到当前客户端的IP
* 进行统计工作,结果保存到Map中
*/
@WebFilter(filterName="IPFilter",urlPatterns={"/*"})
public class IPFilter implements Filter {
private FilterConfig confige;
/**
* Default constructor.
*/
public IPFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
// place your code here
//1得到application中的map
ServletContext app = confige.getServletContext();
Map<String, Integer> map = (Map<String, Integer>)app.getAttribute("map");
//2从request中获取当前客户端的IP地址
String ip = request.getRemoteAddr();
//3查看map中的是否存在这个IP对应访问次数,如果存在,把次数+1再保存回去
//如果不存在这个IP,那么说明是第一次访问本站,设置访问次数为1
if(map.containsKey(ip)){
int cnt = map.get(ip);
map.put(ip, cnt+1);
}else {
map.put(ip, 1);
}
//4把map放回到app中
app.setAttribute("map", map);
// 放行
chain.doFilter(request, response);
}
/**
* 在服务器启动时执行此方法,而且只执行一次
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
this.confige = fConfig;
}
}
show.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 导入JSTL标签库 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>结果页面</title>
</head>
<body>
<h1 align="center">显示结果</h1>
<table align="center" width="60%" border="1">
<tr>
<td>IP</td>
<td>次数</td>
</tr>
<c:forEach items="${applicationScope.map}" var="entry">
<tr>
<td>${entry.key}</td>
<td>${entry.value}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
运行结果:
页面静态化
页面静态化说明:首次访问去数据库获取数据,然后把数据保存到一个HTML页面中。第二次访问以及以后的访
问,就不再去访问数据库了,而是直接显示HTML。
项目所用数据库:
create database imooc charset utf8;
create table imooc_book(
bid char(32) primary key,
bname varchar(100),
price numeric(10,2),
category int
)charset utf8;
insert into imooc_book values('b1','JavaSE_1',10,1);
insert into imooc_book values('b2','JavaSE_2',15,1);
insert into imooc_book values('b3','JavaSE_3',20,1);
insert into imooc_book values('b4','JavaSE_4',25,1);
insert into imooc_book values('b5','JavaEE_1',30,2);
insert into imooc_book values('b6','JavaEE_1',35,2);
insert into imooc_book values('b7','JavaEE_1',40,2);
insert into imooc_book values('b8','Java_framework_1',45,3);
insert into imooc_book values('b9','Java_framework_2',50,3);
整个项目的概述:
JSP:link.jsp页面:链接页面,四个超链接:1)查看所有、2)查看SE分类、3)查看EE分类、4)查看框架分类
show.jsp页面:显示查询结果
servlet:BookServlet的findAll()方法:查看所有图书
BookServlet的findByCategory()方法:按分类进行查询
service:BookService略
dao:BookDao的List<Book> findAll()方法:从数据查询出所有图书
BookDao的List<Book> findByCategory(int category)方法:从数据库分类进行查询
domain:Book实体类:与数据库相对应
filter:StaticFilter.java用于判断请求是否为首次请求并作出响应的处理
StaticResponse.java生成静态HTML文件
相关jar包导入:
代码实现:
Book.java
package com.domain; /** * Book实体类 * @author Administrator * @date 2016年8月16日 */ public class Book { private String bid; private String bname; private double price; private int category; public String getBid() { return bid; } public void setBid(String bid) { this.bid = bid; } public String getBname() { return bname; } public void setBname(String bname) { this.bname = bname; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getCategory() { return category; } public void setCategory(int category) { this.category = category; } @Override public String toString() { return "Book [bid=" + bid + ", bname=" + bname + ", price=" + price + ", category=" + category + "]"; } }
BookDao.java
package com.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.domain.Book; import cn.itcast.jdbc.TxQueryRunner; public class BookDao { private QueryRunner qr = new TxQueryRunner(); public List<Book> findAll(){ try { String sql = "select * from imooc_book"; return qr.query(sql, new BeanListHandler<Book>(Book.class)); } catch (SQLException e) { throw new RuntimeException(); } } public List<Book> findByCategory(int category){ try { String sql = "select * from imooc_book where category=?"; return qr.query(sql, new BeanListHandler<Book>(Book.class),category); } catch (SQLException e) { throw new RuntimeException(); } } }
BookServlet.java
package com.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.BookDao; import cn.itcast.servlet.BaseServlet; /** * Servlet implementation class BookServlet */ @WebServlet("/BookServlet") public class BookServlet extends BaseServlet { private BookDao bookDao = new BookDao(); public String findAll(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ request.setAttribute("bookList", bookDao.findAll()); return "/show.jsp";//转发 } public String findByCategory(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ String value = request.getParameter("category"); int category = Integer.parseInt(value); request.setAttribute("bookList", bookDao.findByCategory(category)); return "/show.jsp";//转发 } }
StaticFilter.java
package com.filter; import java.io.File; 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; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet Filter implementation class StaticFilter */ @WebFilter(filterName="StaticFilter",urlPatterns = { "/BookServlet" }) public class StaticFilter implements Filter { private FilterConfig config; /** * Default constructor. */ public StaticFilter() { // TODO Auto-generated constructor stub } /** * @see Filter#destroy() */ public void destroy() { // TODO Auto-generated method stub } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; /* * 第一次访问时,查找请求对应的HTML页面是否存在,如果存在,重定向到HTML页面 * 如果不存在,放行,把Servlet访问数据库后,输出给客户端的数据保存到一个HTML文件中 * 然后再重定向到HTML页面 */ //获取category参数,category有四种可能 String category = request.getParameter("category"); String htmlPage = category + ".html"; String htmlPath = config.getServletContext().getRealPath("/htmls"); File destFile = new File(htmlPath,htmlPage); //如果文件存在 if(destFile.exists()){ res.sendRedirect(req.getContextPath() + "/htmls/" + htmlPage); return ; } //如果不存在 StaticResponse sr = new StaticResponse(res, destFile.getAbsolutePath()); chain.doFilter(request, sr);//放行,即生成HTML文件 //重定向到HTML文件 res.sendRedirect(req.getContextPath() + "/htmls/" + htmlPage); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { this.config = fConfig; } }
StaticResponse.jav
package com.filter; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; public class StaticResponse extends HttpServletResponseWrapper { private PrintWriter pw; /** * String path:HTML文件路径 * @param response * @param path * @throws FileNotFoundException * @throws UnsupportedEncodingException */ public StaticResponse(HttpServletResponse response,String path) throws FileNotFoundException, UnsupportedEncodingException { super(response); //创建一个与HTML文件路径绑定在一起的流对象 pw = new PrintWriter(path,"UTF-8"); } public PrintWriter getWriter(){ //返回一个与HTML绑定在一起的PrintWriter对象 return pw; } }
link.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>超链接页面</title> </head> <body> <h1>超链接页面</h1> <a href="BookServlet?method=findAll" >查看所有</a><br/> <a href="BookServlet?method=findByCategory&category=1" >查看JavaSE</a><br/> <a href="BookServlet?method=findByCategory&category=2" >查看JavaEE</a><br/> <a href="BookServlet?method=findByCategory&category=3" >查看Java_framework</a><br/> </body> </html>
show.jsp页面:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- 导入JSTL标签库 --> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>显示页面</title> </head> <body> <h1 align="center">图书列表</h1> <table border="1" align="center" width="50%"> <tr> <th>图书名称</th> <th>图书单价</th> <th>图书分类</th> </tr> <c:forEach items="${bookList}" var="book"> <tr> <td>${book.bname}</td> <td>${book.price}</td> <c:choose> <c:when test="${book.category eq 1 }"> <td style="color:red">JavaSE</td> </c:when> <c:when test="${book.category eq 2 }"> <td style="color:blue">JavaEE</td> </c:when> <c:when test="${book.category eq 3 }"> <td style="color:green">JavaFramework</td> </c:when> </c:choose> </tr> </c:forEach> </table> </body> </html>
运行结果:
链接页面:
查看所有:
查看JavaSE分类:
查看JavaEE分类:
查看JavaFramework分类:
那么在Tomcat服务器下的webapps中的Book项目中会有静态页面生成: