First Page

<%@ 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>
<body>
<h1> Register
</h1> 
<form method="post" action="welcome.jsp">
<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 rowspan="2">Gender</td><td><input type="radio" name="gender">Male</td></tr><tr><td><input type="radio" name="gender">Female</td></tr>    
<tr><td>Favourite colour</td><td><select name="favcol">
  <option value="Red">Red</option>
  <option value="Green">Green</option>
  <option value="Blue">Blue</option>
  <option value="Yellow">Yellow</option>
  <option value="orange">orange</option>
  <option value="pink">pink</option>
</select></td></tr>    
<tr><td>Agree to TOS</td><td><input type="checkbox" name="tos"></td></tr>    
<tr><td></td><td><input type="submit" value="Register"></td></tr>
</table>
</form>
</body>
</html>

**Second Page **

<%@ 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>
<body bgcolor="">    
<%
    String name = request.getParameter("name");
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    String gender = request.getParameter("gender");
    String tos = request.getParameter("tos");
    String favcol = request.getParameter("favcol");
%>    
<%
    if (tos != null) {
%>
<p>Welcome, <%=name %>!</p>
<p>Your Email is <%=email %>.</p>
<p>Your password is <%=password %>.</p>
<p>Your gender is <%=gender %>.</p>
<p>Your favourite colour is <%=favcol %>.</p>
<% }
else {    
%>
<p>Sorry, you must agree to the Terms of Service.</p>
<p>Click <a href="register.jsp">here</a> to go back</p>
<% } %>
</body>
</html>

Question
I'm looking for the solution for the background colour of the 2nd page that depends on what favourite colour the user chose at the 1st page. I'm new to these things, please help me. Thank you!

Dani AI

Generated

posted a correct form; ’s quick expression-tag idea will work, but it uses deprecated HTML and exposes the page to malformed input. A safer, more modern pattern is to map the submitted value to a small whitelist of known colors (or hex codes) on the server, then apply that value with CSS. This avoids inline scriptlets, prevents CSS/HTML injection, and keeps behavior predictable across browsers.

Example (JSP + JSTL: map, set a session preference, then use CSS):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:set var="col" value="${fn:toLowerCase(param.favcol)}" />

<c:choose>
  <c:when test="${col == 'red'}"><c:set var="bg" value="#ffdddd" /></c:when>
  <c:when test="${col == 'green'}"><c:set var="bg" value="#ddffdd" /></c:when>
  <c:when test="${col == 'blue'}"><c:set var="bg" value="#ddddff" /></c:when>
  <c:when test="${col == 'yellow'}"><c:set var="bg" value="#ffffdd" /></c:when>
  <c:when test="${col == 'orange'}"><c:set var="bg" value="#ffe6cc" /></c:when>
  <c:when test="${col == 'pink'}"><c:set var="bg" value="#ffd6e7" /></c:when>
  <c:otherwise><c:set var="bg" value="#ffffff" /></c:otherwise>
</c:choose>

<c:set var="userBg" value="${bg}" scope="session" />

<body style="background-color:${bg}">
  <!-- page content -->
</body>

Troubleshooting and cautions: ensure the select’s name matches the param (favcol), keep option values predictable (prefer lowercase or fixed tokens that map to hex), and include JSTL on the server (JARs). If JSTL is not available, perform the same whitelist mapping in a servlet before forwarding. Never place raw user input directly into inline CSS or the deprecated bgcolor attribute — always validate or map it first.

Recommended Answers

All 2 Replies

hi,
i dont this is better idea or not but it gives a solution for you by using expression tag

change you code as follows

// get the values before body tag starts and place the selected color value for bgcolor attribute as shown bellow

  <body bgcolor="<%= favCol%>">  

try this once.

reply me incase if you have any issue with my response

NOTE: suggest me if i am wrong

Hi,
please mark this thread as solved or atleast close if you get the answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.