在response.sendRedirect()中传递参数-JSP [英] Passing parameters in a response.sendRedirect() - JSP
问题描述
我是Web技术的新手.我正在尝试做一个简单的程序,要求用户输入名称,如果有效,页面将重定向到另一个jsp文件"RedirectIfSuccessful.jsp"
,如果无效,则页面将重定向到"RedirectIfFailed.jsp"
.我正在使用response.sendRedirect()
方法来做到这一点.
重定向正常.但是,我希望访问用户以RedirectIfSuccessful
和RedirectIfFailed
文件的形式输入的名称,以便在输入有效名称时向用户显示:欢迎,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>
希望您能提供帮助,我的问题很明确.谢谢:)
重定向是向浏览器发送一条响应,内容为请转到以下URL:RedirectIfSuccessful.jsp" .
当收到此响应时,浏览器将不带任何参数的新请求发送到RedirectIfSuccessful.jsp
.因此,从RedirectIfSuccessful.jsp
获取参数name
将返回null.
如果您希望在重定向后能够访问该名称,则需要将重定向发送到RedirectIfSuccessful.jsp?name=<the name the user entered>
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.
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.
I tried using request.getParameter("name")
from both files but it's returning a null
value.. What can I do to access it?
This is the code I have: This is the 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>
<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 :)
A redirect consists in sending a response to the browser saying "Please go to the following 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.
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>
这篇关于在response.sendRedirect()中传递参数-JSP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!