Struts Exercises
Struts Exercises
Exercises used in Martys live Struts and advanced JSP training courses.
See http://courses.coreservlets.com/ for public course schedule. Email [email protected] to arrange an onsite course at your location. See http://courses.coreservlets.com/Course-Materials/ for Struts notes and examples.
http://courses.coreservlets.com
3.
4.
5.
6.
7. 8.
6. 7.
http://courses.coreservlets.com
2. Redo the color preference example, but if the user fails to supply either of the colors, use whatever value they gave last time. If you have no previous value, use a default.
3. Redo the color preference example, but if the user fails to supply any of the parameters, use whatever color the most recent user gave last time. How do you avoid race conditions?
4. Prohibit the foreground and background colors from being the same.
http://courses.coreservlets.com
http://courses.coreservlets.com
2.
Put a servlet in your Web app and access it. Unless youve read ahead, you will have to use the invoker servlet (i.e., http://localhost/webappName/servlet/ServletName). However, in the next lecture we will see how to use a URL that is explicitly registered in web.xml (i.e., http://localhost/webappName/anything). Deploy a second Web app, but use a WAR file this time. If you want to use Java to make the WAR file, open a DOS window, go into the top-level of an existing Web app, and say jar -cvf someName.war *. You can also use WinZip or the Windows XP create compressed folder R-mouse option to make a file called someName.war. Either way, keep the original copy of someName.war in C:\Servlets+JSP. To deploy someName.war, copy it to tomcat_dir/webapps (there is a shortcut in C:\Servlets+JSP), and use the URL http://localhost/someName/.... Try to share data between your Web apps by means of the servlet context. Will the default Tomcat setting let you do this? Make a servlet that creates a cookie. Be sure to set the path appropriately. Make another servlet (or a JSP page) that displays the cookie value. Copy the cookie-displaying program to another Web application and verify that you can see cookies that were created in the first Web app.
3.
4. 5.
http://courses.coreservlets.com
Exercises: web.xml
1.
Make a blank Web application (the easiest way is to copy and rename the appblank Web app). Copy the servlet your wrote for the previous exercise, give it a custom address in web.xml, and access it using that address. For the remaining problems, you are the CTO of Enroff, a company that is so popular that other big companies keep buying you out for higher-and-higher prices. Thats the good news. The bad news: the company name keeps changing, making you keep changing your home page.
2. Make a servlet that reads the company name from an initialization parameter and
then uses it in the same page.
3. Repeat problem 2 (using an init parameter), but use a JSP page instead of a servlet.
4. Make sure that this JSP page is displayed if the user requests
http://host/yourCompany/, with no filename specified.
5. Turn off the invoker servlet (if you made your own Web app from scratch), or verify that the invoker servlet is already disabled (if you started with app-blank).
6. Have a servlet read the company name from an init parameter, store it in the servlet context, and use it in the servlets output. Have two JSP pages that use the same company name, but dont have the JSP pages re-read the init parameter.
7. Be sure nothing bad happens if someone tries to access the above JSP pages before the above servlet that sets the company name.
8. Make a servlet or JSP page that reads a request parameter and prints a message
saying if that value is a substring of the current company name. If the request parameter is missing, display a designated page that is not accessible directly (i.e., there is no URL that the client can supply that directly yields the error page). Dont use any try/catch blocks or explicit checks for null.
http://courses.coreservlets.com
1. 2. 3. 4.
Create a JSP page that can be accessed only by registered users or administrators. Use form-based authentication. Repeat the above, but with BASIC authentication. Hint: just copy your Web application, give it a different name, and make one change to the web.xml file. Back to the first Web app (the one that uses form-based authentication): create a servlet that is also accessible only to registered users and administrators. Make a color preference form that collects the users preferred foreground and background colors. Send it to a page that presents some information using those colors. Use the MVC approach, and make sure the resource is accessible only to registered users and administrators.
http://courses.coreservlets.com
3. 4.
http://courses.coreservlets.com
Exercises: Filters
1. 2.
Download the ReportFilter. Apply it to at least one servlet and at least one JSP page. Check the Tomcat window to verify that it is reporting accesses to those pages. Make a filter that prints a report saying whether or not the user accessing the designated resource is already logged in as an administrator. Test it by applying it to resources from the previous exercise. (Hint: just copy/rename the Web app that supported form-based security, and add in the filter.)7 The name of your CEO keeps changing, but it is already embedded in several pages. Make a filter that updates those pages. Dont hardcode the names into the filter itself. Make a filter that turns the entire page (HTML tags and all) into lower case. Dont worry about whether or not this results in legal HTML (e.g., changing case of the DOCTYPE line is not legal, but you dont have to worry about that). Make a filter that removes all BLINK tags from the resources to which it is applied. Note: in JDK 1.4, the replaceAll method can be used to replace all occurrences of a given substring (regular expression, actually) by another substring. For example, "foobarbazfoobarbaz".replaceAll("foo", "Test"); returns "TestbarbazTestbarbaz" Prepend the first argument with "(?i)" for a case-insensitive replacement.
3. 4. 5.
http://courses.coreservlets.com
Exercises: Listeners
1. Redo the JSP page that displays the company name. Have it read the company name
from a servlet context attribute, and use a listener to set up the attribute. Have the listener also store the current value of the company stock, and display that value in the JSP page.
2. Create a way for an authorized user in the executive role to change the company name. 3. Arrange it so that, whenever the company name changes, the stock value increases by
25%.
4.
Make a listener that keeps track of how many sessions are currently active. Make a servlet or JSP page that displays the value.
5. Create an HTML or JSP page that lets the user put Wonkas Wonder Widget in their
shopping cart (thats the only item your site is selling). Use session tracking to show the user how many of these widgets are in their shopping cartyou dont have to handle the actual purchase, just the tracking of how many items each person has reserved. One wrinkle, though: if there are more than 20 active sessions, dont let new users create sessions: return an error message instead. Give users a log out of session button to simplify your testing. (Hint: in basic session tracking, you call request.getSession() (equivalent to request.getSession(true)), where the true indicates that the system should make a session if it cannot find a preexisting session for that client. If you call request.getSession(false), the system returns an existing session if it finds it, and returns null otherwise.).
http://courses.coreservlets.com
3.
4.
Redo the previous three tags with JSP 2.0 tag files.
http://courses.coreservlets.com
3.
Make a heading tag that expands into <H1 ALIGN="CENTER">... Make an emphasis tag that normally puts the enclosed text in bold. However, since headings are already in bold, if emphasis is used inside heading, it should put the enclosed text in italics instead.
http://courses.coreservlets.com
http://courses.coreservlets.com
2.
3.
4.
Make a third address that also lets the user select a preferred health plan, gathering the same information. But, this time just say OK if all three values are present; Missing Data otherwise. [Hint: you do not need to write a new Action.]
http://courses.coreservlets.com
1. 2. 3.
Use beans instead of calling request.getParameter explicitly. Display the name, ID, and health plan in the confirmation page Make a single page to display messages about missing data. List all the missing parameters, not just the first missing one as before.
http://courses.coreservlets.com
1. 2. 3.
Make Joe User the default employee name, t1234 the default employee ID, and Blue Cross the default health care provider. Display these values in the input form. Redisplay the form if any of the values are missing or if the health care provider is anything other than Blue Cross, Red Cross, or Black Diamond. Extend problem 2 so that a warning message is given when the form is redisplayed. Hint: instead of using a separate bean to store the error message, put the error message in the form bean.
http://courses.coreservlets.com
http://courses.coreservlets.com
2. 3. 4.
http://courses.coreservlets.com
http://courses.coreservlets.com
Manual Validation
1.
For the health-plan application, enforce the restriction that all three values must be non-empty and the health plan name must be one of Blue Cross, Red Cross, or Black Diamond.. When any fields are missing or illegal, redisplay the form with a list of missing fields. Do this in the execute method of the Action. Note that you may have already done most or all of this in the previous set of exercises. If so, just use that as a starting point.
2. 3. 4. 5.
Do the same thing, but using the validate method of the form bean. Use the validate method for the rest of the examples as well. Put the error messages in a red sidebar on the right side of the page, next to the HTML form. (Hint: copy the previous Web app, and then the only thing you need to change is the properties file.) Put the error messages right next to the input form. Require the employee ID to be a single character followed by exactly four digits.
http://courses.coreservlets.com
Automatic Validation
Note. For details on the validator rules, see http://struts.apache.org/userGuide/dev_validator.html
1. 2. 3.
Enable server-side validation. Enforce the restriction that all three values must be non-empty. Require the employee ID to be five characters long. Require the employee ID to be a single character followed by exactly four digits. Note: here is the necessary regular expression for mask
<var> <var-name>mask</var-name> <var-value>^[a-zA-Z]{1}\d{4}$</var-value> </var>
4. 5. 6.
Enable client-side validation. Try to evade the client-side validation. Make a form that collects a name, password, employee number (6 digits without dashes), and email address. Verify that all values are present and of the appropriate format.
http://courses.coreservlets.com
Tiles
1.
Create a layout that looks like this:
The top left and top right should be company logos (pictures), the top-center should be the page title in large letters, and the bottom should have contact information.
2. 3.
Make three concrete versions of this: corporate home page (title: the company name), corporate jobs page (title: Careers at xxxx), corporate contact page (title: Contact Us). Redo problem 2 using Tiles definitions.
http://courses.coreservlets.com
Exercises: JSTL
1. 2. 3. 4.
Make a JSP page that creates a list of 13 random numbers. Use a simple JSP expression for each individual random number: <%= Math.random() %>. Create a servlet that stores a bean containing an array of names, then forwards to a JSP page using RequestDispatcher. In the destination JSP page, loop down the array and put them into a bulleted list. Repeat problem #2 with an ArrayList (or LinkedList or Vector) and HashMap. Create a servlet that stores a bean with two arrays: one with first names and one with the corresponding last names. Have your JSP page make an HTML table with first names in the left table cell and last names in the right table cell. Assume that the arrays have exactly five elements. Create a servlet that makes an array of Name objects, which have firstName and lastName properties. Repeat the HTML table, but this time you dont need to know how many entries you have in the array. Use the JSP 2.0 expression language as well as JSTL. Repeat #4, but if the first name is Marty, add (Monty) between the first and last names. Repeat #4, but If the first name is Marty, replace it with Monty instead. If the first name is Bill, use Microsoft instead. If the first name is Scott, use Sun instead. If the first name is Larry, use Oracle instead.
5.
6. 7.
http://courses.coreservlets.com
Student Survey
1.
Please rate the following from 1 (poor) to 5 (great): Instructor: ______ Topics: ______ Handouts: ______ Exercises: ______ Book: Facilities: Overall: ______ ______ ______
2.
3.
4.
http://courses.coreservlets.com