Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. SUMMARY a. You will have two jsp pages, one that submits user entries to the other. You will also have a regular Java class

1. SUMMARY

a.

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.

b. This lab will involve the following new features:

i. Incorporating external Java classes into a web app.

ii. Handling submitted Request values in a separate JSP page.

2. 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:

1. Import and use external classes in JSP pages and Tomcat.

2. Submit form data from one JSP page to another.

3. 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:

1. A home registration page.

a.

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

b. 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.

2. A registration confirmation page.

a.

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).

3. A new java class.

a.

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

i. checkFavoriteMovie.

ii. checkFavoriteActor.

b. 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.

1. register.jsp

a.

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

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

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

c.

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

d. 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.

e. 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.

f.

A quick explanation of tables as we discussed in class:

i.

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

spreadsheet.

ii.

is a row tag. This starts a new row.

iii.

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).

iv. Your actual content always goes within the

tags.

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

), and

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

g.

Changing colors (***NOT required*** ...just including this for your own fun/interest if youre

not familiar with HTML).

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

ii. 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

iii. 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

1. 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.

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

that Ive created comment lines where you need to add lines for the appropriate input. Be

sure to adjust the name parameter correctly, because this is the lookup value youll be using in

your other jsp page.

2. FavoritesManager.java

a.

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.

b. Create a project called FavoritesManager.

c.

Create a package called moviefan by going to file > new > package.

d. Then, right click on this new folder you now see in the src folder called moviefan, and create a

new class called

FavoritesManager

.

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

package moviefan;

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

i. One for your favorite movie.

ii. One for your favorite actor.

f.

Create two methods:

i. checkFavoriteMovie

1. This method should be public.

2. It should return boolean type.

3. It should take a String as input.

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

static.

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

variable.

a.

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

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

ii. checkFavoriteActor

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

this time.

g.

Compile it.

h. ---

i.

MOVE .class file into Tomcat web app:

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

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

INF

1. Go inside this folder and create a folder called

classes

2. Inside the classes folder is where Tomcat will look for imported classes listed

in our JSP file.

3. Inside the classes folder, were going to create a folder for our package in

which well put our class. Call it

moviefan

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

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

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

3. confirm.jsp

a.

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

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

c.

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

need to do

.

d. 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.

register.jsp:

Movie Fan Membership Registration

First Name

Last Name

Favorite Movie

Favorite Actor

confirm.jsp:

<%@ page import="moviefan.FavoritesManager"%>

<%

//#####################################################################################

//###### Your java code here to declare and instantiate FavoritesManager object. ######

//###### You will be using this variable in the "if" statements lower down. ######

%>

back to Registration page

Welcome,

<%//#################################################################

//###### Your java code here to combine first with last name.######

//###### Don't forget to put a space between first and last. ######

//###### I've given you the first part of the line... ######

//###### (in other words, replace the three periods) ######%>

<%= request.getParameter("firstname") + ... %>

Your favorite movie (<%=request.getParameter("movie")%>)

<%

//#####################################################

//###### Your java code here to call movie check ######

//###step 1: create a String variable and fill it with the

//request parameter called "movie" ...

//Reference the last lab for the syntax on accessing the request object

//or just look above at how we're grabbing the firstname param.

//###step 2: create an if statement.

//Inside the parentheses, not brackets, of the if statement, call

//the movie method on your external class you've created and instantiated

//up towards the top of this file.

//Your passing the string you've created above that has the movie value.

//Now, within the first set of curly brackets of the if statement, only put this line of code:

%>is my favorite movie!<%

//Then do an else statement, and within the brackets, only put this line:

%>is not my favorite movie.<%

//This space is just here for you to put the closing curly bracket of the else statement.

%>

Your favorite actor (<%=request.getParameter("actor")%>)

<%

//#####################################################

//###### Your java code here to call actor check ######

//### For this section, follow the steps up above again, only you're

//substituting actor now instead of movie.

%>

Please screenshot if you can Thank you!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

ISBN: 0071808183, 9780071808187

More Books

Students also viewed these Databases questions