说明 web项目在服务器部署出现了一下情况
删除删除不了
epp/EpServlet?type=del&ep=豫000 URL地址传了中文
点修改操作到另一个页面 一个值是中文乱码 一个是null
不知道该怎么解决 代码如下 帮我看看应该如何解决服务器Web乱码URLJava
删除删除不了
epp/EpServlet?type=del&ep=豫000 URL地址传了中文
点修改操作到另一个页面 一个值是中文乱码 一个是null
不知道该怎么解决 代码如下 帮我看看应该如何解决服务器Web乱码URLJava
package com.uniwell.dao;import java.util.Map;import com.uniwell.until.MapSortDemo;import redis.clients.jedis.Jedis;public class RedisDaoInpl {
//public static Jedis redis = new Jedis("localhost");
public static Jedis redis = new Jedis("localhost");
public static Map<String, String> getMap() {
//RedisDao redisDao = new RedisDao();
Map<String, String> map = redis.hgetAll("devid-name");
MapSortDemo mapSort = new MapSortDemo();
return mapSort.sortMapByKey(map);//对map进行排序
} /*public static void main(String[] args) {
Map<String, String> map = RedisDaoInpl.getMap();
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}*/}
package com.uniwell.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;public class EncodingFilter implements Filter
{ protected String encoding; protected FilterConfig filterConfig;
public EncodingFilter() {
encoding = null;
filterConfig = null;
}
@Override
public void destroy()
{
encoding = null;
filterConfig = null;
} @Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException,
ServletException
{
if (encoding != null)
arg0.setCharacterEncoding(encoding);
else
arg0.setCharacterEncoding("UTF-8");
arg2.doFilter(arg0, arg1);
} @Override
public void init(FilterConfig filterconfig) throws ServletException
{
filterConfig = filterconfig;
encoding = filterconfig.getInitParameter("UTF-8");
}}
package com.uniwell.until;import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;import redis.clients.jedis.Jedis;
/**
* map按键排序
* @author Administrator
*
*/
public class MapSortDemo {
/*public static Jedis redis = new Jedis("localhost");
public static void main(String[] args) {
RedisDao redisDao = new RedisDao();
Map<String, String> map = new TreeMap<String, String>(); map.put("豫d007", "kfc");
map.put("豫a007", "wnba");
map.put("豫c007", "nba");
map.put("豫b007", "cba"); Map<String, String> resultMap = sortMapByKey(map); //按Key进行排序 for (Map.Entry<String, String> entry : resultMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
}*/
/**
* 使用 Map按key进行排序
* @param map
* @return
*/
public static Map<String, String> sortMapByKey(Map<String, String> map) {
if (map == null || map.isEmpty()) {
return null;
} Map<String, String> sortMap = new TreeMap<String, String>(
new MapKeyComparator()); sortMap.putAll(map); return sortMap;
}
}package com.uniwell.until;import java.util.Comparator;class MapKeyComparator implements Comparator<String>{ @Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
}
后台在
String param = request.getParameter("ep");
param = new String(param.getBytes( "iso-8859-1" ), "UTF-8" )
字符集看你jsp上用的多试试或者用post方式
package com.uniwell.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.uniwell.dao.RedisDaoInpl;public class EpServlet extends HttpServlet { /**
*
*/
private static final long serialVersionUID = 119387545093364124L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String user = request.getParameter("userName");
String pass = request.getParameter("password");
String type = request.getParameter("type");// 接收页面传递的参数
RedisDaoInpl redisDaoInpl = new RedisDaoInpl();
if ("login".equals(type)) {// 登录操作
if ("admin".equals(user) && "admin".equals(pass)) {
response.sendRedirect(request.getContextPath() + "/acces.jsp");
// 从redis数据库中取出数据
Map<String, String> map = redisDaoInpl.getMap();
this.getServletContext().setAttribute("map", map);
} else {
response.sendRedirect("index.jsp");
}
} else if ("del".equals(type)) {// 删除操作 String ep = request.getParameter("ep");
String car = new String(ep.getBytes("ISO-8859-1"),"UTF-8");
redisDaoInpl.redis.hdel("devid-name", car);
//System.err.println(car); Map<String, String> map = redisDaoInpl.getMap();
this.getServletContext().setAttribute("map", map);
response.sendRedirect(request.getContextPath() + "/acces.jsp");
/*request.getRequestDispatcher("acces.jsp")
.forward(request, response);*/
} else if ("edit".equals(type)) {// 修改操作
String ep = request.getParameter("ep");
String car = new String(ep.getBytes("ISO-8859-1"),"UTF-8");
String value = redisDaoInpl.redis.hget("devid-name", car);
request.setAttribute("car", car);
request.setAttribute("value", value);
request.getRequestDispatcher("up.jsp").forward(request, response); } else if ("add".equals(type)) {// 添加操作
String ep = request.getParameter("num");//
//String car = new String(ep.getBytes("ISO-8859-1"),"UTF-8");
String sd = request.getParameter("sd");//
//System.out.println(ep);
redisDaoInpl.redis.hset("devid-name", ep, sd);
Map<String, String> map = redisDaoInpl.getMap();
this.getServletContext().setAttribute("map", map);
response.sendRedirect(request.getContextPath() + "/acces.jsp");
} else {// 更新操作
String ep = request.getParameter("num");
String val = request.getParameter("val");
RedisDaoInpl redisdao = new RedisDaoInpl();
redisdao.redis.hset("devid-name", ep, val);
Map<String, String> map = redisDaoInpl.getMap();
this.getServletContext().setAttribute("map", map);
response.sendRedirect(request.getContextPath() + "/acces.jsp");//
} }
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN" xml:lang="zh-CN">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Content-Language" content="zh-CN"/>
<title>
高速参数标准
</title>
<link rel="Shortcut Icon" href="/favicon.ico" type="image/x-icon" />
<style type="text/css">
* {
font-family: Tahoma, Arial, Helvetica, Sans-serif, "宋体";
}table {
width: 700px;
margin: 0px auto;
font: Georgia 11px;
font-size: 12px;
color: #333333;
text-align: center;
border-collapse: collapse; /*细线表格代码*/
}table td {
border: 1px solid #999; /*细线表格线条颜色*/
height: 22px;
}caption {
text-align: center;
font-size: 12px;
font-weight: bold;
margin: 0 auto;
}tr.t1 td {
background-color: #fff;
}tr.t2 td {
background-color: #eee;
}tr.t3 td {
background-color: #ccc;
}th,tfoot tr td {
font-weight: bold;
text-align: center;
background: #c5c5c5;
}th {
line-height: 30px;
height: 30px;
}tfoot tr td {
background: #fff;
line-height: 26px;
height: 26px;
}thead {
border: 1px solid #999;
}thead tr td {
text-align: center;
}#page {
text-align: center;
float: right;
}#page a,#page a:visited {
width: 60px;
height: 22px;
line-height: 22px;
border: 1px black solid;
display: block;
float: left;
margin: 0 3px;
background: #c9c9c9;
text-decoration: none;
}#page a:hover {
background: #c1c1c1;
text-decoration: none;
}.grayr {
padding: 2px;
font-size: 11px;
background: #fff;
float: right;
}.grayr a {
padding: 2px 5px;
margin: 2px;
color: #000;
text-decoration: none;;
border: 1px #c0c0c0 solid;
}.grayr a:hover {
color: #000;
border: 1px orange solid;
}.grayr a:active {
color: #000;
background: #99ffff
}.grayr span.current {
padding: 2px 5px;
font-weight: bold;
margin: 2px;
color: #303030;
background: #fff;
border: 1px orange solid;
}.grayr span.disabled {
padding: 2px 5px;
margin: 2px;
color: #797979;
background: #c1c1c1;
border: 1px #c1c1c1 solid;
}
</style>
</head><body>
<h1 align="center">高速限速</h1>
<center>
<form action="EpServlet?type=add" method="post" >
车牌号: <input type="text" name="num" />
高速时速:<select name="sd">
<option>70 20 20 180</option>
<option>80 20 20 180</option>
</select>
<input type="submit" value="提交" />
</form>
</center>
<table align="center" id=“tb_1”>
<thead>
<tr>
<th >车牌号</th>
<th >速度参数</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tab">
<c:forEach items="${map}" var="item"> <tr>
<td>${item.key}</td>
<td>${item.value}</td>
<td><a href="EpServlet?type=edit&ep=${item.key}">修改</a>|| <a
href="EpServlet?type=del&ep=${item.key}"
onclick="return confirm('确认要删除吗?')"> 删除</a></td>
</tr> </c:forEach>
</tbody>
</table></body>
</html>
修改操作
<%@ 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>
<%
String var = (String) request.getAttribute("value");
//空格解决思路 在value后面加双引号
%>
</head>
<body>
<form action="EpServlet?type=up" method="post">
车 牌 号:<input type='text' readOnly value=<%=request.getAttribute("car") %> name="num" /></br>
时速参数:<input type='text' value="<%=var%>" name="val" /></br>
<input type="submit" value="提交">
</form></body>
</html>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>EpServlet</servlet-name>
<servlet-class>com.uniwell.servlet.EpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EpServlet</servlet-name>
<url-pattern>/EpServlet</url-pattern>
</servlet-mapping>
<!-- 字符过滤器配置 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.uniwell.filter.EncodingFilter</filter-class> </filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 字符过滤器配置 -->
</web-app>
epp/EpServlet?type=edit&ep=豫000
String car = new String(ep.getBytes("ISO-8859-1"),"UTF-8");
你调试下 看car值是不是乱码 如果是的话 jsp头文件的编码格式换一换