Tips and Techniques Tips re: errors Obviously, the best approach to reducing bugs is to code to a design and perform testing at incremental stages. However.

Slides:



Advertisements
Similar presentations
Creating Tables Unit 9 - Databases.
Advertisements

Creating a Login Process Creating a users table and a login form that denies access to unauthorized users.
CSCI 6962: Server-side Design and Programming Input Validation and Error Handling.
Yoshi
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
DT211/3 Internet Application Development JSP: Processing User input.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
Java, Access, SQL, HTML. Three-tier architecture involves: Client - Browser Server - Tomcat Database - Access - Server-side language - JSP could just.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems JavaServer Pages (JSP)
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
MIT AITI 2004 JSP – Lecture 2 Get and Post Requests.
1 Introduction to PHP. 2 What is this “PHP” thing? Official description: “PHP, which stands for "PHP: Hypertext Preprocessor" is a widely-used Open Source.
MIS 3200 – Unit 6.2 Learning Objectives How to move data between pages – Using Query Strings How to control errors on web pages – Using Try-catch.
INTERNET APPLICATION DEVELOPMENT For More visit:
Java for enterprise networks Version 2.3 Feb 2008 JSP Validation and Exception handling Why validate? Client side validation.
1 Chapter Eight Exception Handling. 2 Objectives Learn about exceptions and the Exception class How to purposely generate a SystemException Learn about.
CSCI 6962: Server-side Design and Programming Validation Tools in Java Server Faces.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PHP meets MySQL.
UNIT 9.2: Learning Objectives Agile Development – Bruce Feiler on Agile Programming Database access from code – Database Cycle Review – SQL Command Types.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Servlets Database Access. Agenda:  Setup Java Environment  Install Database  Install Database Drivers  Create Table and add records  Accessing a.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
How to Design Error Steady Code Ivaylo Bratoev Telerik Corporation
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
Introduction to Exception Handling and Defensive Programming.
JSP program that interacts with HTML form & Access Data Base.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Class 3Intro to Databases Class 4 Simple Example of a Database We’re going to build a simple example of a database, which will allow us to register users.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Chapter 8-Exception Handling/ Robust Programming.
Nic Shulver, Storing Data on the Server Introduction We are going to look at some working code It writes fixed data into a simple.
Introduction to MySQL Ullman Chapter 4. Introduction MySQL most popular open-source database application Is commonly used with PHP We will learn basics.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
: Information Retrieval อาจารย์ ธีภากรณ์ นฤมาณนลิณี
Introduction to Exceptions in Java CS201, SW Development Methods.
1 UNIT 13 The World Wide Web. Introduction 2 The World Wide Web: ▫ Commonly referred to as WWW or the Web. ▫ Is a service on the Internet. It consists.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
ASP.NET Programming with C# and SQL Server First Edition
Unit 16 – Database Systems
Using Access and the Web
Designing with Java Exception Handling
CMSC 202 Exceptions.
Presentation transcript:

Tips and Techniques Tips re: errors Obviously, the best approach to reducing bugs is to code to a design and perform testing at incremental stages. However when you don’t know what the problem is then… Whenever you come across an error in your JSP application, you need a mental checklist to help you track down the problem. Version 2.4 Nov 2008

Tips and Techniques Version 2.4 Nov 2008 Files and Folders - locations Your.htm,.jsp and database files should be placed in the following folder: At University: \\fcet11.staffs.ac.uk\studentID\ At Home: /tomcat/webapps/ROOT/myJSPfolder/

Tips and Techniques Version 2.4 Nov 2008 HTML Checklist 1.At home ensure the server is installed correctly and has been started. 1.At Uni the server should be running automatically but you always test by running 2.Are you using the correct URL? (should start with at uni or at home) 3.Are your.htm or.html files appearing ok? 4.Do any of your HTML forms contain reserved Java keywords as names (e.g. not String, int etc.) 5.Do any of your HTML forms contain reserved SQL keywords (e.g. name, description, password etc.)

Tips and Techniques Version 2.4 Nov 2008 JSP Checklist Remember that using GET you are restricted to (usually less than) 1024 chars. You should use POST – but GET is good for debugging as you can see it in the url string in the browser – that is the recommendation for your early use of JSP Use the error page – see later in module

Tips and Techniques Version 2.4 Nov 2008 Database Problems Remember we are using DSN-less connections – you can use a DSN at home if you wish to – available through the ODBC utility in control panel Your connection object must have the following structure: At University: Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=C:/Program Files/Apache Software Foundation/Tomcat 7.0/webapps/studentid/databasefilename"); However it is best to use the provided helper DBConnection.JSP which has the line: String sMyDatabase = getServletContext().getRealPath(sDatapath); to work out the path dynamically.

Tips and Techniques Version 2.4 Nov 2008 Database Problems At Home: Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=C:/tomcat/webapps/ROOT/studentid/databasefil ename;"); OR, you can use the IP address of your machine ( in this example) Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=// /databasefilename;");

Tips and Techniques Version 2.4 Nov 2008 Database Problems Be very careful with your SQL statements! Remember that you cannot put a record into a database that has the same primary key value as another record – you will get a nasty message –We can gracefully handle this eventually You only put apostrophes around values that are string-based and your database field is expecting Text values. E.g. INSERT INTO MyTable (4157, ‘Consummated Ferrets’, ‘B157’); Note the first field in the table is numeric INSERT INTO MyTable (‘4157’, ‘Consummated Ferrets’, ‘B157’); // error!

Tips and Techniques Version 2.4 Nov 2008 Database Problems Be very careful with your SQL statements! Date values have a pair of # around them, but avoid using DATE types. If you have a problem with currency fields, use number, set it’s field size to Decimal and set the format to Currency, then set the number of decimal places and scale to 2 – see next slide

Tips and Techniques Version 2.4 Nov 2008 Autosequence datatype The autosequence column name does not need to be named in your insert sql statement when writing to a database. We recommend you be careful if you use autosequence fields – they can be more trouble than they are worth –Can be nasty when get out of sequence –Your mileage may vary – some people do not experience many problems with autonumbering keyfields –For example, Nic Shulver always uses them, without issues

Tips and Techniques Version 2.4 Nov 2008 Try…Catch In Java (and JSP) we can use a try…catch block around any piece of code that may cause an exception. <% try { // Code which can throw can exception } catch(Exception e) // only catch it if the code fails above { // Exception handler code here } %>

Tips and Techniques Version 2.4 Nov 2008 Exceptions For very practical reasons, Java enforces the use of try…catch blocks around any piece of code that can cause an exception to be thrown. By ‘thrown’, it is meant that the exception has occurred. When an exception is thrown, one of several things can happen depending on what you want your web application to do at that point.

Tips and Techniques Version 2.4 Nov 2008 Exception Handling 1)Do nothing… let your program fall over and read the error message that Java produces on the server. 2)You could handle the exception locally (i.e. in your code at the point where the exception occurred) within your catch block. 3)Or, you could redirect the user to an error page and do something there.

Tips and Techniques Version 2.4 Nov 2008 Form.htm Enter your age ( in years ) :

Tips and Techniques Version 2.4 Nov 2008 FormHandler.jsp <% int age; age = Integer.parseInt(request.getParameter("age")); %> Your age is : years. Back.

Tips and Techniques Version 2.4 Nov 2008 But…….. This code works fine until a user enters something other than an integer via the form.

Tips and Techniques Version 2.4 Nov 2008 Simple Fix - Local Try…Catch <% int age; try {age = Integer.parseInt(request.getParameter("age")); out.print("Your age is : " + age + "years. "); } catch(NumberFormatException e) {out.print("You must enter a number! "); } %>

Tips and Techniques Version 2.4 Nov 2008 User-Defined Error Page <% int age; age = Integer.parseInt(request.getParameter("age")); %> Your age is : years. Back.

Tips and Techniques Version 2.4 Nov 2008 User-Defined Error Page <% out.println("<!--"); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); exception.printStackTrace(pw); out.print(sw); sw.close(); pw.close(); out.println("-->"); %>

Tips and Techniques Version 2.4 Nov 2008 Ok, Good, Better! This works well but we can do better! Currently, the error message that is displayed is a standard Java message. These can be difficult to understand so instead we’ll pass our own message to our error page for it to display…

Tips and Techniques Version 2.4 Nov 2008 Combined Version <% int age; try { age = Integer.parseInt(request.getParameter("age")); } catch (NumberFormatException e) { throw new JspException("Please enter a valid integer value!"); } %>

Tips and Techniques Version 2.4 Nov 2008 Combined Version This time we catch the NumberFormatException locally and throw a new JspException with our own exception message. JspException is a JSP special exception class which extends java.lang.Exception. We need to change the error page code to this:

Tips and Techniques Version 2.4 Nov 2008 Recap Error investigation does improve with experience JSP errors at run time and can be a combination of <% or } problems Validation to catch errors from say user input can be improved by the use of exception JSPs