在重定向jsp页面时传递变量

时间:2016-08-14 15:25:51

标签: jsp

我有三个jsp页面:annotation.jsp,check.jsp和end.jsp。我根据条件从check.jsp重定向到annotation.jsp或end.jsp。在重定向时,我也传递了一个变量。

Annotation.jsp:

<%@page language="java"%>
<%@ page import="java.io.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Annotation Page</title>
</head>
<body>
    <%
          String user = request.getParameter("user");
    %>
    <form method="get" action="check.jsp">
        <input type="hidden" name="user" value="<%=user%>">
        <input type="radio" name="similarity" value="Similar"> Similar<br>
        <input type="radio" name="similarity" value="Dissimilar"> Dissimilar<br>
        <input type="submit" name="submit" value="Submit">
    </form>
</body>
</html>

Check.jsp:

<%@page import="java.io.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <form method="get">
        <%
                    String user = request.getParameter("user");
                    if (condition) {
                        String redirectURL1 = "annotation.jsp";
                        response.sendRedirect(redirectURL1);
                    } else {
                        String redirectURL2 = "end.jsp";
                        response.sendRedirect(redirectURL2);
                    }
        %>
   <input type="hidden" name="user" value="<%=user%>">
    </form>
</body>
</html>

我想知道这种类型的重定向是否传递了这个变量?因为我来自check.jsp时,我在annotation.jsp中获得空值。

1 个答案:

答案 0 :(得分:0)

替换行:

String redirectURL1 = "annotation.jsp;
response.sendRedirect(redirectURL1);

使用:

String redirectURL1 = "annotation.jsp?User="+request.getParameter("User");
response.sendRedirect(redirectURL1);