The method of getting the value of session in HTML page

Time:2020-11-25

1. First of all, the key values of the session exist in the server. There is no way for the browser’s HTML page to directly obtain the value in the session. Only in HTML can we get jesessionid and other things through JS.

1.1. If the amount of data is small, you can consider putting it in a cookie and passing it to the client. You can get it in HTML with JS.
1.2. If there is a large amount of data, you can consider making a separate JSP or servlet. According to the transmitted session key, the value of the serialized session, such as JSON, can be returned. HTML in JS through Ajax. This method is a little more complicated, one more remote access, but flexible and convenient.
as:<input type="text" value='<%#Session["username"]%>'>2. Or it can be obtained through the background. The session exists on the server side. If you use a cookie, you can get it through JS.

Problem Description: the userinfo object is saved in the session. After successful login, the “welcome XXX” will be displayed in HTML

Solution: get the userinfo data through Ajax and JSON, and then display it

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 ("request failed");
            }
        });
    });
</script>

2. Page

<html>
<head>
< title > Administration page
</head>
<body>
     <table>
            <tr>
                        <td width="74%" height="38" class="admin_ Txt "> administrator: < b > < / b > Hello, thank you for logging in! </td>
 
                    </tr>
                </table>
</body>
</html>

3. Entity: userinfo

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

4. In 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();
        }
 
    }

3response.sendRedirect("a.html?param=hello");Use the following JS method

For example:


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

}
}

The above methods get the value of session in HTML page

summary

This article about the method of getting the value of session in HTML page will be introduced here. For more related HTML pages, please search the previous articles of developeppaer or continue to browse the related articles below. I hope you can support developeppaer more in the future!