servlet+JSP interacts with SpringBoot+Vue project——servlet accesses Vue page

question

servlet+JSP interacts with SpringBoot+Vue project——servlet accesses Vue page

detailed question

The author developed a project some time ago. The technical framework used is servlet+JSP. The technical framework of the project developed at this stage is SpringBoot+Vue. Now I need to enter the routing address (login page) of servlet+JSP, but the page content is a Vue page (login page), how to achieve

solution

Create a class, inherit HttpServlet, and override doGetmethods
. The relevant code is as follows

@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        // 访问Vue页面所在路由
        response.sendRedirect("http://localhost:8081/user/login");
    }
}

problem causes

In the servlet+JSP technical framework, Vue pages cannot be accessed directly, because servlet+JSP is a technology based on Java Web, and Vue is a front-end framework, and the development methods and technology stacks of the two are different.

solve the cause

To access the Vue page in the servlet+JSP project, we need to use redirection to redirect the request from the servlet route to the route address where the Vue page is located.
In the specific implementation, we created a LoginServlet class and used the @WebServlet annotation to map it to the /login path. In the doGet method, call the response.sendRedirect method to redirect the request to the routing address of the Vue page.
In this way, when we access the /login path in the servlet+JSP project, the servlet will redirect the request to the Vue page, thus realizing the requirement of accessing the Vue page in the servlet+JSP project.
In fact, the rewriting doGetmethod is not the only option, as shown in the code: just get the HttpServletResponse object to operate to achieve redirection. Therefore, for all methods provided by HttpServlet that contain (doGet, doPost, doPut, doDelete) HttpServletResponse object methods, the request can be redirected to the routing address of the Vue page by calling the response.sendRedirect method.

references

The cause of the problem and the reason for the solution refer to chatgpt

It’s not easy
to be original, please indicate the source
if it’s helpful to you, don’t forget to like it and support it
insert image description here

Guess you like

Origin blog.csdn.net/T_Y_F_/article/details/131447089