January 6, 2011

0 Form Processing Using JSP

 Task 1

You are given the JSP page containing the form. Observe that the form posts to itself recursively. Instantiate the bean FormBean when you recognize that a POST operation has taken place. Allow the
setter methods to be called on the bean using introspection.You can use the useBean tag for instantiating the bean. 


By indicating property="*" within the setProperty tag, you can direct the JSP engine to parse all the incoming values from the HTML form elements that are part of the request object and assign them to
their corresponding bean properties.



Task 2
Deploy the JSP page within Tomcat.
Assuming you have installed Tomcat in say, \jakarta-tomcat, copy the JSP file to \jakarta-tomcat\webapps\examples\jsp\jdc\forms\form.jsp


Task 3
Develop the bean, FormBean.java with properties matching the form
elements.


Develop a bean with the names of the bean properties matching the names of the form input elements. Note that values for certain form elements like checkbox, need to be mirrored within an indexed property. Define the corresponding getter/setter methods for each property within the bean 


Task 4
Compile the bean source FormBean.java. You can compile the bean as javac FormBean.java

Task 5
Deploy the bean within Tomcat.
Copy FormBean.class to \jakarta-tomcat\webapps\examples\WEB-INF\classes\com\jguru\FormBean.class


Task 6
Run the example.
From your browser, access the URL
http://localhost:8080/examples/jsp/jdc/forms/form.jsp.
Fill in data for the form input elements and hit submit. You should
see the data you entered displayed at the bottom of the page.



<html>
<body bgcolor="#c8d8f8">
<form action="/examples/jsp/jdc/forms/form.jsp" method=post>
<center>
<table cellpadding=4 cellspacing=2 border=0>
<th bgcolor="#CCCCFF" colspan=2>
<font size=5>User Registration</font>
</th>
<tr>
<td valign=top>
<b>First Name</b>
<br>
<input type="text" name="firstName" size=15></td>
<td valign=top>
<b>Last Name</b>
<br>
<input type="text" name="lastName" size=15></td>
</tr>
<tr>
<td valign=top colspan=2>
<b>E-Mail</b>
<br>
<input type="text" name="email" size=20>
<br></td>
</tr>
<tr>
<td valign=top colspan=2>
<b>What languages do you program in?</b>
<br>
<input type="checkbox" name="languages" value="Java">Java&nbsp;&nbsp;
<input type="checkbox" name="languages" value="C++">C++&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="languages" value="C">C<br>
<input type="checkbox" name="languages" value="Perl">Perl&nbsp;&nbsp;
<input type="checkbox" name="languages" value="COBOL">COBOL
<input type="checkbox" name="languages" value="VB">VB<br>
</td>
</tr>
<tr>
<td valign=top colspan=2>
<b>How often can we notify you regarding your interests?</b>
<br>
<input type="radio" name="notify" value="Weekly" checked>Weekly&nbsp;&nbsp;
<input type="radio" name="notify" value="Monthly">Monthly&nbsp;&nbsp;
<input type="radio" name="notify" value="Quarterly">Quarterly
<br></td>
</tr>
<tr>
<td align=center colspan=2>
<input type="submit" value="Submit"> <input type="reset" value="Reset">
</td>
</tr>
</table>
</center>
</form>

<%-- Create the bean only when the form is posted --%>
<%
if (request.getMethod().equals("POST")) {
%>
<jsp:useBean id="formHandler" class="com.jguru.FormBean">
<%-- provide a setProperty tag and ensure that the setter methods are invoked via
introspection --%>
</jsp:useBean>
<p>
<hr>
<font color=red>
<b>You submitted:<P>
First Name:</b><br>
<%-- invoke the getter method to display the firstName using the getProperty tag --%>
<br><b>Last Name:</b><br>
<%-- invoke the getter method to display the lastName using the getProperty tag --%>
<br><b>Email:</b><br>
<%-- invoke the getter method to display the email address using the getProperty tag --%>
<b>Languages:</b><br>
<%
String[] lang = formHandler.getLanguages();
if (!lang[0].equals("1")) {
out.println("<ul>");
for (int i=0; i<lang.length; i++)
out.println("<li>"+lang[i]);
out.println("</ul>");
} else out.println("Nothing was selected<br>");
%>
<b>Notification:</b><br>
<%-- invoke the getter method to display the ntotification status using the getProperty
tag --%>
<br>
<%
}
%>
</font>
</body>
</html>


package com.jguru;
public class FormBean {
// declare properties for firstname, lastname, notify and email with the names
//matching the corrosponding form input elements
private String[] languages;
public FormBean() {
firstName="";
lastName="";
email="";
languages = new String[] { "1" };
notify="";
}
//write getter methods for firstname, lastname, notify, email and languages
//write setter methods for firstname, lastname, notify, email and languages
}
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/Forms/FormBean.java
 


<%@ page import="com.jguru.CounterBean" %>
<%-- provide appropriate values for the class and scope attributes --%>
<jsp:useBean id="session_counter" class="" scope="" />
<jsp:useBean id="app_counter" class="" scope="" />
<% session_counter.increaseCount();
synchronized(page) {
app_counter.increaseCount();
}
%>
<h3>
Number of accesses within this session:
<%-- provide appropriate values for the name attribute --%>
<jsp:getProperty name="" property="count" />
</h3>
<p>
<h3>
Total number of accesses:
<%-- provide appropriate values for the name attribute --%>
<% synchronized(page) { %>
<jsp:getProperty name="" property="count" />
<% } %>
</h3>


package com.jguru;
public class CounterBean {
//declare a integer for the counter
public int getCount() {
//return count
}
public void increaseCount() {
//increment count;
}
}


<%-- Indicate the location of the error handler using the page tag --%>
<html>
<body>
<form method=post action="errhandler.jsp">
What's the coolest programming language in the known universe?<p>
Java<input type=radio name=language value="JAVA" checked>
C++<input type=radio name=language value="CPP">
Visual Basic<input type=radio name=language value="VB">
<p>
<input type=submit>
</form>
<%
if (request.getMethod().equals("POST")) {
if (request.getParameter("language").equals("JAVA")) {
out.println("<hr><font color=red>You got that right!</font>");
} else {
//thow a new exception initializing it with some message
}
}
%>
</body>
</html>


<%-- Indicate that this is an error page using the page tag --%>
<html>
<body>
<h1>
Error Page
</h1>
<hr>
<h2>
Received the exception:<br>
<font color=red>
<%= exception.toString() %>
</font>
</h2>
</body>
</html>
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/ErrorHandling/errorpage.txt





<%@ page import="com.jguru.CounterBean" %>
<jsp:useBean id="session_counter" class="com.jguru.CounterBean" scope="session" />
<jsp:useBean id="app_counter" class="com.jguru.CounterBean" scope="application" />
<% session_counter.increaseCount();
synchronized(page) {
app_counter.increaseCount();
}
%>
<h3>
Number of accesses within this session:
<jsp:getProperty name="session_counter" property="count" />
</h3>
<p>
<h3>
Total number of accesses:
<% synchronized(page) { %>
<jsp:getProperty name="app_counter" property="count" />
<% } %>
</h3>
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/Counter/solution/Counter.txt
 


package com.jguru;
public class CounterBean {
int count;
public int getCount() {
return count;
}
public void increaseCount() {
count++;
}
}
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/Counter/solution/CounterBean.java
 


<%@ page errorPage="errorpage.jsp" %>
<html>
<body>
<form method=post action="errhandler.jsp">
What's the coolest programming language in the known universe?<p>
Java<input type=radio name=language value="JAVA" checked>
C++<input type=radio name=language value="CPP">
Visual Basic<input type=radio name=language value="VB">
<p>
<input type=submit>
</form>
<%
if (request.getMethod().equals("POST")) {
if (request.getParameter("language").equals("JAVA")) {
out.println("<hr><font color=red>You got that right!</font>");
} else {
throw new Exception("You chose the wrong language!");
}
}
%>
</body>
</html>





<%@ page isErrorPage="true" %>
<html>
<body>
<h1>
Error Page
</h1>
<hr>
<h2>
Received the exception:<br>
<font color=red>
<%= exception.toString() %>
</font>
</h2>
</body>
</html>











 

0 comments:

Post a Comment

Blogger Themes

 
Powered by Blogger