本文隶属于分类

Web开发

推荐文章

广告推荐

技术交流学习或者有任何问题欢迎加群 : 154514123 爱上编程

跪求答案!一天一夜,发现这是一个世界上最奇怪的Servlet中用request.getParameter()取参数问题

package mybbs;

import java.io.IOException;
import java.io.PrintWriter;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CommentServlet extends HttpServlet {

private String counterComment="dfsak";

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("gb2312");
response.setCharacterEncoding("gb2312");

counterComment=request.getParameter("counterComment");
counterComment=new String(counterComment.getBytes("ISO-8859-1"),"GB2312");
//竟然用了这句都没有效;
System.out.println(counterComment);
System.out.println(request.getParameter("counterComment"));
System.out.println((request.getParameter("counterComment")==null)+"----");
System.out.println((counterComment==null)+"boolean2");

if(counterComment==null){

System.out.println("if语句里面");

}else{
System.out.println(counterComment+"=====");

System.out.println("else语句里面");
}
}
}


//输出如下:
/*
null
null---------------->,大家看看嘛,都明明是null了,明明该执行if语句了,结果还是错啊,永远都执行else语句啊,嗨
false----
falseboolean2
null=====
else语句里面
*/


------解决方案--------------------
是有问题啊
1)counterComment=request.getParameter("counterComment");
2)counterComment=new String(counterComment.getBytes("ISO-8859-1"),"GB2312");
如果1) counterComment== null 成立 则 2)应该是异常 
counterComment.getBytes("ISO-8859-1") 相当于 null.getBytes("ISO-8859-1") 

最后分析 counterComment =="null" counterComment !=null

技术交流学习或者有任何问题欢迎加群 : 154514123 爱上编程

广告推荐

讨论区