1. Trang chủ
  2. » Công Nghệ Thông Tin

Teach Yourself E-Commerce Programming with ASP in 21 Days phần 7 pps

62 186 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 62
Dung lượng 1,15 MB

Nội dung

Now that you have installed Remote Machine Debugging and enabled debugging on your development site, you must give yourself permission to debug on the development server. You can do this as follows: 1. From the Start button, click Run. 2. In the small box that appears, type Dcomcnfg.exe and click OK. 3. On the Applications tab, find the Catalog class (see Figure 16.11) and click the Properties button. This will bring up a dialog called Catalog Class Properties. 356 Day 16 F IGURE 16.10 The App Debugging tab of Application Configuration. FIGURE 16.11 The Applications tab. 4. Click the Security tab (see Figure 16.12). In each of the three permissions sections, use the Edit button to bring up a dialog that will allow you to give yourself access permission for the resource. Click OK. 22 0672318989 ch16 3/30/00 8:13 AM Page 356 Debugging Your E-Commerce Applications 357 16 5. You will return to the Applications tab of the previous dialog. Find the Machine Debug Manager and repeat step 4. Debugging a Site You are now ready to debug the development Web site. Re-open Visual InterDev, and open the project you created in the previous section, “Deploying Your Application Using Visual InterDev.” Go to the Project Explorer, and select default.asp, and then from the menu select Project, Web Files, Set as Start Page. To try debugging out, double-click default.asp to open it. The file will open in the cen- ter area of the Visual InterDev IDE, and you will notice a gray strip running down the left side of the window. You can set a breakpoint by clicking the gray strip next to the line you’d like to stop on. To try out debugging, click to the left of the first executable line of the file, cat = TRIM ( Request( “cat” ) ). You will see a red circle appear next to the line as shown in Figure 16.13. (In the figure, the red circle is black.) Press the F5 button to start debugging, and you will be prompted for your Windows NT username and password. Enter it to continue. Internet Explorer will now launch, and the Visual InterDev IDE will change from editing view to debugging view. The line broken on will appear highlighted in yellow, and there will be a yellow arrow pointing to the highlighted line (see Figure 16.14). F IGURE 16.12 The Security tab. 22 0672318989 ch16 3/30/00 8:13 AM Page 357 You can now watch the .asp script execute line-by-line and, as the script executes, you can monitor the contents of VBScript variables in the watch window in the lower right side of the IDE. Try it now by clicking on the blank cell in the Name column of the watch window, entering cat and pressing return. You will observe Variable is 358 Day 16 F IGURE 16.13 A breakpoint set in default.asp. FIGURE 16.14 Stopped at a break- point set in default.asp. 22 0672318989 ch16 3/30/00 8:13 AM Page 358 Debugging Your E-Commerce Applications 359 16 undefined: ‘cat’ in the Value column, and Error in the Type column (see Figure 16.15). Press the F10 key once, and you will see several things happen: • The yellow arrow moves to the next line of VBScript IF cat = “” THEN cat = “Home”. • The yellow highlight moves to the next line of VBScript. • The Value column in the watch window changes to “”. • The Type column in the watch window changes to String. F IGURE 16.15 The watch window. According to the current line, because the value of the variable cat is “”, it should be set to “Home”. Try it by pressing F10 again. You will observe that the Value column in the watch window changes to “Home”. The color of the string is red to signify that the value changed as a result of executing the previous statement (see Figure 16.16). The watch window can also look inside COM objects that your ASP script instantiates. The line of ASP script currently highlighted creates an ADO Connection. Try looking into it by clicking the watch window on the empty cell in the Name column (underneath cat) and typing Con. Press F10 again, and you will see a plus sign inside a box next to the variable name Con. Click the plus sign to expand the object, and you will see the members of the new Connection object (see Figure 16.17). 22 0672318989 ch16 3/30/00 8:13 AM Page 359 The Visual InterDev IDE can also show you all the variables and objects defined for the current script. Go to the View menu and select Debug Windows, Locals. The Locals win- dow will appear on the lower left side of the IDE, and will include all the objects that ASP defines for your page (like Request, Response, Session, Application) as well as the variables defined in adovbs.inc and the variables defined in default.asp as of the 360 Day 16 F IGURE 16.16 The watch window changes as a result of the single statement executed. FIGURE 16.17 Examining an ADO Connection object in the watch window. 22 0672318989 ch16 3/30/00 8:13 AM Page 360 Debugging Your E-Commerce Applications 361 16 current line (see Figure 16.18). You can exit the debugger by going to the Debug menu and selecting End, or by pressing Shift+F5. F IGURE 16.18 The Locals window. As you can imagine, this is an extremely powerful way to examine potential errors in your ASP code. Many bugs result from unanticipated data or typing errors; these kinds of bugs are easy to diagnose and fix with the Visual InterDev IDE. Single-step debugging isn’t appropriate for diagnosing every problem; for example, bugs that relate to multiple users accessing your Web site simultaneously. This is because when the debugger is stopped at a breakpoint, the Web server isn’t able to respond to requests from other Web clients. To debug problems related to multiple requests, you will need to use techniques like those described in the next two sections. Debugging Your Application on a Production Server Although you might test your application thoroughly on your development site, a few bugs will almost always slip into your production system. The best way to diagnose these problems is to try to reproduce the bug on your development system while watching ASP execution in the Visual Studio debugger; however, as discussed in the previous section, this isn’t always possible. In those cases, you will need a way to have the production Web site output additional information to you while your customers continue to use the 22 0672318989 ch16 3/30/00 8:13 AM Page 361 Web site normally. The ASP Session object offers a straightforward way to do this by allowing you to set a per-session debugging level and adding a hidden administrative page to allow you to set the debugging level. You can then use that debugging variable in your code to decide how much debugging information to add to the output of your ASP. 362 Day 16 This technique takes advantage of ASP Sessions. Although Sessions have many advantages, keeping per-session information in an ASP-driven Web site will degrade performance when your site is under high levels of load. You will need to explore the specific performance characteristics of your own Web site to decide whether using this technique is worth the potential performance impact. Note Creating and Maintaining a Session Variable for Debugging To create the debugging variable, you will first need to create a global.asa file if it isn’t already created. To create an instance of the Global.asa file using Visual InterDev, go to the Project Explorer window and right-click your Web project. In the context menu that appears, select Add, Active Server Page. Change the name of the file in the Add Item page from ASPPage1.asp to global.asa. The new global.asa file will open in a new win- dow in the center of the IDE. Delete all the text from the window and add the lines of code in Listing 16.1. If you aren’t using the Visual InterDev IDE, you can simply create the global.asa file in Notepad or another text editor. LISTING 16.1 Setting iDebugLevel to 0 for Each Session 1 <SCRIPT LANGUAGE=”VBScript” RUNAT=”Server”> 2 3 Sub Session_OnStart 4 Session(“iDebugLevel”) = 0 5 End Sub 6 </SCRIPT> Whenever a new user comes to your Web site, line 4 of this script initializes a session variable called iDebugLevel and sets it to 0. In the future, you will use values of iDebugLevel that are greater than 0 as a signal to emit additional information on your Web pages. INPUT ANALYSIS 22 0672318989 ch16 3/30/00 8:13 AM Page 362 Debugging Your E-Commerce Applications 363 16 You will need to add a hidden, administrative page that allows you to reset the value of iDebugLevel. For additional flexibility, we will do this by adding a subfolder called Admin, and a page in the Admin folder called adminPage.asp. You can do this in the Visual InterDev IDE by going to the Project Explorer and right-clicking your Web pro- ject. In the context menu that appears, select New Folder. In the dialog that appears, enter Admin. A new Admin folder will appear in the Project Explorer. Right-click that folder and select Add, Active Server Page. Change the name of the file in the Add Item page from ASPPage1.asp to adminPage.asp. A template for the new page will appear in the center of the IDE. If you aren’t using the Visual InterDev IDE, you can create the folder and adminPage.asp file using Notepad or another text editor. In either case, the ultimate contents of adminPage.asp are in Listing 16.2. Although there is not a link to adminPage.asp on your Web site, you can access it directly by going to http:// www.yoursite.com/Admin/adminPage.asp, changing the debug level, and clicking Submit. LISTING 16.2 Resetting iDebugLevel in adminPage.asp 1 <%@ Language=”VBScript” %> 2 <% 3 If Request.Form(“ProcForm”) = “Process” Then 4 Session(“iDebugLevel”) = Request.Form(“debugLevel”) 5 If Session(“iDebugLevel”) = “” Then 6 Session(“iDebugLevel”) = 0 7 End If 8 End If 9 %> 10 <HTML> 11 <HEAD> 12 <META NAME=”GENERATOR” Content=”Microsoft Visual Studio 6.0”> 13 <title>Johnson’s Candies and Gifts Administration Page</title> 14 </head> 15 <body link=”#ff4040” vtext=”lightred” bgcolor=”#ffffff”> 16 <center> 17 18 <table width=”640” border=”0” bgcolor=”#ffffff” cellspacing=”0” ➥ cellpadding=”0”> 19 <tr> 20 <td> 21 <img src=”logo.gif” WIDTH=”300” HEIGHT=”30”> 22 </td> 23 </tr> 24 <tr> 25 <td colspan=”2”> INPUT continues 22 0672318989 ch16 3/30/00 8:13 AM Page 363 26 <hr width=”640”> 27 </td> 28 </tr> 29 </table> 30 31 <H2>Administration Page<H2> 32 <FORM method=”POST” action=”adminPage.asp”> 33 <input type=”hidden” name=”ProcForm” value=”Process”> 34 <table> 35 <tr> 36 <td> Debug Level </td> 37 <td> <input name=”debugLevel” value=”<%=Session.Value ➥ (“iDebugLevel”)%>”></td> 38 </tr><tr> 39 <td> <input type=”submit” value=”Submit”> </td> 40 </tr> 41 </table> 42 </FORM> 43 </BODY> 44 </HTML> When the page is first executed, the value of the hidden variable ProcForm is not defined, so the page ignores the script between lines 4 and 7. On line 37, the cur- rent value of iDebugLevel is placed into an input textbox. When the user clicks the Submit button, the hidden variable ProcForm has been defined as “Process” on line 33, so the script between lines 4 and 7 sets the value of the session variable iDebugLevel to whatever the user entered. The form is now regenerated with the new value of iDebugLevel placed into the input textbox (line 37). Using the Session-level Debugging Variable A second axiom of programming is that errors most frequently occur when two different pieces of software interact. As you’ve seen, in building an E-Commerce site, the external software interacted with the most is the database, which is accessed via SQL and ADO. Using the iDebugLevel variable defined previously, you can now add code that displays extra debugging information in your Web pages—an especially useful technique for pro- duction debugging when your ASP is interacting with the database. For example, you might want to display SQL statements before they are executed if iDebugLevel is 1, and then display both SQL statements and their results if iDebugLevel is 2, and so on. In these examples, we will only take advantage of setting iDebugLevel to 1. To show the power of this technique, we will take, as an example, a page we created on Day 5, “Building Your Product Catalog,” called updateProducts.asp, and add 364 Day 16 LISTING 16.2 continued ANALYSIS 22 0672318989 ch16 3/30/00 8:13 AM Page 364 Debugging Your E-Commerce Applications 365 16 session-level debugging output to that script to allow us to diagnose our SQL queries. (This file is included with the CD-ROM that accompanies this book with the name debugUpdateProducts.asp.) First, we’ll add a subroutine to the top of the page that will capture the logic of deciding whether to display extra information. We’ll insert the code in Listing 16.3 between lines 1 and 2 of updateProducts.asp so that the routine is available to the entire script. LISTING 16.3 Adding a Debug Routine to updateProducts.asp 1.1 Sub DebugWrite(sDebugText, iDebugLevel) 1.2 If CInt(Session(“iDebugLevel”)) >= CInt(iDebugLevel) Then 1.3 Response.Write(vbNewLine & “<COMMENT>” & vbNewLine & ➥ ”Debugging output level “ & iDebugLevel & vbNewLine) 1.4 Response.Write(sDebugText) 1.5 Response.Write(vbNewLine & “</COMMENT>” & vbNewLine) 1.6 End If 1.7 End Sub When the DebugWrite subroutine is called, if the current value of the session debugging level is greater than or equal to the value passed in as iDebugLevel (line 1.1), the information passed in as sDebugText is emitted in an HTML comment (lines 1.3–1.5). Now that the DebugWrite subroutine is available, we can place it in strategic places in updateProducts.asp. As we’re concerned about our SQL statements, we will insert code that emits the SQL statements when iDebugLevel is 1. The code we add is in Listing 16.4. LISTING 16.4 Adding Strategic DebugWrite Calls to updateProducts.asp 6 Set Con = Server.CreateObject( “ADODB.Connection” ) 6.1 DebugWrite “Opening accessDSN”, 1 7 Con.Open “accessDSN” 13 RS.CursorType = 3 13.1 selectStr = “SELECT * FROM Products WHERE product_id=” & productID 13.2 DebugWrite selectStr, 1 14 RS.Open selectStr Now, go to admin/adminPage.asp and change the Debug Level to 1. Next, go to manageProducts.asp, and click one of the product links to bring you to an updateProduct.asp generated page. Although the output of the page looks the same in the browser, if you select View, Source, you will observe that the first four lines of HTML INPUT ANALYSIS INPUT 22 0672318989 ch16 3/30/00 8:13 AM Page 365 [...]... about using the FileSystemObject, see Day 4, “Working with Files in Your E-Commerce Application.”) You can write the errors by making the changes in Listing 16 .7 to debug .asp INPUT LISTING 16 .7 Changes to debug .asp to Capture Errors into a File 1 Sub WriteToFileAndHtml(fFileStream, sString) 2 Response.Write sString 3 If Not IsEmpty(fFileStream) Then 4 fFileStream.Write sString 5 End If 6 End Sub 7 8 Sub... the same Windows domain as your Web server You might find yourself in a situation in which you don’t have access to such a machine but need to administer your site The IIS Internet Service Manager Web pages give you the ability to perform many site administration tasks from any machine with Internet access and a Web browser Installing the Administration Pages If you are using Windows 2000, the Internet... output detailed information about each of them, if available, in an HTML comment To demonstrate CheckError in action, we will introduce a common error into We’ll change the manageProducts .asp as shown in Listing 16.6 so that debug .asp is included and product names with single quotes are no longer escaped manageProducts .asp Debugging Your E-Commerce Applications INPUT 1 2 3 4 5 6 7 8 LISTING 16.6 369... Web-Based Product Catalog Maintenance In Chapter 5, we introduced the addproduct .asp, updateProduct .asp, and manageproducts .asp pages, which allow you to add, update, and manage the products in your catalog In this section, we will integrate these pages into the administration pages created in Day 16, “Debugging Your E-Commerce Applications,” making adminPage .asp the home page of an administrative Web site... beside Internet Service Manager (HTML) (see Figure 17. 1) and press OK Press the Next button The setup program will then install the additional components and might prompt you to reboot your computer Do so FIGURE 17. 1 Installing IIS subcomponents Administering Your Store Remotely with ASPs 379 5 Re-install the latest Windows NT Service Pack The Internet Service Manager (HTML) is now installed into a... page (see Figure 17. 5) 6 Change the IP Address Access Restrictions to Grant Access to All Computers by default (see Figure 17. 6) and click the OK link Caution At this point, anyone with access to the Internet can administer your Web site by visiting http://www.yoursite.com/iisadmin So don’t stop here Continue through steps 7 10 Administering Your Store Remotely with ASPs 381 FIGURE 17. 4 The WWW Properties... also might run on a wide variety of machines 388 Day 17 Windows 95, Windows 98, Windows NT, and Windows 2000 come with a simple, textoriented FTP client Figure 17. 12 illustrates the steps involved in using the simple Windows FTP client to transfer a file to the Web site: FIGURE 17. 12 A transcript of uploading a file to a Web server 1 Open an MS-DOS (Windows 95 or Windows 98) or Command Prompt 2 Change... file is included on the CD-ROM that accompanies this book with the name debug .asp. ) Keeping all the debugging routines in a single file (in this site, we will call it debug .asp) makes the routines much easier to use and maintain Rather than having to copy and paste the same functions into multiple files, it will be much easier to INCLUDE=”/debug .asp at the top of each page This technique also gives each... you will find the Internet Service Manager (HTML) a useful tool for performing emergency maintenance on your E-Commerce site Administering Your Store Remotely with ASPs 385 Installing and Administering the IIS FTP Service One of the more common remote tasks you might need to perform on your E-Commerce site will be to add and remove content—for example, directories, Web pages, and images Using the File... called IISADMIN It is ready to use, but only from a browser running on the server on which it was installed Navigate to http://localhost/IISADMIN to explore its features (see Figure 17. 2) FIGURE 17. 2 The home page of the Internet Service Manager (HTML) 17 Securing the Administration Pages At this point, if you try to access the Internet Service Manager (HTML) with a browser running on a machine other . book with the name debug .asp. ) Keeping all the debug- ging routines in a single file (in this site, we will call it debug .asp) makes the routines much easier to use and maintain. Rather than having. about using the FileSystemObject, see Day 4, “Working with Files in Your E-Commerce Application.”) You can write the errors by making the changes in Listing 16 .7 to debug .asp. LISTING 16 .7 Changes. access it directly by going to http:// www.yoursite.com/Admin/adminPage .asp, changing the debug level, and clicking Submit. LISTING 16.2 Resetting iDebugLevel in adminPage .asp 1 <%@ Language=”VBScript”

Ngày đăng: 13/08/2014, 08:21

TỪ KHÓA LIÊN QUAN