tag:blogger.com,1999:blog-33909979725269341922024-03-13T05:56:14.702-07:00Web Developmentrajhttp://www.blogger.com/profile/07775159902177850551noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-3390997972526934192.post-10029535361100003482011-01-06T06:54:00.000-08:002011-01-06T06:54:11.791-08:00Understanding JSP Object Scope<u style="color: magenta;"><b>Task 1</b></u><br />
<b><span style="color: lime;">Develop a simple counter bean, CounterBean.java.</span><br style="color: lime;" /><span style="color: lime;">Develop a bean which can maintain a count within say, an integer</span><br style="color: lime;" /><span style="color: lime;">property. Provide at least a getter method for this property.</span></b><br />
<br />
<u style="color: magenta;"><b>Task 2</b></u><br />
<b><span style="color: lime;">Compile the counter bean.</span><br style="color: lime;" /><span style="color: lime;">You can compile the bean as javac CounterBean.java</span></b><br />
<br />
<u style="color: magenta;"><b>Task 3</b></u><br />
<b><span style="color: lime;">Deploy the bean within Tomcat.</span><br style="color: lime;" /><span style="color: lime;">Copy CounterBean.class to</span><br style="color: lime;" /><span style="color: lime;">jakarta-tomcat\webapps\examples\WEB-INF\jsp\classes\com\jguru\CounterBean.class.</span><br style="color: lime;" /><span style="color: lime;">You will need to create the directories below classes for the package the</span><br style="color: lime;" /><span style="color: lime;">bean is in.</span></b><br />
<br />
<u style="color: magenta;"><b>Task 4</b></u><br />
<b style="color: lime;">Develop a JSP page, Counter.jsp, which creates two instances of the<br />
counter bean, one with session scope, and the other with application<br />
scope.</b><br />
<b style="color: lime;"><br />
You can use the jsp:useBean tag for instantiating the beans. Make sure<br />
you provide the appropriate scope for the scope attribute.</b><br />
<br />
<br />
<u style="color: magenta;"><b>Task 5</b></u><br />
<b><span style="color: lime;">Deploy the JSP file for the example within Tomcat.</span><br style="color: lime;" /><span style="color: lime;">Assuming you have installed Tomcat in say, \jakarta-tomcat, copy the JSP</span><br style="color: lime;" /><span style="color: lime;">file to \jakarta-tomcat\webapps\examples\jsp\jdc\counter\Counter.jsp</span></b><br />
<br />
<u style="color: magenta;"><b>Task 6</b></u><br />
<b><span style="color: lime;">Run the example.</span><br style="color: lime;" /><span style="color: lime;">From your browser (say, Netscape Navigator), access the URL</span><br style="color: lime;" /><span style="color: lime;">http://localhost:8080/examples/jsp/jdc/counter/Counter.jsp</span><br style="color: lime;" /><span style="color: lime;">Now, start up a different browser (say, MSIE) access the URL</span><br style="color: lime;" /><span style="color: lime;">http://localhost:8080/examples/jsp/jdc/counter/Counter.jsp</span></b><br />
<b><br style="color: lime;" /><span style="color: lime;">(Note: If you use only MSIE, you can simply double click on the browser</span><br style="color: lime;" /><span style="color: lime;">icon again to run a second instance of MSIE as a separate process. This</span><br style="color: lime;" /><span style="color: lime;">is important to ensure that the browser creates a new session and does</span><br style="color: lime;" /><span style="color: lime;">not reuse the one created by an earlier instance.)</span><br style="color: lime;" /><span style="color: lime;">Click a few times within both browser to increment the counters for the</span><br style="color: lime;" /><span style="color: lime;">beans with session and application scope. Observe the difference</span><br style="color: lime;" /><span style="color: lime;">between the two counts</span></b>rajhttp://www.blogger.com/profile/07775159902177850551noreply@blogger.com0tag:blogger.com,1999:blog-3390997972526934192.post-29559581639431939972011-01-06T06:47:00.000-08:002011-01-06T06:47:13.376-08:00Form Processing Using JSP<u style="color: magenta;"><b> Task 1</b></u><br />
<br />
<b style="color: cyan;">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<br />
setter methods to be called on the bean using introspection.You can use the useBean tag for instantiating the bean. </b><br />
<div style="color: cyan;"><br />
</div><b style="color: cyan;">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<br />
their corresponding bean properties.</b><br />
<br />
<div style="color: magenta;"><u><b><br />
</b></u></div><u style="color: magenta;"><b>Task 2</b></u><br />
<b><span style="color: cyan;">Deploy the JSP page within Tomcat.</span><br style="color: cyan;" /><span style="color: cyan;">Assuming you have installed Tomcat in say, \jakarta-tomcat, copy the JSP file to \jakarta-tomcat\webapps\examples\jsp\jdc\forms\form.jsp</span></b><br />
<br />
<u style="color: magenta;"><b>Task 3</b></u><br />
<b><span style="color: cyan;">Develop the bean, FormBean.java with properties matching the form</span><br style="color: cyan;" /><span style="color: cyan;">elements.</span></b><br />
<b><br />
<span style="color: cyan;">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 </span></b><br />
<br />
<u style="color: magenta;"><b>Task 4</b></u><br />
<b><span style="color: cyan;">Compile the bean source FormBean.java. You can compile the bean as javac FormBean.java</span></b><br />
<br />
<u style="color: magenta;"><b>Task 5</b></u><br />
<b><span style="color: cyan;">Deploy the bean within Tomcat.</span><br style="color: cyan;" /><span style="color: cyan;">Copy FormBean.class to \jakarta-tomcat\webapps\examples\WEB-INF\classes\com\jguru\FormBean.class</span></b><br />
<br />
<u style="color: magenta;"><b>Task 6</b></u><br />
<b style="color: cyan;">Run the example.<br />
From your browser, access the URL<br />
http://localhost:8080/examples/jsp/jdc/forms/form.jsp.<br />
Fill in data for the form input elements and hit submit. You should<br />
see the data you entered displayed at the bottom of the page.</b><br />
<br />
<br />
<div style="color: lime;"><b><html><br />
<body bgcolor="#c8d8f8"><br />
<form action="/examples/jsp/jdc/forms/form.jsp" method=post><br />
<center><br />
<table cellpadding=4 cellspacing=2 border=0><br />
<th bgcolor="#CCCCFF" colspan=2><br />
<font size=5>User Registration</font><br />
</th><br />
<tr><br />
<td valign=top><br />
<b>First Name</b><br />
<br><br />
<input type="text" name="firstName" size=15></td><br />
<td valign=top><br />
<b>Last Name</b><br />
<br><br />
<input type="text" name="lastName" size=15></td><br />
</tr><br />
<tr><br />
<td valign=top colspan=2><br />
<b>E-Mail</b><br />
<br><br />
<input type="text" name="email" size=20><br />
<br></td><br />
</tr><br />
<tr><br />
<td valign=top colspan=2><br />
<b>What languages do you program in?</b><br />
<br><br />
<input type="checkbox" name="languages" value="Java">Java&nbsp;&nbsp;<br />
<input type="checkbox" name="languages" value="C++">C++&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
<input type="checkbox" name="languages" value="C">C<br><br />
<input type="checkbox" name="languages" value="Perl">Perl&nbsp;&nbsp;<br />
<input type="checkbox" name="languages" value="COBOL">COBOL<br />
<input type="checkbox" name="languages" value="VB">VB<br><br />
</td><br />
</tr><br />
<tr><br />
<td valign=top colspan=2><br />
<b>How often can we notify you regarding your interests?</b><br />
<br><br />
<input type="radio" name="notify" value="Weekly" checked>Weekly&nbsp;&nbsp;<br />
<input type="radio" name="notify" value="Monthly">Monthly&nbsp;&nbsp;<br />
<input type="radio" name="notify" value="Quarterly">Quarterly<br />
<br></td><br />
</tr><br />
<tr><br />
<td align=center colspan=2><br />
<input type="submit" value="Submit"> <input type="reset" value="Reset"><br />
</td><br />
</tr><br />
</table><br />
</center><br />
</form></b></div><div style="color: lime;"><b><br />
</b></div><b style="color: lime;"><%-- Create the bean only when the form is posted --%><br />
<%<br />
if (request.getMethod().equals("POST")) {<br />
%><br />
<jsp:useBean id="formHandler" class="com.jguru.FormBean"><br />
<%-- provide a setProperty tag and ensure that the setter methods are invoked via<br />
introspection --%><br />
</jsp:useBean><br />
<p><br />
<hr><br />
<font color=red><br />
<b>You submitted:<P><br />
First Name:</b><br><br />
<%-- invoke the getter method to display the firstName using the getProperty tag --%><br />
<br><b>Last Name:</b><br><br />
<%-- invoke the getter method to display the lastName using the getProperty tag --%><br />
<br><b>Email:</b><br><br />
<%-- invoke the getter method to display the email address using the getProperty tag --%><br />
<b>Languages:</b><br><br />
<%<br />
String[] lang = formHandler.getLanguages();<br />
if (!lang[0].equals("1")) {<br />
out.println("<ul>");<br />
for (int i=0; i<lang.length; i++)<br />
out.println("<li>"+lang[i]);<br />
out.println("</ul>");<br />
} else out.println("Nothing was selected<br>");<br />
%><br />
<b>Notification:</b><br><br />
<%-- invoke the getter method to display the ntotification status using the getProperty<br />
tag --%><br />
<br><br />
<%<br />
}<br />
%><br />
</font><br />
</body><br />
</html></b><br />
<br />
<b style="color: lime;">package com.jguru;<br />
public class FormBean {<br />
// declare properties for firstname, lastname, notify and email with the names<br />
//matching the corrosponding form input elements<br />
private String[] languages;<br />
public FormBean() {<br />
firstName="";<br />
lastName="";<br />
email="";<br />
languages = new String[] { "1" };<br />
notify="";<br />
}<br />
//write getter methods for firstname, lastname, notify, email and languages<br />
//write setter methods for firstname, lastname, notify, email and languages<br />
}<br />
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/Forms/FormBean.java<br />
</b><br />
<br />
<b style="color: lime;"><%@ page import="com.jguru.CounterBean" %><br />
<%-- provide appropriate values for the class and scope attributes --%><br />
<jsp:useBean id="session_counter" class="" scope="" /><br />
<jsp:useBean id="app_counter" class="" scope="" /><br />
<% session_counter.increaseCount();<br />
synchronized(page) {<br />
app_counter.increaseCount();<br />
}<br />
%><br />
<h3><br />
Number of accesses within this session:<br />
<%-- provide appropriate values for the name attribute --%><br />
<jsp:getProperty name="" property="count" /><br />
</h3><br />
<p><br />
<h3><br />
Total number of accesses:<br />
<%-- provide appropriate values for the name attribute --%><br />
<% synchronized(page) { %><br />
<jsp:getProperty name="" property="count" /><br />
<% } %><br />
</h3></b><br />
<br />
<b style="color: lime;">package com.jguru;<br />
public class CounterBean {<br />
//declare a integer for the counter<br />
public int getCount() {<br />
//return count<br />
}<br />
public void increaseCount() {<br />
//increment count;<br />
}<br />
}</b><br />
<br />
<b style="color: lime;"><%-- Indicate the location of the error handler using the page tag --%><br />
<html><br />
<body><br />
<form method=post action="errhandler.jsp"><br />
What's the coolest programming language in the known universe?<p><br />
Java<input type=radio name=language value="JAVA" checked><br />
C++<input type=radio name=language value="CPP"><br />
Visual Basic<input type=radio name=language value="VB"><br />
<p><br />
<input type=submit><br />
</form><br />
<%<br />
if (request.getMethod().equals("POST")) {<br />
if (request.getParameter("language").equals("JAVA")) {<br />
out.println("<hr><font color=red>You got that right!</font>");<br />
} else {<br />
//thow a new exception initializing it with some message<br />
}<br />
}<br />
%><br />
</body><br />
</html></b><br />
<br />
<b style="color: lime;"><%-- Indicate that this is an error page using the page tag --%><br />
<html><br />
<body><br />
<h1><br />
Error Page<br />
</h1><br />
<hr><br />
<h2><br />
Received the exception:<br><br />
<font color=red><br />
<%= exception.toString() %><br />
</font><br />
</h2><br />
</body><br />
</html><br />
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/ErrorHandling/errorpage.txt</b><br />
<br />
<br />
<br />
<br />
<b style="color: lime;"><%@ page import="com.jguru.CounterBean" %><br />
<jsp:useBean id="session_counter" class="com.jguru.CounterBean" scope="session" /><br />
<jsp:useBean id="app_counter" class="com.jguru.CounterBean" scope="application" /><br />
<% session_counter.increaseCount();<br />
synchronized(page) {<br />
app_counter.increaseCount();<br />
}<br />
%><br />
<h3><br />
Number of accesses within this session:<br />
<jsp:getProperty name="session_counter" property="count" /><br />
</h3><br />
<p><br />
<h3><br />
Total number of accesses:<br />
<% synchronized(page) { %><br />
<jsp:getProperty name="app_counter" property="count" /><br />
<% } %><br />
</h3><br />
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/Counter/solution/Counter.txt<br />
</b><br />
<br />
<b style="color: lime;">package com.jguru;<br />
public class CounterBean {<br />
int count;<br />
public int getCount() {<br />
return count;<br />
}<br />
public void increaseCount() {<br />
count++;<br />
}<br />
}<br />
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/exercises/Counter/solution/CounterBean.java<br />
</b><br />
<b style="color: lime;"> </b><br />
<b style="color: lime;"><%@ page errorPage="errorpage.jsp" %><br />
<html><br />
<body><br />
<form method=post action="errhandler.jsp"><br />
What's the coolest programming language in the known universe?<p><br />
Java<input type=radio name=language value="JAVA" checked><br />
C++<input type=radio name=language value="CPP"><br />
Visual Basic<input type=radio name=language value="VB"><br />
<p><br />
<input type=submit><br />
</form><br />
<%<br />
if (request.getMethod().equals("POST")) {<br />
if (request.getParameter("language").equals("JAVA")) {<br />
out.println("<hr><font color=red>You got that right!</font>");<br />
} else {<br />
throw new Exception("You chose the wrong language!");<br />
}<br />
}<br />
%><br />
</body><br />
</html></b><br />
<b style="color: lime;"><br />
</b><br />
<b style="color: lime;"><br />
</b><br />
<b style="color: lime;"><%@ page isErrorPage="true" %><br />
<html><br />
<body><br />
<h1><br />
Error Page<br />
</h1><br />
<hr><br />
<h2><br />
Received the exception:<br><br />
<font color=red><br />
<%= exception.toString() %><br />
</font><br />
</h2><br />
</body><br />
</html></b><br />
<b style="color: lime;"><br />
</b><br />
<b style="color: lime;"></b><br />
<b style="color: lime;"></b><br />
<b style="color: lime;"></b><br />
<b style="color: lime;"></b><br />
<b style="color: lime;"></b><br />
<b style="color: lime;"></b><br />
<b style="color: lime;"></b><br />
<b style="color: lime;"><br />
</b>rajhttp://www.blogger.com/profile/07775159902177850551noreply@blogger.com0