Bug Hunting

Shared by: dandanhuanghuang
Categories
Tags
-
Stats
views:
4
posted:
12/4/2011
language:
English
pages:
23
Document Sample
scope of work template
							          SQE – use of JSP vehicle
         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.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Files and Folders - locations

• Your .htm, .jsp and database files should be placed in:

• At University:
    //trentdev/wwwdatajsp/studentid/


• At Home:
    /tomcat/webapps/ROOT/studentid/




Version 2.3 Nov 2007    j.c.westlake@staffs.ac.uk
            SQE – use of JSP vehicle
           HTML Checklist
1. Ensure the server is installed correctly and has been
   started.
2. Are you using the correct URL (should start with http://)
3. Are your .htm 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 etc.)

   http://www.xlinesoft.com/asprunnerpro/articles/sql_access_odbc_reserved_keywords.htm


  Version 2.3 Nov 2007       j.c.westlake@staffs.ac.uk
            SQE – use of JSP vehicle
           JSP Checklist
• Remember that using GET you are restricted to 1024 chars.
• You should use POST but GET is good for debugging as you
  can see it in the url string in the browser
• Use the error page – see later




  Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
             SQE – use of JSP vehicle
            Database Problems
• Remember we are using DSN-less connections.
• Your connection object must have the following structure:
• At University:
    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft
      Access Driver (*.mdb)};DBQ=C:/Program Files/Apache Software
      Foundation/Tomcat 5.5/webapps/studentid/databasefilename");
• At Home:
    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft
      Access Driver
      (*.mdb)};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;");


   Version 2.3 Nov 2007     j.c.westlake@staffs.ac.uk
             SQE – use of JSP vehicle
            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 only put apostrophes around values that are string-based and your
  database field is expecting Text values. E.g.

    INSERT INTO MyTable (4157, ‘Yvan Cartwright’, ‘LC24’);
    Note the first field in the table is numeric

    INSERT INTO MyTable (‘4157’, ‘Yvan Cartwright’, ‘LC24’);

• Date values have a pair of # around them.
• 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

   Version 2.3 Nov 2007      j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Autosequence datatype
• The autosequence column name does not need to be
  named in your insert sql statement when writing to a
  database.




Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Exceptions
• Exceptions are by definition exceptional events that
  occur during program execution.
• Typical exceptional events are:
    –   Database server is down.
    –   File is locked by another user.
    –   Mathematical errors (division by zero etc.)
    –   And there are many others
• Useful for helping to track problems and also for your
  testing/quantitative part of your SQAP


Version 2.3 Nov 2007      j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Exception Handling
• Unfortunately, it is not usually possible to know in
  advance that an exception is about to occur.
• How do we tell our program what to do in case an
  exception does happen?
• Fortunately for object oriented coders, this problem
  has a generic solution.
• Since JSP is based on Java we can use this solution
  in our web applications.




Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         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)
  {
       // Exception handler code here
  }
  %>
Version 2.3 Nov 2007    j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         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.




Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Exception Handling
• Do nothing… let your program fall over and read the
  error message that Java produces on the server.
• You could handle the exception locally (i.e. in your
  code at the point where the exception occurred)
  within your catch block.
• Or, you could redirect the user to an error page and
  do something there.




Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
          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>




Version 2.3 Nov 2007       j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         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>




Version 2.3 Nov 2007    j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         But……..
• This code works fine until a user enters something
  other than an integer via the form.




Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Simple Fix - Local Try…Catch

<%
  int age;
  try {
        age = Integer.parseInt(request.getParameter("age"));
%>
<p>Your age is : <%= age %> years.</p>
<%
  }
  catch(NumberFormatException e) {
%>
<p>You must enter a number!</p>
<%
  }
%>
Version 2.3 Nov 2007    j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         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>


Version 2.3 Nov 2007    j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         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>
Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         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…




Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Combined Version
<%
  int age;
  try
  {
        age = Integer.parseInt(request.getParameter("age"));
  }
  catch (NumberFormatException e)
  {
        throw new JspException("Please enter a valid integer value!");
  }
%>



Version 2.3 Nov 2007    j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         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>




Version 2.3 Nov 2007    j.c.westlake@staffs.ac.uk
          SQE – use of JSP vehicle
         Recap
• Error investigation does improve with experience
• The usual distinction between run time and compile
  time occurs with beans
• 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




Version 2.3 Nov 2007   j.c.westlake@staffs.ac.uk

						
Related docs
Other docs by dandanhuanghuang
jowers
Views: 433  |  Downloads: 0
Tree Structured Index
Views: 1  |  Downloads: 0
32_sales_per_qtr_bv
Views: 1621  |  Downloads: 0
LATEST STAFF DETAILS
Views: 597  |  Downloads: 0
4grandparents
Views: 292  |  Downloads: 0
CommunicationsElectronicCommunicationsAnalyst
Views: 3  |  Downloads: 0
Lire un message SWIFT
Views: 332  |  Downloads: 0
David Cracknell EPC CIC
Views: 326  |  Downloads: 1