42
1 1

1 1. 1 2 J2EE Web Application Best Practices 1 3 Web Apps: Best Practices

Embed Size (px)

Citation preview

1 1

1 2

J2EEWeb Application Best

Practices

1 3

Web Apps:

Best Practices

1 4

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

1 5

• 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

1 6

• 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

1 7

Web Apps:HttpSession/sendRedirect()

Vs

RequestDispatcher/forward()

1 8

• 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

1 9

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

1 10

• 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

1 11

• 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

1 12

• 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

1 13

Web Apps:

Should I Execute JDBC From Inside

a JSP?

1 14

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

1 15

• 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?

1 16

Web Apps:

Java static

Constants On A JSP

1 17

• 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

1 18

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

1 19

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

1 20

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

1 21

Web Apps:

Make an HTML Form Post to Two

Actions

1 22

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

1 23

Web Apps:

JSP Lets me Create Methods in JSPs

1 24

Web Apps: JSP Lets me Create Methods in JSPs

1 25

• 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

1 26

Web Apps:HTML Tables:

width % vs. Pixels

1 27

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

1 28

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

1 29

Web Apps:

JavaScript Popup—How to do that

1 30

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

1 31

• 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;

1 32

Web Apps:

Submitting a JSP using JavaScript

1 33

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

1 34

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

1 35

Web Apps:

Communicate Between Java and

JavaScript

1 36

• 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

1 37

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

1 38

Web Apps: Communicate Between Java and JavaScript

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

1 39

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.

1 40

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.

1 41

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

this lecture.

[email protected]

1 42