点击登录后跳转回原来界面

  1. 具体思想,在点击登录是时候,将当前界面的地址callback通过get方式传到login界面,login界面再通过方式传递过去

some.jsp

<%@ page import="java.util.List" %>
<%@ page import="java.util.Collection" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/6/4 0004
  Time: 上午 11:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <a id="logina" href="login.jsp?callback=">login</a>
  </body>
<script type="text/javascript">
    var a = document.getElementById("logina");
        console.log(a);
        a.href+=window.location.href;
</script>
</html>

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/6/4 0004
  Time: 下午 1:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form method="post" action="login">
    <input type="text" name="name"/>
    <input type="password" name="password"/>
    <input type="hidden" name="callback" value="<%=request.getParameter("callback")%>"/>
    <input type="submit" value="login"/>
</form>
</body>
</html>

controller

package controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = {"/login"})
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        /**
         * 省略对用户名和密码的操作,和数据库匹配
         *
         * */

        String callback = request.getParameter("callback");
        response.sendRedirect(callback);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

猜你喜欢

转载自blog.csdn.net/qq_32296307/article/details/80569044
今日推荐