【问题标题】:Passing parameters in a response.sendRedirect() - JSP在 response.sendRedirect() 中传递参数 - JSP
【发布时间】:2012-12-24 10:41:52
【问题描述】:

我是网络技术的新手。我正在尝试做一个简单的程序,要求用户输入名称,如果有效页面重定向到另一个 jsp 文件"RedirectIfSuccessful.jsp",如果无效页面重定向到"RedirectIfFailed.jsp"。我正在使用response.sendRedirect() 方法来执行此操作。

重定向工作正常。但是,我希望从RedirectIfSuccessfulRedirectIfFailed 文件中访问用户在表单中输入的名称,以便在输入有效名称时向用户显示:Welcome, nameEntered 并且当失败时,消息将是 nameEntered无效。请返回并重试。

我尝试从两个文件中使用request.getParameter("name"),但它返回了null 值。我该怎么做才能访问它?

这是我的代码:这是RedirectingPage.jsp

 <%@ page 
    language="java" 
    import="java.util.regex.*"
    contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">


<%  
    String name = request.getParameter("name");

    final String NAME_PATTERN = "^[a-zA-Z]{3,15}$";
    Pattern pattern = Pattern.compile(NAME_PATTERN);

    Matcher matcher = pattern.matcher(name);

    if (matcher.matches() == true){
        response.sendRedirect("RedirectIfSuccessful.jsp");

    } else {
        response.sendRedirect("RedirectIfFailed.jsp");
    }

%>

这是我的表单所在的 HTML 文件:FormSubmit.html

<html>
    <head>
        <title> Welcome </title>
    </head>

    <body BGCOLOR="#FDF5E6">
        <p> <i> This program redirects to a page if the name entered is valid and to another one if name
            entered is invalid... This uses response.sendRedirect() </i> </p>

        <form action="RedirectingPage.jsp" method="post">
          <font size=6 face="Georgia"> <strong> Enter your name: </strong> </font> <input type="text" name="name"> <br> <br>
          <input type="submit" name="btn" value="Submit" >
        </form>
    </body>
</html>

这是成功页面:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Successful Submit </title>
</head>

<body>

<font face="Georgia" size="6"> Hello, <% request.getParameter("name"); %> </font>

</body>
</html>

我希望你能提供帮助,我的问题很清楚。谢谢:)

【问题讨论】:

    标签: html jsp redirect httprequest


    【解决方案1】:

    重定向包括向浏览器发送响应“请转到以下 URL:RedirectIfSuccessful.jsp”

    当它收到这个响应时,浏览器会向RedirectIfSuccessful.jsp发送一个新的请求,不带任何参数。所以从RedirectIfSuccessful.jsp获取参数name会返回null。

    如果您想在重定向后访问该名称,您需要发送重定向到RedirectIfSuccessful.jsp?name=&lt;the name the user entered&gt;

    【讨论】:

    • 哦好的我明白了,所以当浏览器创建新请求时,它将没有参数。所以如果我使用 forward() 方法而不是 sendRedirect() 它会保存参数,对吧?因为浏览器不会创建新请求?我不太明白最后一行代码的含义:/感谢您的回答JB :)
    • 是的,你没看错。事实上,前锋不会有这种行为。请注意,在成功 POST 后重定向(而不是转发)是一种很好的做法。这是the post-redirect-get pattern。最后一句的意思是:可以重定向到包含请求参数的URL。例如,如果您向RedirectIfSuccessful.jsp?name=John 发送重定向,浏览器将向RedirectIfSuccessful.jsp?name=John 发送GET 请求,JSP 将有权访问参数name,其值为John
    • 是的,我同意你在这种情况下使用 sendRedirect。我正在尝试在 RedirectPage 中执行此操作,但它仍然显示 null:response.sendRedirect("RedirectIfSuccessful.jsp?name=enteredName");其中字符串名称 = request.getParameter("name");和 String enterName = name;
    • 在 url 中显示 name=enteredName 而不是 name=John :/ 我也在对 URL 进行编码并正常获取参数。无法弄清楚问题是什么:/
    • 当然应该是response.sendRedirect("RedirectIfSuccessful.jsp?name=" + enteredName);(并且enteredName也应该是URL编码的,以防它包含特殊字符)
    【解决方案2】:

    要在另一个页面中获取名称,请使用 session。

    例如登录页面session.setAttribute("name",name)

    要检索名称,请使用session.getAttribute("name")。您可以将它分配给这样的变量:

    <% String name=(string)session.getAttribute("name");%>
    

    成功!

    【讨论】:

      猜你喜欢
      • 2013-06-04
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      相关资源
      最近更新 更多