jsp傳值中文亂碼問題解決方法示例介紹
更新時(shí)間:2013年07月19日 17:12:25 作者:
在jsp中,我們經(jīng)常從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)返回客戶端,但我們常常在制作時(shí)出現(xiàn)亂碼現(xiàn)象,下面與大家分享下jsp傳值亂碼解決辦法,有類似情況的朋友可以參考下哈,希望對(duì)大家有所幫助
在jsp中,我們經(jīng)常從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)返回客戶端,但我們常常在制作時(shí)出現(xiàn)亂碼現(xiàn)象,所以我們可以用<%request.setCharacterEncoding("UTF-8");%>這個(gè)方法來保證中文的正確輸出,下面舉個(gè)例子吧,
我們要接住表單的值或者把數(shù)據(jù)庫(kù)數(shù)據(jù)打印出來的之前,先把<%request.setCharacterEncoding("UTF-8");%>放在他們的前面,然后,表單的提交方式必須是post,即method="post",這樣可以閉避免亂碼了,請(qǐng)看下面:
<%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>
或者有時(shí)用戶登陸時(shí),我們需要在某一頁(yè)用到用戶名或者密碼,我們可以用下面這種方法來記住,在其他頁(yè)面可以隨便調(diào)用,如:
<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);密碼和用戶名就這樣被記住了,在其他也可以隨便調(diào)用,如下
application.getAttribute(“names");
application.getAttribute("passwords");
<%
out.print(application.getAttribute(“names"));
out.print(application.getAttribute(“passwords")
%>
這樣就會(huì)輸出文本框的值
我們要接住表單的值或者把數(shù)據(jù)庫(kù)數(shù)據(jù)打印出來的之前,先把<%request.setCharacterEncoding("UTF-8");%>放在他們的前面,然后,表單的提交方式必須是post,即method="post",這樣可以閉避免亂碼了,請(qǐng)看下面:
復(fù)制代碼 代碼如下:
<%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>
或者有時(shí)用戶登陸時(shí),我們需要在某一頁(yè)用到用戶名或者密碼,我們可以用下面這種方法來記住,在其他頁(yè)面可以隨便調(diào)用,如:
復(fù)制代碼 代碼如下:
<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);密碼和用戶名就這樣被記住了,在其他也可以隨便調(diào)用,如下
application.getAttribute(“names");
application.getAttribute("passwords");
<%
out.print(application.getAttribute(“names"));
out.print(application.getAttribute(“passwords")
%>
這樣就會(huì)輸出文本框的值
相關(guān)文章
CORBA對(duì)象生命周期之實(shí)現(xiàn)和內(nèi)存管理
CORBA對(duì)象生命周期之實(shí)現(xiàn)和內(nèi)存管理...2006-10-10jsp實(shí)現(xiàn)checkbox的ajax傳值實(shí)例
這篇文章主要介紹了jsp實(shí)現(xiàn)checkbox的ajax傳值,實(shí)例分析了針對(duì)checkbox的Ajax使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02JSP使用ajaxFileUpload.js實(shí)現(xiàn)跨域問題
這篇文章主要介紹了JSP使用ajaxFileUpload.js實(shí)現(xiàn)跨域問題的相關(guān)內(nèi)容,本文介紹非常詳細(xì),具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-04-04使用JSP制作一個(gè)超簡(jiǎn)單的網(wǎng)頁(yè)計(jì)算器的實(shí)例分享
這篇文章主要介紹了一個(gè)使用JSP制作超簡(jiǎn)單的網(wǎng)頁(yè)計(jì)算器的實(shí)例,文中的方法利用到了JavaBean組件,需要的朋友可以參考下2016-04-04