Your SlideShare is downloading. ×
0
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Web Application Best Practies
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Web Application Best Practies

90

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
90
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Java II--Copyright © 2001-2007 Tom Hunter
  • 2. Java II--Copyright © 2001-2007 Tom Hunter J2EE Web Application Best Practices
  • 3. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: Best Practices
  • 4. 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.
  • 5. 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
  • 6. 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
  • 7. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: HttpSession/sendRedirect() Vs RequestDispatcher/forward()
  • 8. 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
  • 9. 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)
  • 10. 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 your order twicesubmit your order twice if someone refreshes the page. Web Apps: HttpSession
  • 11. 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
  • 12. 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
  • 13. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: Should I Execute JDBC From Inside a JSP?
  • 14. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: Should I Execute JDBC From Inside a JSP?
  • 15. 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 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!) Web Apps: Should I Execute JDBC From Inside a JSP?
  • 16. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: Java static Constants On A JSP
  • 17. 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
  • 18. 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.
  • 19. 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.
  • 20. 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.
  • 21. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: Make an HTML Form Post to Two Actions
  • 22. 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.
  • 23. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: JSP Lets me Create Methods in JSPs
  • 24. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: JSP Lets me Create Methods in JSPs
  • 25. 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
  • 26. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: HTML Tables: width % vs. Pixels
  • 27. 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.
  • 28. 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.
  • 29. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: JavaScript Popup—How to do that
  • 30. 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.
  • 31. 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;
  • 32. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: Submitting a JSP using JavaScript
  • 33. 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 the textbecause the text boxes and theboxes and the button are inside ofbutton are inside of thethe form, the page is submitted.
  • 34. 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.
  • 35. Java II--Copyright © 2001-2007 Tom Hunter Web Apps: Communicate Between Java and JavaScript
  • 36. 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
  • 37. 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.
  • 38. 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.
  • 39. 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.
  • 40. 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.
  • 41. Java II--Copyright © 2001-2007 Tom Hunter Please send me any best practices you know and I will incorporate them into this lecture. curmudgeon99@gmail.com
  • 42. 1 42

×