htm传入参数到servlet乱码
本帖最后由 a156435646 于 2013-01-21 18:40:21 编辑
htm页面有<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<td><a href="/shoppingcart/servlet/addShoppingCart?id=0006&name=掌上游戏机&price=638.00">这句
当servlet
String name = request.getParameter("name");
System.out.println(name);
中,输出的是问号乱码
而且当name超过4个中文时候,
String name = request.getParameter("price");
System.out.println(price);
输出是null
源代码在下面
addShoppiingCart:
package com.v512.cart;
import
java.io.IOException;
import java.io.PrintWriter;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import webbook.util.StringUtil;
public class AddShoppingCartServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -2355034166575174650L;
//private static final long serialVersionUID = -6552354364752194751L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
IOException {
request.setCharacterEncoding("UTF-8");//针对post提交的转码
response.setContentType("text/html;charset=UTF-8");//针对url提交的乱码处理
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");//针对post提交的转码
response.setContentType("text/html;charset=UTF-8");//针对url提交的乱码处理
HttpSession session = request.getSession();
ShoppingCart cart = (ShoppingCart) session.getAttribute("shoppingcart");
if (cart == null) {
cart = new ShoppingCart();
session.setAttribute("shoppingcart", cart);
}
String id = request.getParameter("id");
String name = request.getParameter("name");
String quantity = request.getParameter("q");
String price = request.getParameter("price");
System.out.println(id);
System.out.println(name);
System.out.println(price);
if (StringUtil.validateNull(id) || StringUtil.validateNull(name) || StringUtil.validateNull(price)) {
System.out.println("111");
printError(request, response);
return;
}
//id = StringUtil.filterHtml(id);
//name = StringUtil.filterHtml(name);
try {
if (StringUtil.validateNull(quantity)) {
cart.addCartItem(new CartItem(id, name, 1, Double.parseDouble(price)));
} else {
cart.addCartItem(new CartItem(id,