The method of getting the value of session in HTML page


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


<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(function() {
            type : "get",
            url : "login!getLoginName.action",
            dataType : "text",
            success : function(result) {
            error : function() {
                Alert ("request failed");

2. Page

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

3. Entity: userinfo

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

4. In loginaction:

public void getLoginName() {
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter out;
        try {
            String userName = ((UserInfo) ActionContext.getContext()
            out = response.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block

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

For example:

var v=getUrlParameter('param');
function getUrlParameter( name ){
name = name.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


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!