Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of getting session value in html

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "what are the methods to obtain sessionvalue in html", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what are the ways to obtain sessionvalue in html" can help you solve your doubts? let's follow the editor's way of thinking to learn new knowledge.

1. First of all, there is a server in the key-value of session. There is no way for the browser HTML page to get the value in session directly. Only in html, you can get things like jesessionid through js.

1.1.If the amount of data is small, you can consider putting it in cookie and transmitting it to the client. You can get it with js in html.

1.2.If the amount of data is large, you can consider making a separate jsp or servlet, and return the serialized session value, such as json, according to the key of the session. Use js in html to get it through ajax. This approach is a little more complicated, one more remote access, but flexible and convenient.

Such as:

two。 Or it can only be obtained through the background, session is stored on the server side, if you use cookie, you can get it through js.

Problem description: the UserInfo object is saved in session. After logging in successfully, "Welcome xxx" is displayed in html.

Solution: get UserInfo data through ajax,json, and then display

1.js

$(function () {$.ajax ({type: "get", url: "loginloggetLoginName.action", dataType: "text", success: function (result) {document.getElementsByTagName ('b') [0] [xss_clean] = result }, error: function () {alert ("request failed");});})

two。 Page:

Administrative page administrator: Hello, thank you for logging in!

3. Entities: UserInfo

Public class UserInfo {private int UserInfoId; private String userInfoName; private String UserInfoPsw; / / omit get,set

In 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. Use response.sendRedirect ("a.html?param=hello"); use the following JS method

Such as:

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 in session in the html page.

After reading this, the article "what are the ways to get session values in html" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report