Search code examples
jspif-statementscriptlet

if..else..in JSP. What's wrong with my code?


I'm writing a simple application for registration. There are one register.jsp (allow users to input their information), one JAVABean,and one welcome.jsp(print out user information).

In the welcome.jsp. I want to add if..else..to my code. If users check the agree to TOS option in register.jsp,then their information print out. Else, there is a warning that they must agree to TOS.

I have problem when I write the if..else...simply saying, I try to use:

<% if (<%= User.getTos()%> != null) {
 %> Print Information <% 
} else {
 %> A Warning <% 
} %>

But this if...else..has syntax error. I don't know how to fix it. How should I write the if statement correctly??**

This is register.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>Register</title>
</head>
<body>

<h1>Register</h1>

<form action="welcome.jsp" method="POST">

<table>
<tr><td>Email</td><td><input type="text" name="email"></td></tr>
<tr><td>Full name</td><td><input type="text" name="name"></td></tr>
<tr><td>Password</td><td><input type="password" name="password"></td></tr>
<tr><td>Gender</td><td><input type="radio" name="gender"     value="male">Male<br><input type="radio" name="gender"    value="female">Female</td></tr>
<tr><td>Favourite colour</td><td><select name="favcol">     <option>red<option>green</select></td></tr>
<tr><td>Agree to TOS</td><td><input type="checkbox" name="tos" value="tos">   </td></tr>
<tr><td></td><td><input type="submit" value="Register"></td></tr>
</table>

</form>

</body>
</html>

This is Java Bean (I generate it by Eclipse):

package uts;

public class User {
  private String name;
  private String email;
  private String password;
  private String gender;
  private String favcol;
  private String tos;
public User() {
    super();
    // TODO Auto-generated constructor stub
}
public User(String name, String email, String password, String gender,    String favcol, String tos) {
    super();
    this.name = name;
    this.email = email;
    this.password = password;
    this.gender = gender;
    this.favcol = favcol;
    this.tos = tos;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getFavcol() {
    return favcol;
}
public void setFavcol(String favcol) {
    this.favcol = favcol;
}
public String getTos() {
    return tos;
}
public void setTos(String tos) {
    this.tos = tos;
}
}

This is welcome.jsp (the one contains if..else...)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="User" scope="session" class="uts.User"/> 
<jsp:setProperty name="User" property="*"/>

<!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>
<body bgcolor="<%= User.getFavcol() %>">

**<% if (<%= User.getTos()%> != null) { %>**
<p>Welcome, <%= User.getName() %>!</p>

<p>Your Email is <%= User.getEmail() %>.</p>

<p>Your password is <%= User.getPassword() %>.</p>

<p>Your gender is <%= User.getGender() %>.</p>

<p>Your favourite colour is <%= User.getFavcol() %>.</p>

<% } else { %>
<p>Sorry, you must agree to the Terms of Services.</p>
<p>Click <a href="register.jsp">here</a> to go back.</p>
<%
}
%>
</body>
</html>

Solution

  • Don't nest scriptlets.

    <% if (<%= User.getTos()%> != null) { %>
    

    should be something like

    <% if (User.getTos() != null) { %>
    

    Also, please don't use scriptlets.