cs636 hw4 Solution

CS636 Homework 4 Solution

1)

a. "ant-config-mysqldb" followed by "ant sysTest" accesses the right persistence.xml file (the one in database/mysql/persisitence.xml) and puts it in two places, so that it is available to the running SystemTest program executed by "ant sysTest". In the execution of "ant-config-mysqldb", ant accesses values for the mysql user and password, and mysql host and port, from environment variables and places these values in the operational persistence.xml. Thus different users can execute the same project, and the same user can execute at home or on topcat, based on the values in their environment variables.

b. The obvious difference between domain classes in pizza1 and pizza2 are all the JPA annotations in pizza2, except in Cart.java, which has no changes. (The lack of difference means that Cart.java does not show up in the list of changed files, so might be missed, and shouldn't cause marks off.) All the other pizza2 classes have @Entity, @Table, @Id, and @TableGenerator annotations. Other columns have @Column annotations. There are relationship annotations in PizzaOrder: @OneToMany for PizzaOrder to PizzaTopping, @OneToOne for PizzaOrder to PizzaSize. Also PizzaTopping has @ManyToOne for PizzaTopping to PizzaOrder.


2) Browse to http:// topcat.cs.umb.edu:11600/cs636/index.html to see my little index.html page--you will be making a similar one for yourself.  Report on what this page says
Answer:
Welcome to the CS636 test page, served by eoneil's tomcat running on topcat.cs.umb.edu, on port 11600 
Link to test.html in CS636 home page, served by the departmental web server at www.cs.umb.edu, on port 80
test page.

Further Question:

Follow its link and see another little page. The first page was served by tomcat. What server served the second page?
Answer:
test.html in the CS636 home page is served by the departmental web server, www.cs.umb.edu.

3)

Install Tomcat 8.5 on Linux system topcat.cs.umb.edu, following the instructions linked from the class web page.  As instructed there, make a little HTML page accessible at http:// topcat.cs.umb.edu:NNNNN/cs636/index.html, (via the tunnel) where NNNNN is the first port assigned to you, when your tomcat is running.  This installation for your own tomcat will be in your cs636 directory, protected from other student's access.  Don't worry, tomcat will be running under your user id, and will be able to read everything it needs.  IMPORTANT: Don't ever change permissions on your cs636 directory.  Report on success or problems.

Answer: Various.

4. Tomcat and simple JSPs and servlets
  1. After you install tomcat on Linux, install the webapp named basicjsp from my Linux tomcat installation and use your ordinary browser to execute /basicjsp/date0.jsp. From home, use URL  http://localhost:xxxxx/basicjsp/date.jsp, with a tunnel from localhost:xxxxx to  topcat.cs.umb.edu:xxxxx.
  2. Similarly install the basicjstl web app from my tomcat, with its count.jsp and guess.jsp.
  3. The servlet1 project: use "ant deploy" on your copy of this project to install in your tomcat.
Run these and report on what you see, and what HTML is sent to your browser. This shows the minimal setup for JSP and JSP with JSTL. For JSP alone, all we have to have is an empty WEB-INF directory in the webapp. For JSP with JSTL (e.g. with <@ taglib... prefix="c"%> and <c: in use), all we have to do is put jstl.jar and standard.jar in WEB-INF/lib, as you see for /basicjstl.

what we see from date0.jsp:

Today's date is Sat Nov 25 15:21:33 EST 20175

html sent to browser:

<!-- Using scriptlet (now deprecated) for accessing Java from JSP -->
<HTML>
  <HEAD>
    <TITLE>Date JSP</TITLE>
  </HEAD>
  <BODY>
    <BIG>
       Today's date is Sat Nov 25 15:21:33 EST 2017
    </BIG>
  </BODY>
</HTML>

basicjstl/count.jsp:

what we see:

1
2
3
4
5
6
7
8
9
10

HTML:
<!--Note the URL above: need "/jsp" in it for usable version-->
<!--Requires jstl.jar and standard.jar in WEB-INF/lib
    of version 1.1 or newer, also at least Tomcat 5 or equivalent -->

<html>
  <head>
    <title>If with Body</title>
  </head>
  <body>

     Guess what computer language I am thinking of?

    <form method="post">
      <input type="text" name="guess" />
      <input type="submit" value="Try!" />
      <br/>
    </form>
  </body>
</html>

Not required in the answer:

--------- After submit "Java" as guess ------------------

--------- On the browser -----------------------

A new line "You guessed it!" appears above the original form.

5) JSP execution and errors. Try the following on topcat, or your home tomcat if available. Note count.jsp is preinstalled for cs636 in webapp basicjstl.

a) Try breaking count.jsp by removing the / from </c:forEach>. Report on the error.  Explain how it points to the bad line.

Answer: The error is:

HTTP Status 500 - /count.jsp (line: 15, column: 0) Unterminated &lt;c:forEach tag

Further explanation (not required for solution) will be indented:

HTTP Status 500 means "internal Server Error"

and the description is "The server encountered an internal error that prevented it from fulfilling this request".

Line 15 is actually the end of the file, and it reports that it did not find anything to terminate the <c:foreach tag (it uses &lt for the '<' character).   The problem isn't where the tag occurs (line 11) or the place we changed, effectively removing the tag terminator (line 13) but at the end of the file it expects all tags we opened to have been closed.

b) Try breaking count.jsp by misspelling body in <body>, say by deleting the b.  What happens?  Make sure the edit was effective by viewing the source.

Answer: It ignores the error and prints the count from 1 to 10 as usual.  The source shows that the b from body is missing.  If nothing else, I would expect that since we do not have a </ody> tag that http would complain about an unterminated tag for "ody", but it does not.

Doing a litle googling/research I discovered this: http://stackoverflow.com/questions/5641997/is-it-necessary-to-write-head-body-and-html-tags

where people claim that the spec says html will infer a body under certain circumstances even where no tag exists.

 

c) Find the generated java files for date.jsp and count.jsp under tomcat's work directory.  Report on their locations.

Answer: for date.jsp: work\Catalina\localhost\basicjsp\org\apache\jsp\date_jsp.java (relative to the tomcat home directory)

for count.jsp: work\Catalina\localhost\basicjstl\org\apache\jsp\count_jsp.java (relative to the tomcat home directory)

5) Tomcat at Home: Report on success or problems for these steps

a) Try out the installations in Home System tomcat installation described in steps 1-6.

Answer: varies, this is just an example:

I actually had a lot of difficulty with this.  Probably due to not having installed the Java EE version of Eclipse.  Despite the fact I would need to recreate all my projects in Eclipse I decided to reinstall it with the Java EE version.  After that I didn't encounter any further problems.

 

However, there was no step labeled 6 in the instruction write up.  Also, for the last part (creating a dynamic web project), no specific example of one was described, so I decided to user the servlet1 project, described below, as my guinea pig.  .

b) Download the servlet1 project and try “ant test1” to use curl or wget to do the HTTP GET to its URL. If you are using Windows and wget is unavailable, it means your executables path doesn’t include tomcat’s bin directory, where it resides (to fix this, see step 2 of Home PC tomcat installation). If you are using Linux or Mac, curl should be available on the system.

  Answer: Successfully installed and did "ant test1". It output:

This is class cs636.hello.servlet.HelloWorld, using the GET method at Fri Apr 24 02:15:58 EDT 2015

c) Also redeploy servlet1 in your Linux tomcat (i.e. using "ant deploy") and try it out the two ways.  Report on your results.

 topcat$ ant test1
Buildfile: /courses/cs636/public_html/servlet1/build.xml

test1:
     [echo] running wget
     [exec] --2015-11-27 12:01:58--  http://topcat.cs.umb.edu:11600/servlet1/servlet/HelloWorld
     [exec] Resolving topcat.cs.umb.edu (topcat.cs.umb.edu)... 192.168.104.84
     [exec] Connecting to topcat.cs.umb.edu (topcat.cs.umb.edu)|192.168.104.84|:11600... connected.
     [exec] HTTP request sent, awaiting response... 200 OK
     [exec] Length: 194 [text/html]
     [exec] Saving to: ‘HelloWorld.3’
     [exec]
     [exec]      0K                                                       100% 26.3M=0s
     [exec]
     [exec] 2015-11-27 12:01:58 (26.3 MB/s) - ‘HelloWorld.3’ saved [194/194]
     [exec]

BUILD SUCCESSFUL
Total time: 0 seconds

Also, starting a tunnel and browsing to http://localhost:51590/servlet1/servlet/HelloWorld:

This is class cs636.hello.servlet.HelloWorld, using the GET method at Fri Apr 24 23:06:22 EDT 2015

  Source from above web page:

<!DOCTYPE HTML>
<HTML>
  <HEAD><TITLE>A Servlet</TITLE></HEAD>
  <BODY>
    This is class cs636.hello.servlet.HelloWorld, using the GET method at Fri Apr 24 23:06:22 EDT 2015
  </BODY>
</HTML>

 6) tomcat at Home: report on success installing, running servlet1

7) JSP and Servlets: second servlet example, EmailListServlet

a) Unzip the provided ch05email.zip project and use "ant deploy" to deploy it in your home tomcat and try it out the two ways (browse to its known URL and use "ant test1").

Browsing to url (http://localhost:8080/ch05email/emailList):

email list


after clicking Join Now:

email pg 2

 

ant test1c results:

D:\ch05email>ant test1c

Buildfile: D:\ch05email\build.xml

test1c:

     [echo] running curl
     [exec]   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
     [exec]                                  Dload  Upload   Total   Spent    Left  Speed
     [exec]
     [exec]
     [exec] <!DOCTYPE html>
     [exec] <html>
     [exec] <head>
     [exec]     <meta charset="utf-8">
     [exec]     <title>Murach's Java Servlets and JSP</title>
     [exec]     <link rel="stylesheet" href="styles/main.css" type="text/css"/>
     [exec] </head>
     [exec]
     [exec] <body>
     [exec]     <h1>Thanks for joining our email list</h1>
     [exec]
     [exec]     <p>Here is the information that you entered:</p>
     [exec]
     [exec]     <label>Email:</label>
     [exec]     <span>eo@foo</span><br>
     [exec]     <label>First Name:</label>
     [exec]     <span>Joe</span><br>
     [exec]     <label>Last Name:</label>
     [exec]     <span>Smith</span><br>
     [exec]
     [exec]     <p>To enter another email address, click on the Back
     [exec]     button in your browser or the Return button shown
     [exec]     below.</p>
     [exec]
     [exec]     <form action="" method="post">
     [exec]         <input type="hidden" name="action" value="join">
     [exec]         <input type="submit" value="Return">
     [exec]     </form>
     [exec]
     [exec] </body>
     [exec] </html>
     [exec]   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0100   760  100   760    0     0   1570      0 --:--:-- --:--:-- --:--:--  1570

BUILD SUCCESSFUL

Total time: 0 seconds

D:\ch05email>

b) Also deploy ch05email in your cs.umb tomcat (i.e. using "ant deploy" on Linux at cs.umb.edu) and try it out the two ways, from topcat and also from users.cs.umb.edu.  Report on your results. Note that curl is available on users.cs.umb.edu and our other Linux systems at cs.umb.edu, as well as on topcat. 

from topcat, via lynx text browser:

tolkien@topcat:~/cs636/ch05email$ lynx localhost:51590/ch05email

                                                                                  Murach's Java Servlets and JSP                

                                                                                     Join our email list

   To join our email list, enter your name and email address below.

   Email: ____________________

   First Name: ____________________

   Last Name: ____________________

     Join Now

from topcat, using ant test1: This is using the text browser ?lynx? on topcat, but another way is to use curl on topcat

tolkien@topcat:~/cs636/ch05email$ ant test1c

Buildfile: /courses/cs636/s15/eoneil/tolkien/ch05email/build.xml

test1c:

     [echo] running curl
     [exec]   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
     [exec]                                  Dload  Upload   Total   Spent    Left  Speed
     [exec]
     [exec]
     [exec] <!DOCTYPE html>
     [exec] <html>
     [exec] <head>
     [exec]     <meta charset="utf-8">
     [exec]     <title>Murach's Java Servlets and JSP</title>
     [exec]     <link rel="stylesheet" href="styles/main.css" type="text/css"/>  
     [exec] </head>
     [exec]
     [exec] <body>
     [exec]     <h1>Thanks for joining our email list</h1>
     [exec]
     [exec]     <p>Here is the information that you entered:</p>
     [exec]
     [exec]     <label>Email:</label>
     [exec]     <span>eo@foo</span><br>
     [exec]     <label>First Name:</label>
     [exec]     <span>Joe</span><br>
     [exec]     <label>Last Name:</label>
     [exec]     <span>Smith</span><br>
     [exec]
     [exec]     <p>To enter another email address, click on the Back
     [exec]     button in your browser or the Return button shown
     [exec]     below.</p>
     [exec]
     [exec]     <form action="" method="post">
     [exec]         <input type="hidden" name="action" value="join">
     [exec]         <input type="submit" value="Return">
     [exec]     </form>
     [exec]
     [exec] </body>
     [exec] </html>
     [exec]   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0  0   760    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0100   760  100   760    0     0    697      0  0:00:01  0:00:01 --:--:--   697
BUILD SUCCESSFUL
Total time: 1 second
tolkien@topcat:~/cs636/ch05email$

from users.cs.umb.edu via browser:

tolkien@itserver6:~$ lynx topcat.cs.umb.edu:51590/ch05email                                                                                                                                                            Murach's Java Servlets and JSP

  Join our email list

    To join our email list, enter your name and email address below.

   Email: ____________________

   First Name: ____________________

   Last Name: ____________________

     Join Now

from users, using ant test1:

tolkien@itserver6:~/cs636/ch05email$ ant test1

--this also worked

c. Draw the page flow for this webapp as we did in class. If you wish, start by Xeroxing/scanning pg. 35 and then drawing on it, but drawing simple boxes is fine. It should show:

i. two boxes, one for index.jsp and one for thanks.jsp, the two pages.

ii. inside the boxes, show the form and the buttons

iii an arrow from the submit button of the form to a big dot for the servlet, with an indication of parameters sent with this request. You could write ?validation? next to the servlet dot.

iv. two arrows out from the servlet dot, one to each page, one marked ?OK?

v. an arrow from the button on thanks.jsp back to another servlet dot (the same servlet, but a different request), then on to index.jsp. Add info on any params here too.

emaillist page flow

Note: there is a parameter ?action=join? that should show on the arrow from thanks.jsp to the servlet.

In the redo case, the parameters shown on the submit arrow are still there and are used on the form.

However, this is almost complete, good enough for full credit.