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

Java web project Session can not get the problem and the solution

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

Share

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

In this issue, the editor will bring you about the problems and solutions of java web project Session access. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Java web project Session cannot be obtained

Session is set in the servlet class, but when the servlet jumps to the page, the page does not get it!

About the life cycle of session

The Session is saved on the server side. Session is created automatically the first time a user accesses the server. It should be noted that Session is created only when accessing programs such as JSP and Servlet, and Session is not created when accessing static resources such as HTML, IMAGE, etc. If you have not already generated Session, you can also use request.getSession (true) to force the generation of Session.

The reason why we can't get Seesion on the page may be that the browser disables Cookie.

Use url address rewriting to solve the problem!

Response.encodeRedirectURL (url) is used to rewrite the url address after the sendRedirect method.

Response.encodeURL (url) is used to rewrite the url address of the form action and hyperlink

String url=response.encodeRedirectURL ("url"); response.sendRedirect (url)

In this way, you can use the

To get our stored SeesionSecretaries!

The problem that session can not be obtained from the separation of front and rear ends

For projects separated from the front and back ends, the front end is vue, the back end springboot, or the maven project. When generating the CAPTCHA, a copy is stored in session and stored in redis. The front end transfers the backend registration to not get the session, but when the user fetches the information from the session, it is found that the new sessionid is sent by the ajax every time the cross-domain request is made, resulting in the inability to obtain the information.

Solution

The front end adds the following code to the main file of vue globally:

Import axios from 'axios';axios.defaults.withCredentials=true

Add a filter in the background

Web.xml file

ContextFilter com.zl.filter.ContextFilter contextFilter / * public class ContextFilter implements Filter {@ Override public void init (FilterConfig filterConfig) throws ServletException {} @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {HttpServletResponse response = (HttpServletResponse) servletResponse; HttpServletRequest request = (HttpServletRequest) servletRequest; String origin = request.getHeader ("Origin"); response.setHeader ("Access-Control-Allow-Origin", origin) Response.setHeader ("Access-Control-Allow-Methods", "*"); String headers = request.getHeader ("Access-Control-Request-Headers"); / / supports all custom headers if (! org.springframework.util.StringUtils.isEmpty (headers)) {response.setHeader ("Access-Control-Allow-Headers", headers) } response.setHeader ("Access-Control-Allow-Credentials", "true"); response.setHeader ("Access-Control-Max-Age", "3600"); filterChain.doFilter (request, response) } @ Override public void destroy () {}} above is the java web project Session that Xiaobian shared with you can not get the problem and solution. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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