Grossmont College Incorporating External Java Classes Into a Web App HW

User Generated

lbhfrsonqv

Computer Science

Description

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

  1. SUMMARY
    1. 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.
    1. This lab will involve the following new features:
      1. Incorporating external Java classes into a web app.
      2. Handling submitted Request values in a separate JSP page.
  1. DETAILS


Alrighty… More web app Development!

This week, we’re going to expand our HTML a bit, and we’re going to expand our Java out to two pages. We’re also going to make a class and use it in our web pages.

You’ll 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 we’re going to make a simple “Movie Fan” registration site. This is what we’re going to make:

  1. A home registration page.
    1. This page will have first/last name fields to fill out along with favorite movie and actor.
    2. 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.
    1. This page will instantiate a class you will create called FavoritesManager, and call methods on that class that will compare the user’s submitted favorites against yours (which you’ll put into the class when you code it).
  3. A new java class.
    1. This class will have two methods that will check the user’s favorites against yours.
      1. checkFavoriteMovie.
      2. checkFavoriteActor.
    2. 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 don’t have any beginning method such as “init” or “main.”

These are the three files you’ll be turning in.

So now I’m going to take you through the details… follow along carefully.

  1. “register.jsp”
    1. I have put the template for this file where you got this doc from.
    2. Put this file in the same place we worked last week:

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

    1. In this file, you’re creating a simple form that asks for first/last names and a movie and actor.
    2. You’ll notice that this form is different from last week’s. It has an “action” parameter that is set. This tells the form which web page to submit the form’s contents to once the submit button is clicked. We’re sending all the info to our second jsp page.
    3. You’ll see I’ve 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.
    4. A quick explanation of tables as we discussed in class:
      1. <table> tag encompasses a whole table, which is just rows and cells like in a spreadsheet.
      2. <tr> is a row tag. This starts a new row.
      3. <td> is a cell tag. There is supposed to be the same amount of cells in each row, which is why you’ll see I spanned the first cell in the first row “100%” so that I could center the title (it’s similar to merging columns).
      4. Your actual content always goes within the <td> </td> tags, not in <tr> nor <table> tags.
        1. You can actually nest tables (by starting a new table inside of <td> </td>), and this is one way to do more elaborate layouts of content.
    5. Changing colors (***NOT required*** …just including this for your own fun/interest if you’re not familiar with HTML).
      1. You’ll see that I’ve changed the background color of the cell that contains the title:

<td colspan="100%" style="text-align:center;background-color:ffffcc">

      1. ffffcc is the color code part. Here’s a color chart link that shows you a bunch of codes.


      1. Feel free to change the colors of anything. I’ve 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.
    1. Now I’ve started you off with one line of html that creates a text box (<input…). You’ll see that I’ve 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 you’ll be using in your other jsp page.
  1. FavoritesManager.java
    1. This will be fun… we’re 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.
    2. Create a project called FavoritesManager.
    3. Create a package called “moviefan” by going to file > new > package.
    4. Then, right click on this new folder you now see in the src folder called moviefan, and create a new class called FavoritesManager.
      1. IMPORTANT REMINDER: The package listing at the top of your java file should say:

package moviefan;

    1. Add two instance variables and set their variables directly in code:
      1. One for your favorite movie.
      2. One for your favorite actor.
    2. Create two methods:
      1. 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.
          1. NOTE: Use .equals() and not == when comparing two Strings.
        6. It should then return false if not the same and true if the same.
      2. checkFavoriteActor
        1. Follow the guidelines of the above except checking against your favorite actor this time.
    3. Compile it.
    4. ---
    5. MOVE .class file into Tomcat web app:
      1. Now, go get the .class file of this class you created.
      2. Go to the directory where we’re putting our jsp files. You’ll 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, we’re going to create a folder for our package in which we’ll put our class. Call it “moviefan
        1. So let’s review… you now have a folder structure like this:

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


      1. Now in this moviefan folder, you should have your FavoritesManager.class file.
  1. “confirm.jsp”
    1. I have put the template for this file in the same place as the other files.
    2. Put this file in the same place as the “register.jsp” file above.
    1. Look for the ##### signs for guidance/notes on where you need to add code and what you need to do.
    1. NOTE: You’ll see that I’m 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 we’ve instantiated a class. You’ll also notice that your external class is imported up at the top of the file, and you’ll also notice that the moviefan folder is referenced in that import.

Time to Test. Make sure Tomcat’s running (stop and restart if already running) and open your browser, and navigate to your page just like you did last week. Only now, you’re 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.

  1. TURNING IN LAB:
    1. Turn your lab into Canvas.
    2. IMPORTANT: DO NOT ZIP your folders/files please.
    3. Turn in ALL the JSP/java files for this lab.

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Hey buddy,The assignment is completeI have added some CSS ...


Anonymous
Very useful material for studying!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Similar Content

Related Tags