简体   繁体   中英

response.sendRedirect in JSP

This is my current JSP code:

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

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

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

<%
}  
else 
{
  response.sendRedirect("index.jsp");
  return; //this is to redirect immediately so it doesn't
  //run any code below this point before redirecting
}
%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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