jsp传值乱码解决办法实例怎么办

jsp传值乱码解决办法实例怎么办,在jsp中,我们经常从数据库读取数据返回客户端,但我们常常在制作时出现乱码现象,所以我们可以用<%request.setCharacterEncoding(“UTF-8″);%>这个方法来保证中文的正确输出,下面举个例子吧,

我们要接住表单的值或者把数据库数据打印出来的之前,先把<%request.setCharacterEncoding(“UTF-8″);%>放在他们的前面,然后,表单的提交方式必须是post,即method=”post”,这样可以闭避免乱码了,请看下面:

<%request.setCharacterEncoding(“UTF-8″);%>

<form action=”" method=”post”>

姓名:<input type=”text” name=”name”/>

<br/>

性别:<input type=”text” name=”sex” />

<%

String name=requset.getParameter(“name”);

String sex=request.getParameter(“sex”);out.print(name);

out.print(sex);

%>

</form>

 

或者有时用户登陆时,我们需要在某一页用到用户名或者密码,我们可以用下面这种方法来记住,在其他页面可以随便调用,如:

<%request.setCharacterEncoding(“UTF-8″);%>

<form action=”" method=”post”>

用户名:<input type=”text” name=”name”/>

<br/>

密码:<input type=”password” name=”password” />

<form/>

String name=requset.getParameter(“name”);

String password=request.getParameter(“password”);

 

application.setAttribute(“names”,name);
application.setAttribute(“passwords”,password);密码和用户名就这样被记住了,在其他也可以随便调用,如下

application.getAttribute(“names”);

application.getAttribute(“passwords”);

<%

out.print(application.getAttribute(“names”));

out.print(application.getAttribute(“passwords”)

 

%>

这样就会输出文本框的值 本文链接地址: jsp传值乱码解决办法实例怎么办