doGet和doPost的数据的获取

在Servlet里面,其中有一种方式是通过
<a href="<%=request.getContextPath() %>/search?username=<%=name %>" title="查看个人资料" target="_blank">
来提交信息,在点击点击链接的时候,username的数据传给链接到的页面,在打开的页面里,因为他是一个doGet方法,所以汉字可能会产生乱码,这时候在接受页面要通过
String notUsername = req.getParameter("username");
String username = new String(notUsername.getBytes("ISO-8859-1"),"UTF-8");

的方式,把接受过来汉字通过UTF-8的编码方式进行解码,这样才能保证得到的文字不出现乱码。

但是,在正常的doPost页面,不能通过这种方式,而是应该使用
String username = req.getParameter("username");
的方式来进行数据的获取,各种方式必须使用对应的格式,否则会产生乱码。
综上,在doPost方式中,应该使用
String username = req.getParameter("username");
在doGet方式中,应该使用
String notUsername = req.getParameter("username");
String username = new String(notUsername.getBytes("ISO-8859-1"),"UTF-8");