进入表单页面出现乱码,request传值为null,request出现乱码,解决经验总结

 一、进入表单页面出现乱码

                一开始的表单代码:
 

<html>
<head><title>填写表单</title><head>
<body>
<center>
	<form action="input.jsp" method="post" >
		姓名:<input type="text" name="username" value="请输入你的姓名:"><br>
		学号:<input type="text" name="userid" value="" size="10" maxlength="10"><br>
		请输入要显示的内容:<textarea name="note" cols="30" rows="3">
								请输入你的建议:
							</textarea><br>
		<input type="submit" value="提交"><br>
		<input type="reset" value="重置">
	</form>
</center>
</body>

</html>

进入表单页面 是这样的:

后来 找到了一篇解救我的文章,用以下方法:

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>填写表单</title><head>
<body>
<center>
	<form action="input.jsp" method="post" enctype="multipart/form-data" accept-charset="UTF-8">
		姓名:<input type="text" name="username" value="请输入你的姓名:"><br>
		学号:<input type="text" name="userid" value="" size="10" maxlength="10"><br>
		请输入要显示的内容:<textarea name="note" cols="30" rows="3">
								请输入你的建议:
							</textarea><br>
		<input type="submit" value="提交"><br>
		<input type="reset" value="重置">
	</form>
</center>
</body>

</html>

添加第二行和第五行多出来的句子,运行结果恢复正常了:
  

填写: 

 

二、request传值为null

提交之后,转跳到jsp页面就request传值为空了!!!!:

 

为什么呢?????然后我又找到了一篇解救我于水火之中的文章,所以有了以下解决办法:
修改了第五行:

<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>填写表单</title><head>
<body>
<center>
	<form action="input.jsp" method="post" accept-charset="UTF-8">
		姓名:<input type="text" name="username" value="请输入你的姓名:"><br>
		学号:<input type="text" name="userid" value="" size="10" maxlength="10"><br>
		请输入要显示的内容:<textarea name="note" cols="30" rows="3">
								请输入你的建议:
							</textarea><br>
		<input type="submit" value="提交"><br>
		<input type="reset" value="重置">
	</form>
</center>
</body>

</html>

 

 运行结果,request传值正常:

 

 

 三、request乱码

刚开始的jsp代码:

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>显示</title></head>
<body>
	<%
		request.setCharacterEncoding("GBK");
		
		String name=request.getParameter("username");
		String id=request.getParameter("userid");
		String note=request.getParameter("note");
		
		
	%>
	<h3>姓名:<%=name%></h3>
	<h3>学号:<%=id%></h3>
	<h1>建议:<%=note%></h1>
</body>
</html>

运行html填写完后提交到jsp页面:

 jsp页面乱码:

解决方法:把GBK换成UTF-8  (那个有用用哪个,主要是要和提交表单的html一致,所以我选择了UTF-8)

<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head><title>显示</title></head>
<body>
	<%
		request.setCharacterEncoding("utf-8");
		
		String name=request.getParameter("username");
		String id=request.getParameter("userid");
		String note=request.getParameter("note");
		
		
	%>
	<h3>姓名:<%=name%></h3>
	<h3>学号:<%=id%></h3>
	<h1>建议:<%=note%></h1>
</body>
</html>

 

 结果:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值