tips re: errors
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 PresentationTRANSCRIPT
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
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 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
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/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;");
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
<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>
Tips and Techniques
Version 2.4 Nov 2008
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>
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. <br>");
}catch(NumberFormatException e)
{ out.print("You must enter a number! <br>");}
%>
Tips and Techniques
Version 2.4 Nov 2008
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>
Tips and Techniques
Version 2.4 Nov 2008
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>
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:
<font color="red"><%= exception.getMessage() %><br></font>
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