tips re: errors

23
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

Upload: gavin-johnson

Post on 04-Jan-2016

22 views

Category:

Documents


1 download

DESCRIPTION

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… - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Tips re: errors

Tips and TechniquesTips 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

[email protected]

Page 2: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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/

Page 3: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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)

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

Page 4: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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

Page 5: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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.

Page 6: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

Database Problems

• At Home:Connection conn =

DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb,*.accdb)};DBQ=C:/tomcat/webapps/ROOT/studentid/databasefilename;");

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;");

Page 7: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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!

Page 8: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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

Page 9: Tips re: errors
Page 10: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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

Page 11: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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}%>

Page 12: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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.

Page 13: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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.

Page 14: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

Form.htm

<html>

<head></head>

<body>

<form action="FormHandler.jsp" method="post">

Enter your age ( in years ) : <input type="text" name="age" />

<input type="submit" value="Submit" />

</form>

</body>

</html>

Page 15: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

FormHandler.jsp<html><head></head><body><%

int age;age = Integer.parseInt(request.getParameter("age"));

%><p>Your age is : <%= age %> years.</p><p><a href="Form.htm">Back</a>.</p></body></html>

Page 16: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

But……..

• This code works fine until a user enters something other than an integer via the form.

Page 17: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

Simple Fix - Local Try…Catch

<%int age;try

{ age = Integer.parseInt(request.getParameter("age"));out.print("Your age is : " + age + "years. <br>");

}catch(NumberFormatException e)

{ out.print("You must enter a number! <br>");}

%>

Page 18: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

User-Defined Error Page<%@ page errorPage="ExceptionHandler.jsp" %><html><head></head><body><%

int age;age = Integer.parseInt(request.getParameter("age"));

%><p>Your age is : <%=age %> years.</p><p><a href="Form.html">Back</a>.</p></body></html>

Page 19: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

User-Defined Error Page<%@ page isErrorPage="true" import="java.io.*" %> <html><head></head><body><font color="red"><%= exception.toString() %><br></font><%

out.println("<!--");StringWriter sw = new StringWriter();PrintWriter pw = new PrintWriter(sw); exception.printStackTrace(pw);out.print(sw);sw.close();pw.close();out.println("-->");

%></body></html>

Page 20: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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…

Page 21: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

Combined Version

<%int age;try{

age = Integer.parseInt(request.getParameter("age"));}catch (NumberFormatException e){

throw new JspException("Please enter a valid integer value!");}

%>

Page 22: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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:

<font color="red"><%= exception.getMessage() %><br></font>

Page 23: Tips re: errors

Tips and Techniques

Version 2.4 Nov 2008

[email protected]

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