Html 在 response.sendRedirect() 中传递参数 - JSP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14020234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing parameters in a response.sendRedirect() - JSP
提问by Bernice
I am new to Web Technologies. I am trying to do a simple program which ask the user to input a name, if valid the page redirects to another jsp file "RedirectIfSuccessful.jsp"
, if invalid the page redirects to "RedirectIfFailed.jsp"
. I am using the response.sendRedirect()
method to do this.
我是 Web 技术的新手。我正在尝试做一个简单的程序,它要求用户输入一个名称,如果有效页面重定向到另一个 jsp 文件"RedirectIfSuccessful.jsp"
,如果无效页面重定向到.jsp 文件"RedirectIfFailed.jsp"
。我正在使用该response.sendRedirect()
方法来执行此操作。
The redirect is working fine. However, I wish to access the name the user inputs in the form from RedirectIfSuccessful
and RedirectIfFailed
files so that when a valid name is entered the user is presented with a : Welcome, nameEntered and when failed the message would be nameEntered was not valid. Please go back and try again.
重定向工作正常。但是,我希望访问用户在表单RedirectIfSuccessful
和RedirectIfFailed
文件中输入的名称,以便在输入有效名称时向用户显示:Welcome, nameEntered,失败时消息将是 nameEntered is not valid。请返回重试。
I tried using request.getParameter("name")
from both files but it's returning a null
value.. What can I do to access it?
我尝试request.getParameter("name")
从这两个文件中使用,但它返回一个null
值.. 我该怎么做才能访问它?
This is the code I have: This is the RedirectingPage.jsp
这是我的代码:这是 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");
}
%>
This is the HTML file where I have the form: FormSubmit.html
这是我有表单的 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>
And this is the Successful page:
这是成功页面:
<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>
I hope you can help and I was clear in my question. Thanks :)
我希望你能帮忙,我的问题很清楚。谢谢 :)
回答by JB Nizet
A redirect consists in sending a response to the browser saying "Please go to the following URL : RedirectIfSuccessful.jsp".
重定向包括向浏览器发送响应,说“请转到以下 URL:RedirectIfSuccessful.jsp”。
When it receives this response, the browser sends a new request to RedirectIfSuccessful.jsp
, without any parameter. So getting the parameter name
from RedirectIfSuccessful.jsp
will return null.
当它收到这个响应时,浏览器向 发送一个新请求RedirectIfSuccessful.jsp
,不带任何参数。所以获取参数name
fromRedirectIfSuccessful.jsp
将返回 null。
If you want to have access to the name after the redirection, you need to send a redirect to RedirectIfSuccessful.jsp?name=<the name the user entered>
如果您想在重定向后访问名称,则需要发送重定向到 RedirectIfSuccessful.jsp?name=<the name the user entered>
回答by HILLARY RONOH-MMUST
To get the name in another page, use session.
要在另一个页面中获取名称,请使用 session。
e.g. session.setAttribute("name",name)
at the login page;
例如session.setAttribute("name",name)
在登录页面;
To retrieve the name, use session.getAttribute("name")
. You can assign it to a variable like this:
要检索名称,请使用session.getAttribute("name")
. 您可以将其分配给这样的变量:
<% String name=(string)session.getAttribute("name");%>
success!
成功!