Java web项目开发中,解决乱码的一种很好的方式

今天在写一个游戏后台系统项目时,通过jsp向后台提交表单数据,通过实体类对象获取表单内容遇到了乱发问题。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>代理商个人信息</title>
<base href="<%=basePath%>">
<link  href="css/systemSetttings/inc_style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body{
margin-top: 0px;
}
.red{
color: red;
}
</style>
<script src="js/basevalue.js"></script>
<script src="js/jquery-1.11.1.js"></script>
<!-- 验证JS -->
<script src="js/operation.js"></script>
</head>
<body>
<form name="form1" method="post" action="agent/save.do" οnsubmit="return checkForm()">
<table width="100%" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#A4B6D7"
bgcolor="F2F8FF" class="admin_table2">
<tr>
<td class="title_03">
<strong>新增代理界面</strong>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td style="height: 5px">
</td>
</tr>
</table>
<table width="100%" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="A4B6D7"
bgcolor="F2F8FF" class="admin_table">
<tr>
<td align="right" width="40%" style="height:20px;">
账户:
</td>
<td colspan="2" align="left"  style="height:20px;">
<input type="text" name="account" id="account">
<span id="accountMSG" class="red"></span>
</td>
</tr>
<tr>
<td align="right">
房卡:
</td>
<td colspan="2" align="left">
<input type="text" name="roomCard" id="roomCard">
<span id="roomCardMSG" class="red"></span>
</td>
</tr>
<td align="right">
<input type="submit" value=" 保存 "/>
</td>
<td colspan="2" align="left">
<input type="button" value=" 取消 " id="cancel"/>
</td>
</tr>
</table>
</form>
</body>
</html>

解决方案在web.xml中添加过滤器<filter>

<!-- 配置乱码解决方案 -->
  <filter>
<filter-name>characterEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值