Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 j.c.westlake@staffs.ac.uk

2 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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/

3 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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 http://fcet11:8080/index.jsp 2.Are you using the correct URL? (should start with http://fcet11:8080 at uni or http://localhost:8080 at home)http://fcet11:8080http://localhost:8080 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.) http://www.xlinesoft.com/asprunnerpro/articles/sql_access_odbc_reserved_keywords.htm

4 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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

5 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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.

6 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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 (127.0.1.2 in this example) Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=//127.0.1.2/databasefilename;");

7 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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!

8 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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

9

10 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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

11 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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 } %>

12 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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.

13 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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.

14 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk Form.htm Enter your age ( in years ) :

15 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk FormHandler.jsp <% int age; age = Integer.parseInt(request.getParameter("age")); %> Your age is : years. Back.

16 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk But…….. This code works fine until a user enters something other than an integer via the form.

17 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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! "); } %>

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

19 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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("-->"); %>

20 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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…

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

22 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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:

23 Tips and Techniques Version 2.4 Nov 2008 j.c.westlake@staffs.ac.uk 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


Download ppt "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."

Similar presentations


Ads by Google