Listing A – JSP files
 
index.jsp:
<jsp:forward page="form.jsp" />
 
form.jsp
<html>
<body>
<jsp:include page="nav.jsp" /><br><br>
<h3>Select the appropriate color for the Company Name (Servlet):</h3>
<jsp:include page="servlet_form.jsp" />
<br><br>
<h3>Select the appropriate color for the Company Name (JSP):</h3>
<jsp:include page="jsp_form.jsp">
   <jsp:param name="company" value="My Company, Inc." />
</jsp:include>
</body>
</html>
 
nav.jsp:
<a href="form.jsp">Form Handling</a> | <a href="variables.jsp">JSP Variables</a> | <a href="bean_values.jsp">useBean</a>
 
servlet_form.jsp:
<form action="/myapp/servlet/CompanyName">
   <input type="radio" name="color" value="red">red<br>
   <input type="radio" name="color" value="blue">blue<br>
   <input type="radio" name="color" value="green">green<br>
   <input type="submit" value="Submit">
</form>
 
jsp_form.jsp:
<form action="/myapp/company_name.jsp">
   <input type="text" name="company" value="<%= request.getParameter("company") %>"><br>
   <input type="radio" name="color" value="red">red<br>
   <input type="radio" name="color" value="blue">blue<br>
   <input type="radio" name="color" value="green">green<br>
   <input type="submit" value="Submit">
</form>
 
company_name.jsp:
<html>
<head><title>Company Name JSP</title></head>
<body>
<font color="<%= request.getParameter("color") %>"><h1><%= request.getParameter("company") %></h1></font>
<hr>
</body>
</html>
 
bean_values.jsp:
<%@ page import="myapp.MyBean"%>
<jsp:useBean id="sessionBean" scope="session" class="myapp.MyBean"/>
<jsp:useBean id="applicationBean" scope="application" class="myapp.MyBean"/>
<jsp:include page="nav.jsp" /><br><br>
<%
   if (request.getParameter("session_value") != null) {
      sessionBean.setStringValue(request.getParameter("session_value"));
  }
   if (request.getParameter("application_value") != null) {
      applicationBean.setStringValue(request.getParameter("application_value"));
   }
%>
<h3>Session Scoped Bean:</h3>
stringValue = <%= sessionBean.getStringValue() %>
<br><br>
<h3>Application Scoped Bean:</h3>
stringValue = <%= applicationBean.getStringValue() %>
<br><br><hr><br>
<h3>Change the values:</h3>
<form action="/myapp/bean_values.jsp">
   session stringValue: <input type="text" name="session_value" value="<%= sessionBean.getStringValue() %>"><br>
   application stringValue: <input type="text" name="application_value" value="<%= applicationBean.getStringValue() %>"><br>
   <input type="submit" value="Submit">
</form>
 
variables.jsp:
<%
   pageContext.setAttribute("time", new java.util.Date());
%>
<jsp:include page="nav.jsp" /><br><br>
<h3>request</h3>
Referer: <%= request.getHeader("referer") %><br>
<h3>pageContext</h3>
<%= (java.util.Date) pageContext.getAttribute("time") %>
<h3>session</h3>
ID: <%= session.getId() %><br>
Created: <%= session.getCreationTime() %><br>
<h3>application</h3>
<%= application.getInitParameter("appName") %>