Response.Redirect Implement page redirection

Source: Internet
Author: User
Tags error code execution functions iis implement include relative urlencode
The redirect|response| page uses the Redirect method to redirect the browser to another URL instead of sending the content to the user. For example, if you want to verify that users have entered your application from the home page so that they can receive a customer ID, you can verify that they have a customer ID number, and if not, you can redirect it to the home page. Here's a concrete example:
<%if session ("CustomerID") = 0 Then Response.Redirect ' homepage.asp ' End if%>

Unless the buffer is open, you must redirect the browser before any content or title is returned to the browser. Placing the Response.Redirect statement before the top of the page and the tag ensures that nothing is returned to the browser. If you use Response.Redirect after returning to the content or title of the browser, you will see an error message.

If you use Response.Redirect in the middle of a page, use it with the Response.Buffer property because, by default, the WEB server returns HTML and the script processing results when processing ASP pages. However, you can set the Response object's Buffer property to process All server script commands on the page before sending any content to the user.

You can use buffering techniques to determine a point in the page process, and you do not want to send the content before that point to the user. You can also redirect the user to another page using the Redirect method of the Response object, or clear the buffer with the Response object's clean method and send different content to the user. The following examples use both of these methods.

Example one:

<% ' Next program must be placed in the program before the <HTML> identifier Response.Buffer = True%> Example two:
<%if Request ("FName") = "Then response.clear Response.Redirect". /test.html "Else Response.Write Request (" FName ") End if%> </body>

The above is the function that the response object often uses in the program.

   the server object has those attribute methods and how to use the

The server object provides methods and properties that are accessible to servers. Most of these methods and properties are provided as functionality of the utility.
What are the basic properties and methods of the server object?

(1). Grammar:
Server.property|method
(2). Attribute (property)
The server object has only one property: The maximum time a scripttimeout program can run
(3). Method (Methods)
CreateObject establishes an object instance.
Execute executes an ASP file
GetLastError returns an error code
HTMLEncode converts the specified HTML code.
MapPath converts a relative path to an absolute path.
Transfer sends all current status information to another ASP file
UrlEncode converts the specified code, including spaces, as a URL

   How do I use the properties and methods of the server object?

(1). CreateObject

    Grammar
Server.CreateObject (ProgID)
    Parameters
ProgID
Specifies the name of the component to be created, in the following format: [Vendor.] component[. Version].
    Main points:
In general, the lifetime of objects created with the Server.CreateObject method is within the scope of the page. This means that when this page's ASP program is finished, the object automatically disappears.

In order to create an object that has a session or application range, you will use the CreateObject method in the Global.asa file

(2). Execute

The Execute method calls an ASP file and executes it as if the ASP file being called is present in the ASP file. This is similar to the invocation of classes in many languages.
   Grammar
Server.Execute (Path)
   Parameters
Path
Specifies the path to the ASP file that will be executed. If it is a relative path, then it must be a file in the same place (directory) as this ASP application.

   explain
The Server.Execute method provides a way to differentiate a complex ASP application into a small unit. In this way, you can build an ASP library, and you can call the ASP files in your library whenever you need them.

When IIS finishes executing the ASP file based on the specified ASP file path, the previous ASP file is automatically returned. The ASP file that has just been executed may have changed the HTTP head. However, as with other ASP files, when the program tries to change the HTTP head, it will be an error!

This path parameter can include a query information.

If you have the same child functions in the ASP files that are called and called, these child functions only work in this ASP file. For example, if you have a child function that discards a program in the following ASP1 and ASP2 two files. First ASP1 call ASP2, then the OnTransactionAbort in ASP2 begins execution, and ASP2 in ASP1 begins execution when OnTransactionAbort executes.

ASP1:
<%@ Transaction = Required%><% server.execute ("page22.asp") sub OnTransactionAbort sub ontransactioncommit%>
Asp2.asp:
%@ Transaction = Required Sub OnTransactionAbort Sub ontransactioncommit%>
Example:
ASP1
<% Response.Write ("I am going to execute ASP2") Server.Execute ("/myasps/asp2.asp")%>
ASP2
<% Response.Write ("Here I am")%>

(3). GetLastError method

The GetLastError method returns a ASPError Object to describe an error message. This method only works before the ASP file sends any content to the user machine.
  Grammar
Server.GetLastError ()
  Points:
If a 500;100 user error has been defined in an ASP application, it refers to a file with an. asp suffix. In this case, when an error occurs while the program is running, the server is automatically routed to the executing ASP page in Server.Transfer. The ASP application will effectively handle this error. In addition, this ASPError object must be valid so that you can see the error message provided by the server to change the file!

The general web Site is constructed from file \iishelp\common\500-100.asp. You can use it to execute an ASP error, of course you can define it yourself!. If you want to change to another ASP file to perform these user errors. Then you can use the snap-in in IIS.

Note: When IIS discovers an ASP file or an error in the Global.asa file, a 500;100 user error occurs. The following programs will not execute!

The following three examples demonstrate a user error that can result from a different error. Three errors are:

Compile error
Run error
Logical error

The first example demonstrates a compilation error that occurs when IIS attempts to include a file. This error occurs because the required parameters are not defined in this include file. The second example shows a run-time error, which is interrupted because there is no "next" in the program. The third example shows a logical error because the program attempts to divide by a 0.

Example 01:

% Response.Write "Hello"%>
Example 02:
% Dim I For i = 1 to 1 nxt%>
Example 03:
<% Dim i, j Dim sum = 0 J = 0 for i = 1 to sum = sum+1 next sum = sum/j%>

(4). HTMLEncode method:

The HTMLEncode method HTML encodes the specified string.
  Grammar
Server.HTMLEncode (String)
  Parameters
String the character to encode
  Example
The following program:
<%= Server.HTMLEncode ("The paragraph tag:")%>
The output is:
The paragraph tag:
Notice what you see in the browser after the program is executed:
The paragraph tag:

But if you look at it with a "view source," The source code is not.

(5). MapPath

MapPath method converts a relative path to a physical path on the server
  Grammar
Server.MapPath (Path)
  Parameters
Path

This path is a path that begins with "/" or "\" and if there is no "\" in the path, the MapPath method returns the path based on the current directory.

  Explain
The MapPath method cannot check whether the path exists under this server. This is because MapPath transforms the path regardless of whether the path exists under this server.

You can use it to convert a relative path into a physical path, and then perform various operations on that path.

In the following example, the Data.txt file exists in the C:\Inetpub\Wwwroot\Script directory, and a test.asp file includes the following code. C:\Inetpub\Wwwroot is the home directory for this server.

In the following example, the physical path to the current file is obtained first with the environment variable "path_info".

Here is the script code:

<%= Server.MapPath (Request.ServerVariables ("Path_info"))%>
Display as:
C:\inetpub\wwwroot\script\test.asp
Because the path parameter in the following example does not begin with "/", it is converted to the current directory, and the ASP file is placed in the C:\Inetpub\Wwwroot\Script. The following is the scripts:
<%= Server.MapPath ("Data.txt")%> <%= Server.MapPath ("Script/data.txt")%>
Display as:
C:\inetpub\wwwroot\script\data.txt C:\inetpub\wwwroot\script\script\data.txt
The following two examples start with "\". Here's what scripts:
<%= Server.MapPath ("\script")%>
Display as:
C:\inetpub\wwwroot\script\data.txt C:\inetpub\wwwroot\script
Use the "/" or "\" directly to get the server's home directory:
<%= Server.MapPath ("\")%>
Display as:
C:\inetpub\wwwroot

(6). Transfer method:

The transfer method passes all the information of an executing ASP file to another person ASP file.
  Grammar
Server.Transfer (PATH)
  Parameters
Path
The location of the ASP file where the information will be received.
  Points:
When you call Server.Transfer, the state information of all the built-in objects is included in this transmission. This means that all information stored in the session or application will be transmitted, and all the information currently requested will be accepted by the ASP file that receives the information.

The following example demonstrates the transfer from one ASP file to another ASP file example!

ASP1
<% Dim sessvar1 Response.Write Session.SessionID Response.Write ("") Response.Write ("I am going to ASP2") server.tr Ansfer ("/myasps/asp2.asp")%>
ASP2
<% Response.Write Session.SessionID%>

(7). UrlEncode Method:

The UrlEncode method can URL-encode the specified string.
  Grammar
Server.URLEncode (String)
  Parameters
String specifying the strings to convert
Here's the code:
<% Respones.write (Server.URLEncode (http://www.microsoft.com))%>
Display as:
Http%3a%2f%2fwww%2emicrosoft%2ecom

(8). Properties: ScriptTimeout

The ScriptTimeout property sets the maximum elapsed time for a program.
  Grammar
Server.ScriptTimeout = Numseconds
  Parameters
Numseconds
The maximum elapsed time (in seconds) of the program is specified. The default value is 90 seconds.
Remarks

The value of the default SCRITPT timeout on the Web Sertvic or Web server can be set by the AspScriptTimeout property. In a program, the value of the ScriptTimeout property cannot be less than this default value. For example, if numseconds we set to 10 seconds and the default is 90 seconds, then the program will be aborted after 90 seconds instead of 10 seconds. Similarly, if we set the value of ScriptTimeout to 100 seconds, then the program will abort after 100 seconds instead of 90 seconds.

In the following example, the program will be set to be automatically aborted after 100 seconds.

<% server.scripttimeout = 100%>
The following example will get the ScriptTimeout value back, and then put it in the timout variable
<% TimeOut = Server.ScriptTimeout%>

  Summarize:

Through the combination of examples above, there is a more specific understanding of how server objects are used. But to use it flexibly, only keep practicing.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.