Question: Lab 9 NOTE: This lab is a graded lab. The main new functionality in this lab is building on what you learned in the last

Lab 9

NOTE: This lab is a graded lab.

The main new functionality in this lab is building on what you learned in the last lab.

SUMMARY

You will have two jsp pages, one that submits user entries to the other. You will also have a regular Java class that provides methods for the second JSP page to access.

This lab will involve the following new features:

Incorporating external Java classes into a web app.

Handling submitted Request values in a separate JSP page.

DETAILS

Alrighty More web app Development!

This week, were going to expand our HTML a bit, and were going to expand our Java out to two pages. Were also going to make a class and use it in our web pages.

Youll learn how to:

Import and use external classes in JSP pages and Tomcat.

Submit form data from one JSP page to another.

Perform basic formatting of HTML pages using tables.

So were going to make a simple Movie Fan registration site. This is what were going to make:

A home registration page.

This page will have first/last name fields to fill out along with favorite movie and actor.

This information will be in an HTML form, as you did last week; but this time the form will be submitting the information to our registration confirmation page.

A registration confirmation page.

This page will instantiate a class you will create called FavoritesManager, and call methods on that class that will compare the users submitted favorites against yours (which youll put into the class when you code it).

A new java class.

This class will have two methods that will check the users favorites against yours.

checkFavoriteMovie.

checkFavoriteActor.

This class/object is being used by another object (in this case, our compiled JSP page that becomes a servlet, which is a standard java class/object), so for that reason, we dont have any beginning method such as init or main.

These are the three files youll be turning in.

So now Im going to take you through the details follow along carefully.

register.jsp

I have put the template for this file where you got this doc from.

Put this file in the same place we worked last week:

C:>>Program Files>>Apache Software Foundation>>Tomcat 9.x>>webapps>>ROOT

In this file, youre creating a simple form that asks for first/last names and a movie and actor.

Youll notice that this form is different from last weeks. It has an action parameter that is set. This tells the form which web page to submit the forms contents to once the submit button is clicked. Were sending all the info to our second jsp page.

Youll see Ive put in an HTML table. Tables help format spacing/layout on an HTML page. There are many ways to do this these days (DIVs w/ CSS, etc.), but I want to keep this as simple as possible.

A quick explanation of tables as we discussed in class:

tag encompasses a whole table, which is just rows and cells like in a spreadsheet.

is a row tag. This starts a new row.

tags, not in nor
is a cell tag. There is supposed to be the same amount of cells in each row, which is why youll see I spanned the first cell in the first row 100% so that I could center the title (its similar to merging columns).

Your actual content always goes within the

tags.

You can actually nest tables (by starting a new table inside of

), and this is one way to do more elaborate layouts of content.

Changing colors (***NOT required*** just including this for your own fun/interest if youre not familiar with HTML).

Youll see that Ive changed the background color of the cell that contains the title:

ffffcc is the color code part. Heres a color chart link that shows you a bunch of codes.

http://www.visibone.com/color/hexagon_800.gif

Feel free to change the colors of anything. Ive aligned the text of this cell, so if you were just to change the background color only, the code would look like this:

style=background-color:ffffcc

Notice that the text-align:center; has all been removed, so you could add this color code to other cells if you wanted to play around with the look of it.

Now Ive started you off with one line of html that creates a text box (

FavoritesManager.java

This will be fun were now going to create a java file, compile it, and have it in a place where the jsp files can see it and import it.

Create a project called FavoritesManager.

Then, create a class called FavoritesManager and fill in the package as moviefan.

IMPORTANT REMINDER: The package listing at the top of your java file should say:

package moviefan;

Add two instance variables and set their variables directly in code:

One for your favorite movie.

One for your favorite actor.

Create two methods:

checkFavoriteMovie

This method should be public.

It should return boolean type.

It should take a String as input.

DO NOT use the keyword static anywhere in this class. Nothing should be static.

It should check to see if the input is the same as your favorite movie instance variable.

NOTE: Use .equals() and not == when comparing two Strings.

It should then return false if not the same and true if the same.

checkFavoriteActor

Follow the guidelines of the above except checking against your favorite actor this time.

Compile it.

---

MOVE .class file into Tomcat web app:

Now, go get the .class file of this class you created.

Go to the directory where were putting our jsp files. Youll see a folder called WEB-INF

Go inside this folder and create a folder called classes

Inside the classes folder is where Tomcat will look for imported classes listed in our JSP file.

Inside the classes folder, were going to create a folder for our package in which well put our class. Call it moviefan

So lets review you now have a folder structure like this:

ROOT>>WEB-INF>>classes>>moviefan

Now in this moviefan folder, you should have your FavoritesManager.class file.

confirm.jsp

I have put the template for this file in the same place as the other files.

Put this file in the same place as the register.jsp file above.

Look for the ##### signs for guidance/notes on where you need to add code and what you need to do.

NOTE: Youll see that Im having you declare a variable and instantiate an object of FavoritesManager class type, which is the .class file you just added to the web app folder structure. This line of code is exactly like the other times when weve instantiated a class. Youll also notice that your external class is imported up at the top of the file, and youll also notice that the moviefan folder is referenced in that import.

Time to Test. Make sure Tomcats running (stop and restart if already running) and open your browser, and navigate to your page just like you did last week. Only now, youre editing the url to point to register.jsp.

You should be able to submit values and see the results on the second page. Be patient with the debugging and work through it. If you have any stumpers, post it to the open dboard or e-mail me.

If you adjust your code in the .class file and build it to make a new .class file, then you will need to restart Tomcat to see the changes.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!