java JSP 中的 response.sendRedirect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33228029/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:23:23  来源:igfitidea点击:

response.sendRedirect in JSP

javajsp

提问by Alex M

This is my current JSP code:

这是我当前的 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>

Here I use jsp:forward page="index.jsp"to redirect to index.jsp page. Then, if I want to use response.sendRedirect("index.jsp")? How can I proceed?

这里我使用jsp:forward page="index.jsp"重定向到 index.jsp 页面。那么,如果我想使用response.sendRedirect("index.jsp")? 我该如何继续?

I tried this:

我试过这个:

<% 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>

But it failed. Please help! Thank you!!

但它失败了。请帮忙!谢谢!!

回答by developerwjk

response.sendRedirect()is Java code not a tag, so you should notclose the scriptlet tags before typing it, and it is not to be preceded by <and closed with >...its just Java code:

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
}
%>