response.sendRedirect是在JSP
这是我目前的JSP代码:response.sendRedirect是在JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<jsp:useBean id="user" class= "uts.wsd.User" scope="session" ></jsp:useBean>
<%
String name = request.getParameter("name");
String email = request.getParameter("email");
String password = request.getParameter("password");
String gender = request.getParameter("gender");
String color = request.getParameter("favcol");
user.setName(name);
user.setEmail(email);
user.setPassword(password);
user.setGender(gender);
user.setFavouriteColour(color);
%>
<body style="background: <%= color %>;">
<% if (request.getParameter("tos") == null) {%>
<p>
Sorry, you must agree to the Terms of Service.</p>
<p>Click <a href="register.jsp" > here </a> to go back.
</p>
<%} else { %>
<jsp:forward page="index.jsp" />
<% } %>
</html>
这里我用jsp:forward page="index.jsp"
重定向到index.jsp页面。那么,如果我想用response.sendRedirect("index.jsp")
?我该如何继续?
我尝试这样做:
<% if (request.getParameter("tos") == null) {%> <p>
Sorry, you must agree to the Terms of Service.</p>
<p>Click <a href="register.jsp" > here </a> to go back.
</p>
<%} else { %>
<response.sendRedirect("index.jsp")>
<% } %>
</html>
但失败了。请帮忙!谢谢!!
回答:
response.sendRedirect()
是Java代码不是一个标签,所以你应该不键入它之前关闭scriptlet标记,它不是由<
开头且>
关闭...它只是Java代码:
<% }
else
{
response.sendRedirect("index.jsp");
return; //this is to redirect immediately so it doesn't
//run any code below this point before redirecting
}
%>
以上是 response.sendRedirect是在JSP 的全部内容, 来源链接: utcz.com/qa/259955.html