I recently learned ajax technology and changed the front-end to HTML static web pages to write. How to get the logged-in user name? The first thing everyone thinks of is to use Cookie and Session. This can be done on a JSP page, but not on a static HTML page.
First of all, what are cookies and sessions? First of all, everyone knows that the HTTP protocol is a stateless protocol. Once the data exchange is completed, the connection between the client and the server will be closed, and a new connection needs to be established to exchange data again. This means that the server cannot track the session from the connection. At this time, cookie session tracking technology appeared. Issue a pass to the clients, one for each person. No matter who visits, they must bring their own pass. This way the server can confirm the client's identity from the pass. This is how cookies work, they are stored on the client. Session is a mechanism used by the server to record client status. It is equivalent to establishing a user profile table on the server. You only need to query the profile table to determine whether it matches the current client. Session is also a key-value attribute pair. It reads and writes customer status information through the getAttribute(Stringkey) and setAttribute(String key, Object value) methods. In the Servlet, the client's Session is obtained through the request.getSession() method.
HTML gets the login user name . Session is one of the nine built-in objects of JSP. It is not very friendly to HTML, so how to obtain static resources? The first is to create a transitional Servlet, save the user session, and obtain it. The second one is to concatenate the path + user name through the background and pass it to the front desk through ajax, and the front desk gets the url and operates it.
$.ajax({ url: '/login', data: { username: username, password: password }, type: 'GET', success: function (result) { if (result.code === 0) { if(val == num){ layer.msg("Login successful", function () { window.location.href = "index1.html?"+$("#username").val()+""; }) }else{ layer.msg('Verification code error') } } else { layer.msg("Login failed," + result.msg, {icon: 2}) } } }) }
The important thing is the sentence window.location.href = "index.html?"+$("#username").val()+"";, where $("#username").val() gets the user name Value, username is the id of the user name.
First define an id to receive the user name
<p id = "name"></p>
Then get it through JS
<script type="text/javascript"> var url = location.href; //alert(url); var num =url.indexOf("?"); var str = url.substr(num+1); $("#name").html("Welcome" +str); </script>
Don't forget to add at the end
<script type="text/javascript" src="./layui/js/jquery.min.js"></script>
This concludes this article about the sample code for Html to obtain the login username. For more related content about Html to obtain the login username, please search the previous articles of downcodes.com or continue to browse the following related articles. I hope you will read more in the future. Support downcodes.com!