Chapter 2 Tutorials
Simple
-
Use the project that was created in the
Chapter 1 Tutorials.
-
The public files belong in
src/main/webapp . If you are using an
IDE, it may have an alias for the folder. For instance, in Netbeans, the folder
has the name Web Pages .
-
Create a new folder named
ch2 .
-
Create a subfolder of
ch2 named TwoPages .
-
Create a subfolder of
TwoPages named Simple .
-
Create a new JSP named
Edit.jsp .
-
Copy the contents of
First.jsp into this file.
-
Modify the form so that it sends its data to the confirm page by
adding an
action attribute with the value Confirm.jsp .
-
Create a new JSP named
Confirm.jsp .
-
Add the standard HTML tags: html, head, title, body.
-
Add the tags for validating the page: DOCTYPE, meta for content-type.
-
Display the value of the data that was sent to this page.
-
Modify the
index.jsp file already exists in the Web Pages
folder.
-
This is the welcome page for the application.
-
Modify the page by adding a hypertext link to the
Edit.jsp file.
-
Run the project.
-
The
index.jsp page will appear in the browser.
-
Click the link for the edit page.
-
Enter some text in the text element.
-
Click the submit button.
-
Notice that the text that was entered has been sent to the confirm page.
-
Notice that the form data is in the query string.
-
Validate the the
Edit.jsp and Confirm.jsp files
contain valid HTML.
Two Directions
-
In the
TwoPages folder, create a page named
Edit.jsp .
-
Copy the contents of the
Edit.jsp from the Simple
tutorial.
-
Be sure that the text element is initialized with the corresponding data
from the query string.
-
In the
TwoPages folder, create a page named
Confirm.jsp .
-
Copy the contents of the
Confirm.jsp from the Simple tutorial.
-
Add a form with a hidden field and a submit button.
-
The form should send its data to the edit page.
-
Initilialize the hidden field with the data from the text element in the
query string.
-
Be sure that the name of the hidden field is the same as the name of the
text element in the edit page.
-
Modify the
index.jsp file already exists in the Web Pages
folder.
-
This is the welcome page for the application.
-
Modify the page by adding a hypertext link to the new
Edit.jsp
file.
-
Run the project.
-
The
index.jsp page will appear in the browser.
-
Click the link for the new edit page.
-
Enter some text in the text element.
-
Click the submit button.
-
The data will appear in the confirm page.
-
Notice that the form data is in the query string.
-
Notice that the data from the text element is not visible.
-
Click the edit button.
-
The edit page will appear.
-
Notice that the form data is in the query string.
-
Notice that the data from the query string has been used to initialize the
text element.
Three Pages
-
Create a new folder named
ThreePages in the ch2
folder.
-
Create a new JSP named
Process.jsp .
-
Copy the contents of the confirm page from the
Simple tutorial.
-
This page only echoes the data from the query string, there is no form in
it.
-
Copy the edit page from the
Two Directions tutorial.
-
Copy the confirm page from the
Two Directions tutorial.
-
Add a new form to the page.
-
The form should send its data to the process page.
-
Add a hidden field for the data from the text element.
-
Add a submit button named Process.
-
There are now two forms in this page.
-
Each form send data to a different page.
-
Each form has a hidden field for the data.
-
Modify the
index.jsp file already exists in the Web Pages
folder.
-
This is the welcome page for the application.
-
Modify the page by adding a hypertext link to the new
Edit.jsp
file.
-
Run the project.
-
The
index.jsp page will appear in the browser.
-
Click the link for the new edit page.
-
Enter some text in the text element.
-
Click the submit button.
-
The data will appear in the confirm page.
-
Notice that the form data is in the query string.
-
Notice that the data from the text element is not visible.
-
Click the edit button.
-
The edit page will appear.
-
Notice that the form data is in the query string.
-
Notice that the data from the query string has been used to initialize the
text element.
-
Click the confirm button.
-
The data will appear in the confirm page.
-
Notice that the form data is in the query string.
-
Notice that the data from the text element is not visible.
-
Click the process button.
-
The process page will appear.
-
Notice that the form data is in the query string.
-
The data from the query string has been displayed in the page.
JSP Controller
-
Create a new folder named
jspController in the ch2
folder.
-
Copy the edit, confirm and process pages from the
Three Pages
tutorial.
-
Create a new JSP named
Controller.jsp .
-
Do not add any HTML tags to his page.
-
Add a block of Java code, inside the special JSP tags for Java code.
-
Add a nested
if-else block that translates button names into
JSP addresses.
-
If the confrim button is in the query string, then the address should be
for the confirm page.
-
If the process button is in the query string, then the address should be
for the process page.
-
The default address should be for the edit page, since this is the first
page that should appear when the application is accessed first.
-
The name of the buttons must agree with the name of the buttons that are
in the pages; button names are case sensitive.
-
The name in the address must agree with the name of the pages; addresses
are case sensitive.
-
Add the statements that will forward the request and response to the correct
address.
-
Modify the edit page.
-
Modify the form so that it sends its data to the controller.
-
Modify the confirm page.
-
Remove the second form from the page; there should only be one form in this
page.
-
The form will send its data to the controller.
-
The form will have one hidden field for the data in the query string.
-
The form will have two buttons with different names.
-
One button will have the name
Edit .
-
One button will have the name
Process .
-
Modify the
index.jsp file already exists in the Web Pages
folder.
-
This is the welcome page for the application.
-
Modify the page by adding a hypertext link to the
Controller.jsp
file.
-
Do not link to the edit page; all accesses should be to the controller.
-
Run the project.
-
The
index.jsp page will appear in the browser.
-
Click the link for the controller page.
-
The edit page will appear, but the URL still shows the controller.
-
Enter some text in the text element.
-
Click the submit button.
-
The data will appear in the confirm page.
-
The confirm page will appear, but the URL still shows the controller.
-
Notice that the form data is in the query string.
-
Notice that the data from the text element is not visible.
-
Click the edit button.
-
The edit page will appear.
-
The edit page will appear, but the URL still shows the controller.
-
Notice that the form data is in the query string.
-
Notice that the data from the query string has been used to initialize the
text element.
-
Click the confirm button.
-
The data will appear in the confirm page.
-
The confirm page will appear, but the URL still shows the controller.
-
Notice that the form data is in the query string.
-
Notice that the data from the text element is not visible.
-
Click the process button.
-
The process page will appear.
-
The process page will appear, but the URL still shows the controller.
-
Notice that the form data is in the query string.
-
The data from the query string has been displayed in the page.
Servlet Controller
-
Java source files belong in the
src/main/java folder. An IDE might
create an alias for the folder. Netbeans uses the alias Source Packages .
-
Create a new folder named
ch2/servletController in the java folder.
-
Add a java source file to the folder named
Controller.java .
-
Create a public class and modify it as follows to make it a servlet.
- Add an appropriate package statement.
- Import the following packages:
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
-
Extend the class from
HttpServlet .
-
Add the
WebServlet class annotation to the class setting
the URL for the controller as "/ch2/servletController/Controller".
-
Add a
doGet method.
-
Add the parameters for method.
-
Throw the required exceptions.
-
Copy the contents of the controller page from the jspController tutorial
into the body of the
doGet method. (Better yet, WRITE the code!)
-
Do not copy the opening and closing JSP tags.
-
No other changes need to be made to the code.
-
Create a new folder named
servletController in the
src/main/webapp/ch2 folder. This will be the location of the JSPs for
the controller.
-
Copy the edit, confirm and process pages from the
jspController
tutorial.
-
Modify the form in the edit page so that it send its data to the controller.
-
The controller is located at the URL that was defined in the
WebServlet annotation: /ch2/servletController/Controller
-
The path to the controller is
/ch2/servletController , which
is the same as the path to the JSPs.
-
Use a relative reference for the controller, since it has the same path as the
path to the JSPs.
-
Change the action in the form to
"Controller" .
-
Do not include an extension. The name agrees with the URL pattern in the annotation.
-
This indicates that the full path to the controller is the current path followed
by
Controller .
-
The current path is
/ch2/servletController , so the actual path
to the controller is /ch2/servletController/Controller , which
agrees with the URL pattern in the WebServlet annotation.
-
Relative references are used as much as possible.
-
The key to the use of relative references is that the controller can be found
at the location
/ch2/servletController/Controller .
-
The JSPs were placed in the folder
/ch2/servletController/ because
that is the path that was used in the URL pattern for the controller.
-
It is the URL pattern in the
web.xml that determines where the
JSPs are located.
-
Modify the
index.jsp file already exists in the Web Pages
folder.
-
This is the welcome page for the application.
-
Modify the page by adding a hypertext link to the controller.
-
Use the value from the URL pattern in the
WebServlet annotation, but
remove the leading / .
-
The leading
/ can be removed, since the index.jsp
file is in the root of the web application and the leading slash in the URL
pattern refers to the root of the web application.
-
Do not link to the edit page; all accesses should be to the controller.
-
Stop the application if it is running.
-
Use the maven commands to delete the old application files and package them again.
This will include compiling the servlet:
mvn clean package
-
Then start the application again.
mvn tomcat7:run
-
The
index.jsp page will appear in the browser.
-
Follow the same steps as for the
jspController tutorial.
|