簡體   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