【发布时间】:2012-12-24 10:41:52
【问题描述】:
我是网络技术的新手。我正在尝试做一个简单的程序,要求用户输入名称,如果有效页面重定向到另一个 jsp 文件"RedirectIfSuccessful.jsp",如果无效页面重定向到"RedirectIfFailed.jsp"。我正在使用response.sendRedirect() 方法来执行此操作。
重定向工作正常。但是,我希望从RedirectIfSuccessful 和RedirectIfFailed 文件中访问用户在表单中输入的名称,以便在输入有效名称时向用户显示: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