繁体   English   中英

JSP 中的 response.sendRedirect

[英]response.sendRedirect in JSP

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM