Web App Best Practices

Download Report

Transcript Web App Best Practices

1
1
J2EE
Web Application Best
Practices
1
2
Web Apps:
Best Practices
1
3
Web Apps: Best Practices
• 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.
1
This lecture was made possible by the assistance of Mr. David Harvey of Salt Lake City,
UT.
4
Web Apps: Best Practices
• 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.
1
5
Web Apps: Best Practices
• 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?
1
6
Web Apps:
HttpSession/sendRedirect()
Vs
RequestDispatcher/forward()
1
7
Web Apps: HttpSession vs. RequestDispatcher
• 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.
1
8
Web Apps: HttpSession
• When you are using an HttpSession as your means of
maintaining state between requests, then you will likely use
As you see here, we get the
the method called a sendRedirect()
HttpSession object from
the HttpServletRequest
as a means to send your users to the
object. Then, our bean—
next page.
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
9
Web Apps: HttpSession
• 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 your order twice if someone
refreshes the page.
1
10
Web Apps: RequestDispatcher
• 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 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.
1
11
Web Apps: RequestDispatcher
• 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!
http://localhost:8100/educator/educatorSearch.jsp
1
12
Web Apps:
Should I Execute JDBC
From Inside a 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?
• 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 a
System.out.println() on the page.
4.) If you have multiple requests, it is possible to be
starting a second request before the first one has
completed—with bizarre effects on Connections.
5.) You will be embarrassed by that JDBC-in-JSP
code when you get more experience. (I was!)
1
15
Web Apps:
Java static
Constants On A JSP
1
16
Web Apps: Java static Constants On A JSP
• As you should now, it is always a bad idea to have hardcoded 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.
1
17
Web Apps: Java static Constants On A JSP
• First of all, you need to make the constants available.
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
18
Web Apps: Java static Constants On A JSP
• Next we need to make the constants visible on our JSP by
importing the class.
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
19
Web Apps: Java static Constants On A JSP
• Finally, we see how this is consumed in the Servlet using
the same constant.
This approach
prevents the
problem I
described a few
slides ago. You
will always find
your parameters
this way.
1
20
Web Apps:
Make an HTML Form
Post to Two Actions
1
21
Web Apps: Make an HTML Form Post to Two Actions
• Sometimes on a JSP page, you want to post to one Servlet
for validation, then to a different Servlet for the update.
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
22
Web Apps:
JSP Lets me Create
Methods in JSPs
1
23
Web Apps: JSP Lets me Create Methods in JSPs
1
24
Web Apps: JSP Lets me Create Methods in JSPs
• 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!
1
25
Web Apps:
HTML Tables:
width % vs. Pixels
1
26
Web Apps: HTML Tables: width % vs. Pixels
• As a web developer, you have few choices except to use
the HTML table to layout your page.
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
27
Web Apps: HTML Tables: width % vs. Pixels
• 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.
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
Web Apps:
JavaScript Popup—How
to do that
1
29
Web Apps: JavaScript Popup—How to do that
• Although users hate having to dismiss a popup using their
mouse—as they invariably have to do—you should know
how to make one.
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.
In this example, the
submit causes the
JavaScript function
doPopup() to be
executed.
1
Click here to see Microsoft’s API on this command.
30
Web Apps: JavaScript Popup—How to do that
• 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.)
String url = "http://" + serverName + ":" + serverPort + restOfLink;
1
31
Web Apps:
Submitting a JSP using
JavaScript
1
32
Web Apps: Submitting a JSP using JavaScript
• This is a pretty typical thing but it should be documented.
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 the text
boxes and the
button are inside of
the form, the page is
submitted.
1
33
Web Apps: Submitting a JSP using JavaScript
• This is a pretty typical thing but it should be documented.
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
34
Web Apps:
Communicate Between
Java and JavaScript
1
35
Web Apps: Communicate Between Java and JavaScript
• 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.
1
36
Web Apps: Communicate Between Java and JavaScript
• The numbers will show the sequence of actions that
occur.
1—page is loaded—
bean either
instantiated or
pulled from session.
1
37
Web Apps: Communicate Between Java and JavaScript
1
2—The
value from
this method
is placed into
this hidden
variable.
38
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
39
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
40
Please send me any best practices you know
and I will incorporate them into this lecture.
[email protected]
1
41
1
42