42
Java II--Copyright © 2001-2007 Tom Hunter

Web Application Best Practies

Embed Size (px)

Citation preview

Page 1: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Page 2: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

J2EEWeb Application Best

Practices

Page 3: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

Best Practices

Page 4: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• After you have coded a few dozen web applications, you start to get some ideas about how things should be done.

• This lecture is designed to reduce your pain. To understand what I’m talking about here, you will need to have completed all the J2EE lectures before this.

Web Apps: Best Practices

This lecture was made possible by the assistance of Mr. David Harvey of Salt

Lake City, UT.

Page 5: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• When should I use an HttpSession and sendRedirect()?

• When should I use RequestDispatcher and forward()?

• Should I execute a JDBC call from within a JSP?

• How do I refer to a Java static constant in my JSP page?

• Can I have an HTML form post to more than one action?

• JSP allows me to create a method in my JSP. Is that a good idea?

• HTML Tables: width percentages versus absolute pixel sizes.

Web Apps: Best Practices

Page 6: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• I need to do a JavaScript Popup. How do I do that?

• I need to submit my page using JavaScript. How do I do that?

• I need to communicate between Java and JavaScript. How can I do that?

Web Apps: Best Practices

Page 7: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:HttpSession/sendRedirect()

Vs

RequestDispatcher/forward()

Page 8: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• When you’re writing a web app, you follow a pattern: the user starts on a JSP, then posts or links to a Servlet—where some processing is done—and then the Servlet needs to send the user to the next JSP page.

• There are two ways of doing that.

• One involves a persistent HttpSession. In that case, a person who is traveling around in a web application carries with them a SESSION ID that is the primary key for a chunk of data called the HttpSession.

Web Apps: HttpSession vs. RequestDispatcher

Page 9: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• When you are using an HttpSession as your means of maintaining state between requests, then you will likely use the method called a sendRedirect() as a means to send your users to the next page.

Web Apps: HttpSession

As you see here, we get the HttpSession object from the HttpServletRequest object. Then, our bean—literally a JavaBean—is pulled out of the session object as an attribute.

After all of our servlet’s processing is done, at the end of the method, you execute a sendRedirect() to the page you want to reach next. To better understand how this JSP path works, know that /admin is the contextRoot of this application. (Look at the URL)

Page 10: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• The previous slide showed how to use the HttpSession and the sendRedirect().

• When should I use the HttpSession and sendRedirect()?

1. If your number of simultaneous users is NOT in the hundreds of thousands.

2. If your server is configured to have a long time before the session times out.

3. If your application is a shopping cart, then the forward() has the potential to submit submit your order twiceyour order twice if someone refreshes the page.

Web Apps: HttpSession

Page 11: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• The other way of getting from page to page involves using a RequestDispatcher object.

• In this style, there is no bean stored in session because there is nois no session.

• Anything you want to be passed from one page to the next or one servlet to the next must be inserted into the HttpServletResponse object manually.

Web Apps: RequestDispatcher

Page 12: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• In this example, we see that I arrived at this doGet() method and then removed a bean from the request.

• Finally, you see that I am manually building the URL with the correct parameters. This is a lot more work than using session!

Web Apps: RequestDispatcher

http://localhost:8100/educator/educatorSearch.jsp

Page 13: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

Should I Execute JDBC From Inside

a JSP?

Page 14: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps: Should I Execute JDBC From Inside a JSP?

Page 15: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• Why not execute JDBC call from within a JSP?

1.) Violates Model-View-Controller separation.

2.) Mixes logic with presentation—which is a pain.

3.) If any exceptions happen, they will appear on the page and not in the logs. You will have no record of

what happened until you put aSystem.out.println() on the page.4.) If you have multiple requests, it is

possible to bestarting a second request before the

first one hascompleted—with bizarre effects on

Connections.5.) You will be embarrassed by that

JDBC-in-JSP code when you get more experience. (I was!)

Web Apps: Should I Execute JDBC From Inside a JSP?

Page 16: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

Java static

Constants On A JSP

Page 17: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• As you should now, it is always a bad idea to have hard-coded values on your JSP page. Take parameter names, for example. You name it on the page and pull it out of the request using the same name.

• Who hasn’t made the mistake in which you gave a parameter one name on the JSP and another name in the Servlet.

• This method prevents that problem from happening.

Web Apps: Java static Constants On A JSP

Page 18: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• First of all, you need to make the constants available.

Web Apps: Java static Constants On A JSP

You see we have two public static final String objects that contain the actual parameter value. But since we really only deal with the constant, we don’t care what the parameter really is—just so it’s unique.

Page 19: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• Next we need to make the constants visible on our JSP by importing the class.

Web Apps: Java static Constants On A JSP

This does the import. Please notice that the <jsp:useBean … does not import the class.

And so you see here that we are just referring to the value by its constant. Notice that the entirety of the JSP Expression is within double quotes.

Page 20: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• Finally, we see how this is consumed in the Servlet using the same constant.

Web Apps: Java static Constants On A JSP

This approach prevents the problem I described a few slides ago. You will always find your parameters this way.

Page 21: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

Make an HTML Form Post to Two

Actions

Page 22: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• Sometimes on a JSP page, you want to post to one Servlet for validation, then to a different Servlet for the update.

Web Apps: Make an HTML Form Post to Two Actions

Notice how the page starts off pointing at one Servlet. When the user exits out of a text field, they trigger the onBlur function, which triggers a post to the validateInput() function.

During the post, it changes the action and points to a

different servlet.

Page 23: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

JSP Lets me Create Methods in JSPs

Page 24: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps: JSP Lets me Create Methods in JSPs

Page 25: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• There are a lot of bad habits available in the JSP world and one of the worst is creating a method on a JSP.

• Although it is technically possible, it is impossible to debug. If you’re doing this—create a new class or a JSP Custom Tag.

• Don’t do it!

Web Apps: JSP Lets me Create Methods in JSPs

Page 26: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:HTML Tables:

width % vs. Pixels

Page 27: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• As a web developer, you have few choices except to use the HTML table to layout your page.

Web Apps: HTML Tables: width % vs. Pixels

In this case, we have two HTML tables. They are identical with one exception. The upper table has a width=“75%”.

I have left off the width value for the last cell to emphasize the difference.

Page 28: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• This is the output of the code below. You see how the <TABLE>’s width attribute is forcing the table to be a certain size. The presence of the absolute values affects the sizes of TDs but the last cell is forced to pick up the slack.

Web Apps: HTML Tables: width % vs. Pixels

In this case, we have two HTML tables. They are identical with one exception. The upper table has a width=“75%”. I have left off the width value for the last cell to emphasize the difference.

Page 29: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

JavaScript Popup—How to do that

Page 30: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• Although users hate having to dismiss a popup using their mouse—as they invariably have to do—you should know how to make one.

Web Apps: JavaScript Popup—How to do that

In this example, the submit causes the JavaScript function doPopup() to be executed.

The popup relies on a URL being supplied via a Java instance variable. The remainder of the values have pretty self explanatory values. Check out the link in red below for more info on the window.open() command.

Click here to see Microsoft’s API on this command.

Page 31: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• the “restOfLink” below in the URL should point to a JSP page that you have built. (In other words, this does not create a JavaScript-type defined box. You have to build it.)

Web Apps: JavaScript Popup—How to do that

String url = "http://" + serverName + ":" + serverPort + restOfLink;

Page 32: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

Submitting a JSP using JavaScript

Page 33: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• This is a pretty typical thing but it should be documented.

Web Apps: Submitting a JSP using JavaScript

1—the user enters data into the text boxes.

2—the user click on the “Submit” button.3—Because the button is type=“submit” and because because the text boxes the text boxes and the button and the button are inside of are inside of the the form, the page is submitted.

Page 34: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• This is a pretty typical thing but it should be documented.

Web Apps: Submitting a JSP using JavaScript

1—the user enters data into the text boxes.

2—the user click on the “Submit” button.

3—Because the button is type=“button”

and is not submit, clicking on the button will NOT trigger a server post the way the previous slide did. Instead, we rely on the onClick() event to trigger the JavaScript function submitMyPage() which then submits the form.

Page 35: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps:

Communicate Between Java and

JavaScript

Page 36: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• Sometimes you need to get some action in JavaScript when your data is in Java.

• This is how that is accomplished. I will follow the sequence.

Web Apps: Communicate Between Java and JavaScript

Page 37: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

• The numbers will show the sequence of actions that occur.

Web Apps: Communicate Between Java and JavaScript

1—page is loaded—bean either instantiated or pulled from session.

Page 38: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps: Communicate Between Java and JavaScript

2—The value from this method is placed into this hidden variable.

Page 39: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps: Communicate Between Java and JavaScript

3—When the page has loaded, the body onload is executed. In this case, that means the function updateSuccessful() is executed.

Page 40: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Web Apps: Communicate Between Java and JavaScript

4—This JavaScript function is executed and it examines the value in that hidden variable—thus causing the popup to appear, in this example.

Page 41: Web Application Best Practies

Java II--Copyright © 2001-2007 Tom Hunter

Please send me any best practices you know and I will incorporate them into

this lecture.

[email protected]

Page 42: Web Application Best Practies

1 42