国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

18143453325 在線咨詢 在線咨詢
18143453325 在線咨詢
所在位置: 首頁 > 營銷資訊 > 建站知識(shí) > 在html頁面中取得session中的值的方法

在html頁面中取得session中的值的方法

時(shí)間:2023-03-03 07:24:01 | 來源:建站知識(shí)

時(shí)間:2023-03-03 07:24:01 來源:建站知識(shí)

在html頁面中取得session中的值的方法:

這篇文章主要介紹了在html頁面中取得session中的值的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1.首先呢session的key-value都是存在server的,瀏覽器HTML頁面是沒有辦法直接取得session中的值,只有在html里能通過js拿到j(luò)esessionid之類的東西。

1.1、數(shù)據(jù)量如果小,可以考慮放到cookie里,傳到客戶端,html里用js就可以拿到。

1.2、如果數(shù)據(jù)量大,可以考慮單獨(dú)做一個(gè)jsp或servlet,根據(jù)傳來的session的key,返回序列化的session的值,比如json之類的。html里用js通過ajax獲取。這種方式復(fù)雜了點(diǎn),多一次遠(yuǎn)程訪問,但是靈活方便。

如:<input type="text" value='<%#Session["username"]%>'>

2.或者得通過后臺(tái)才能獲取,session是存在服務(wù)器端的,如果你用cookie的話,可以通過js獲取。

問題描述:session中保存著UserInfo對(duì)象,成功登錄后,在html中顯示“歡迎xxx”

解決方法:通過ajax,json獲取UserInfo數(shù)據(jù),再顯示

1.js

<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
 
<script type="text/javascript">
    $(function() {
        $.ajax({
            type : "get",
            url : "login!getLoginName.action",
            dataType : "text",
            success : function(result) {
                document.getElementsByTagName('b')[0].innerHTML=result;
            },
            error : function() {
                alert("請(qǐng)求失敗");
            }
        });
    });
</script>

2.頁面

<html>
<head>
<title>管理頁面</title>
</head>
<body>
     <table>
            <tr>
                        <td width="74%" height="38" class="admin_txt">管理員:<b></b>您好,感謝登陸使用!</td>
 
                    </tr>
                </table>
</body>
</html>

3.實(shí)體:UserInfo

public class UserInfo {
    private int UserInfoId;
    private String userInfoName;
    private String UserInfoPsw;
    //省略get,set

4.LoginAction中:

public void getLoginName() {
        System.out.println("getLoginUser");
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/plain;charset=UTF-8");
        PrintWriter out;
        try {
            String userName = ((UserInfo) ActionContext.getContext()
                    .getSession().get("user")).getUserInfoName();
            System.out.println(userName);
            out = response.getWriter();
            out.print(userName);
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }

3.用response.sendRedirect("a.html?param=hello");用下面的JS方法

如:.

var v=getUrlParameter('param');
function getUrlParameter( name ){
name = name.replace(/[
]/,"/[").replace(/[
]/,"http:///]");
var regexS = "[//?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec(window.parent.location.href );
if( results == null ) return ""; else {
return results[1];

}
}

以上幾種方法在html頁面中取得session中的值.

總結(jié)

到此這篇關(guān)于在html頁面中取得session中的值的方法的文章就介紹到這了,更多相關(guān)html頁面取得session值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

來源:腳本之家

鏈接:https://www.jb51.net/html5/739948.html

關(guān)鍵詞:方法

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉