-2

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

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Alex M
  • 89
  • 1
  • 2
  • 12
  • 1
    Failed how? Why did it fail? What do you think `sendRedirect` does? Why do you think what you currently have should work? – Sotirios Delimanolis Oct 20 '15 at 04:32
  • What I want it can do, is once execute it, if all information correct, it can go to index.jsp directly. Which I have tried in my current code, and it works. But as I said, I want to know how to use response.sendRedirect("index.jsp") instead of jsp:forward page="index.jsp" to have the same function in this case. – Alex M Oct 20 '15 at 07:17

1 Answers1

1

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
}
%>
developerwjk
  • 8,619
  • 2
  • 17
  • 33