用过滤器解决MVC添加书本信息时的乱码问题

一:View  addBook.jsp

 1 <%@page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8" import="java.util.*" import="nuc.sw.EL.bean.EL"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="ELServlet" method="post">
11  <table  align="left" >
12  <tr>
13   <td>书名:</td>
14   <td><input type="text" name="bookName"></td>
15  </tr>
16  <tr>
17   <td>作者:</td>
18   <td><input type="text" name="authorName"></td>
19  </tr>
20  <tr>
21   <td>定价:</td>
22   <td><input type="text" name="bookPrice"></td>
23  </tr>
24  <tr>
25   <td><input type="submit" value="添加"></td>
26  </tr>
27 </table>
28 </form>
29 </body>
30 </html>

二:Model  nuc.sw.EL.bean   EL.java

 1 package nuc.sw.EL.bean;
 2
 3 public class EL {
 4   private String bookName;
 5   private String authorName;
 6   private float bookPrice;
 7 public String getBookName() {
 8     return bookName;
 9 }
10 public void setBookName(String bookName) {
11     this.bookName = bookName;
12 }
13 public String getAuthorName() {
14     return authorName;
15 }
16 public void setAuthorName(String authorName) {
17     this.authorName = authorName;
18 }
19 public Float getBookPrice() {
20     return bookPrice;
21 }
22 public void setBookPrice(Float bookPrice) {
23     this.bookPrice = bookPrice;
24 }
25
26 }

三:Control   nuc.sw.EL.servlet   ELServlet.java

 1 package nuc.sw.EL.servlet;
 2
 3 import java.io.IOException;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import nuc.sw.EL.bean.EL;
13
14 /**
15  * Servlet implementation class ELServlet
16  */
17 public class ELServlet extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19
20     /**
21      * @see HttpServlet#HttpServlet()
22      */
23     public ELServlet() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
27
28     /**
29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
30      */
31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
32         // TODO Auto-generated method stub
33         response.getWriter().append("Served at: ").append(request.getContextPath());
34     }
35
36     /**
37      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
38      */
39     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
40         // TODO Auto-generated method stub
41         //doGet(request, response);
42         EL el=new EL();
43         el.setBookName(request.getParameter("bookName"));
44         el.setAuthorName(request.getParameter("authorName"));
45         el.setBookPrice(Float.parseFloat(request.getParameter("bookPrice")));
46         //request.setAttribute("el", el);
47         List<EL> list;
48         if(request.getSession().getAttribute("booklist")==null){
49             list= new ArrayList<EL>();
50             list.add(el);
51         }
52         else{
53             list=(ArrayList<EL>)request.getSession().getAttribute("booklist");
54             list.add(el);
55         }
56
57         request.getSession().setAttribute("booklist", list);
58         request.getRequestDispatcher("showBookInfo.jsp").forward(request, response);
59         //request.getRequestDispatcher("addBook.jsp").forward(request, response);
60     }
61
62 }

四:显示书本信息 showBookInfo.jsp

 1 <%@page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8" import="java.util.*,nuc.sw.EL.bean.EL"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <table  align="right" border="1">
11          <caption>全部书籍信息</caption>
12          <tr>
13           <td>书名:</td>
14           <td>作者:</td>
15          <td>定价:</td>
16   </tr>
17   <%
18         Iterator<EL> iter=((ArrayList<EL>)session.getAttribute("booklist")).iterator();
19         while(iter.hasNext()){
20           pageContext.setAttribute("book",iter.next());
21    %>
22
23     <tr>
24       <td>${book.bookName} </td>
25       <td>${book.authorName} </td>
26       <td>${book.bookPrice} </td>
27    </tr>
28 <%
29     }
30 %>
31
32 </table>
33 <a href="addBook.jsp">继续添加</a>
34 </body>
35 </html>

五:设置汉字乱码  nuc.sw.book.filter   EncodingFilter.java

 1 package nuc.sw.book.filter;
 2
 3 import java.io.IOException;
 4 import javax.servlet.Filter;
 5 import javax.servlet.FilterChain;
 6 import javax.servlet.FilterConfig;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.ServletRequest;
 9 import javax.servlet.ServletResponse;
10
11 /**
12  * Servlet Filter implementation class EncodingFilter
13  */
14 public class EncodingFilter implements Filter {
15
16     /**
17      * Default constructor.
18      */
19     public EncodingFilter() {
20         // TODO Auto-generated constructor stub
21     }
22
23     /**
24      * @see Filter#destroy()
25      */
26     public void destroy() {
27         // TODO Auto-generated method stub
28     }
29
30     /**
31      * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
32      */
33     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
34         // TODO Auto-generated method stub
35         // place your code here
36
37         // pass the request along the filter chain
38         request.setCharacterEncoding("utf-8");
39         chain.doFilter(request, response);
40     }
41
42     /**
43      * @see Filter#init(FilterConfig)
44      */
45     public void init(FilterConfig fConfig) throws ServletException {
46         // TODO Auto-generated method stub
47     }
48
49 }

六:运行结果

时间: 2024-10-13 01:20:30

用过滤器解决MVC添加书本信息时的乱码问题的相关文章

用过滤器解决MVC添加书本信息时的登录安全问题

一:View  addBook.jsp 1 <%@page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8" import="java.util.*" import="nuc.sw.EL.bean.EL"%> 3 <!DOCTYPE html PUBLIC "-//W3C//

使用MVC添加书本信息

一:View addBook.jsp 1 <%@page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8" import="java.util.*" import="nuc.sw.EL.bean.EL"%> 3 <!DOCTYPE html PUBLIC "-//W3C//D

解决Eclipse添加新server时无法选择Tomcat7.0

解决方法:这时打开工作空间目录下的.metadata\.plugins\org.eclipse.core.runtime\.settings文件夹,删除org.eclipse.wst.server.core.prefs和org.eclipse.jst.server.tomcat.core.prefs这两个文件,再重启eclipse就可以了. 原文地址:https://www.cnblogs.com/sg-yys/p/12172871.html

mvc EF 数据保存时,报错:”对一个或多个实体的验证失败……“之解决

在EF5.0添加实体数据到数据库的时候,出现“对一个或多个实体的验证失败.有关详细信息,请参见“EntityValidationErrors”属性这个错误 解决: SaveChanges前先关闭验证实体有效性(ValidateOnSaveEnabled)这个开关 db.Configuration.ValidateOnSaveEnabled = false; int count = db.SaveChanges(); db.Configuration.ValidateOnSaveEnabled =

使用动态代理解决方法调用前后添加日志信息

一般情况,在每个调用的方法中直接添加日志信息,存在如下问题: 1.代码混乱:越来越多的非业务需求加入(如日志和验证等)后,原有的业务方法急剧膨胀,每个方法在处理核心逻辑的同时还必须兼顾其他多个关注点: 2.代码分散:以日志需求为例,只是为了满足这个单一的需求,就不得不在多个模块里多次重复相同的日志代码,如果日志需求发生变化,必须修改所有的模块. 针对以上问题,使用动态代理带解决. 代理设计模式的原理:使用一个代理将对象包装起来,然后用该代理取代原始对象.任何原始对象的调用都要通过代理.代理对象决

向SDE图层中添加大量数据时,出现ORA-00604以及ORA-01000的解决办法

转自原文 向SDE图层中添加大量数据时,出现ORA-00604以及ORA-01000的解决办法 写了一个小程序,从一个列表中读取坐标串,每个坐标串生成一个IPolygon,然后将这些Polygon添加到一个SDE图层中, 向SDE中添加要素的初始代码类似下面: IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace; workspaceEdit.StartEditing(true); workspaceEdit.StartEditOper

ecshop二次开发 购物时为每件商品添加备注信息的修改方法

目标: 用户在选购商品加入购物车前可以为商品添加备注信息 加入购物车后可以在购物车查看备注信息 店主在查看订单时可以看到用户为商品添加的备注信息 用户在个人中心中可以看到商品的备注信息 目标一: 打开ecshop/goods.php,搜索"display"可以看到这句: $smarty->display('goods.dwt', $cache_id); 打开模板目录下的goods.dwt,搜索"<!-- {* 结束循环可选属性 *} -->",在这

记安装EP时在指定BCP账户信息时提示AOS无法访问的解决方法

因为卡在这个问题上好久,外加同事也有遇到,因此记下来! 环境:Windows Server 2012 R2 Standard + Dynamics AX 2012 R2 + SharePoint 2010 + MS SQL 2012 SP1 前提:机器已加域,且账户为域账户. 问题描述: 在安装Enterprise Portal(EP)时,在指定Business Connector proxy账户信息时,出现如下问题: 解决思路:[本文地址:http://www.cnblogs.com/Inte

[Q]“获取AutoCAD安装信息时失败...”解决方法

“获取AutoCAD安装信息时失败...”解决方法:在“setup.exe”上右键,以管理员权限运行即可.