Præsentation er lastning. Vent venligst

Præsentation er lastning. Vent venligst

Internetteknologi (ITNET2) Præsentation 20: ASP.NET Introduktion ”Med Active Server Pages (ASP)”

Lignende præsentationer


Præsentationer af emnet: "Internetteknologi (ITNET2) Præsentation 20: ASP.NET Introduktion ”Med Active Server Pages (ASP)”"— Præsentationens transcript:

1 Internetteknologi (ITNET2) Præsentation 20: ASP.NET Introduktion ”Med Active Server Pages (ASP)”

2 Ingeniørhøjskolen i Århus Slide 2 af 50 Mission Efter denne (3)x35 lektion vil I: –Være introduceret ASP.NET teknologien med et historisk flash back til til ASP teknologien –Forstå ligheder og forskelle med J2EE JSP/Servlets –I vil ikke være ASP.NET eksperter, men i stand til at komme i gang med at bruge ASP.NET –I vil have en ide om hvornår man bruger den ene frem for den anden teknologi

3 Ingeniørhøjskolen i Århus Slide 3 af 50 Active Server Pages kort historie ASP født i november 1996 af Microsoft –RENT Microsoft barn – på godt og ondt –Før var det kun muligt at skabe liv på hjemmesider ved brug af CGI scripts (Perl, Python, C) –Sidste version ASP 3 (der kommer ikke flere) SUN har stjålet kraftigt fra ASP da de byggede JSP ovenpå Servlet teknologien –Heldigt for os … Fremtiden: ASP.NET – væk m. scripting ind m. prog. –Microsoft har lånt kraftigt fra JSP/Servlet teknologien –Seneste version ASP.NET 2 med hhv. udvidelse 3.0 og 3.5

4 Ingeniørhøjskolen i Århus Slide 4 Server side: JSP eksempel Feedback Form skrevet i JSP RESULTAT DER SENDES VIA HTTP: Feedback Form skrevet i JSP Vi har nu registreret dig som kunde Stefan Dette er JSP kode, der ligger, og afvikles på serveren Dette er hvad der sendes retur til klienten – der renderes altså på serveren – OG på klienten

5 Ingeniørhøjskolen i Århus Slide 5 af 57 Server side Active Server Page (name.asp) 1 2 3 <% 4 ' Fig. 25.5 : name.asp 5 ' Another simple ASP example 6 Option Explicit 7 %> 8 9 11 12 13 14 15 Name Information 16 17 18 p { font-family: arial, sans-serif; 19 font-size: 14pt; color: navy } 20.special { font-size: 20pt; color: green } 21 22 23 24 25 26 27 Hi, 28 Welcome to ASP! 29 30 31 32 Vi ser at ligheden KAN være slående de to teknologier imellem

6 Ingeniørhøjskolen i Århus Slide 6 af 50 Flere gode nyheder

7 Ingeniørhøjskolen i Århus Slide 7 af 50 Sådan virker Active Server Pages (MS ASP!!!) Ligesom JSP (Java Server Pages) opbygges sider dynamisk server side Processeres af scripting engine –Server-side ActiveX control parser (asp.dll).asp file extension – vs.jsp Programmers (eller scriptes) med VBScript –JavaScript kan også bruges – men ses ikke ofte –Øvrige (Independent Software Vendors)

8 Ingeniørhøjskolen i Århus Slide 8 af 50 Kommunikation med Server Client HTTP request til server (som JSP) Active server page processere HTTP request og returnerer resultaterne Kode oversættes/fortolkes run time – dvs ingen kompilering VBScript og JavaScript er derfor også typeløse sprog …

9 Ingeniørhøjskolen i Århus Slide 9 af 50 Hvad skal der til for at køre ASP? Ved JSP og Servlets skulle vi bruge en ”Servlet Container” med egen VM –Apache Tomcat, BEA Weblogic, Oracle AS, IBM WebSphere – alle standardiseret ASP skal også bruge et miljø –Nemlig MS Windows og en Web server: IIS (Internet Information Services) –Der følger med (næsten) alle Windows 2000, XP og Vista installationer PWS 4.0 (Personal Web Server 4.0) (“Det her er snart historie”)

10 Ingeniørhøjskolen i Århus Slide 10 af 50 Active Server Page støtte funktioner Som i JSP er det vigtigste i ASP evnen til at understøtte: –Kommunikation med en browser –Opsamle data sendt via HTTP request –Overvinde HTTPs mangel på state -> kende forskel på brugere JSP har en række implicitte objekter til dette, request, response, page, application, session

11 Ingeniørhøjskolen i Århus Slide 11 af 50 Indbyggede objekter til at understøtte dette i ASP –Request (i JSP: request) Indpakker ligesom JSP HTTP requestet Parameter leveret fra en XHTML form (el. Andet) Adgang til information gemt på en klient maskine –Cookies –Fil overførsler (binært) –Response (i JSP: response) Sende information retur til klienten –XHTML, text, Applets osv. –Session, Application (JSP: session, application) Ser vi nærmere på senere –Server (JSP: bl.a. pageContext) Adgang til Webserverens metoder og egenskaber

12 Ingeniørhøjskolen i Århus Slide 12 af 57 Name.html Sender data via HTTP post method. 1 3 4 5 6 7 8 9 10 Name Request 11 12 13 14 15 16 Enter your name: 17 18 19 20 21 22 24 25 26 27 28 action sættes til ASP fil hvor til parametrene bliver HTTP post ed

13 Ingeniørhøjskolen i Århus Slide 13 af 57 Name.asp Brug Request metoden til at hente data ud af HTTP headeren. 1 2 3 <% 4 ' Fig. 25.5 : name.asp 5 ' Another simple ASP example 6 Option Explicit 7 %> 8 9 11 12 13 14 15 Name Information 16 17 18 p { font-family: arial, sans-serif; 19 font-size: 14pt; color: navy } 20.special { font-size: 20pt; color: green } 21 22 23 24 25 26 27 Hi, 28 Welcome to ASP! 29 30 31 32 Request henter data fra textfield elementet “ namebox ” LIGHEDER MED JSP?

14 Ingeniørhøjskolen i Århus Slide 14 Problemer med ASP ikke objekt orienteret ikke altid lige let at strukturere performer ikke specielt godt JavaScript og VBScript er ikke drømmesprog til at kode store løsninger med

15 Ingeniørhøjskolen i Århus Slide 15 ASP.NET – afløser til ASP

16 Ingeniørhøjskolen i Århus Slide 16 Web service enabling Windows You need to: –Install.NET Framework SDK 2.0 + 3.0 og 3.5 Download from Microsoft (Install 2.0 and update it) 3.5 Comes standard with Visual Studio 2008. –Install Internet Information Server Enabled via the Control Panel Remember to shut down after using!!! –Install Visual Studio 2008

17 Ingeniørhøjskolen i Århus Slide 17 Not a Windows.NET course You will NOT be required to be an expert on.NET You will be required to be knowledgeable about it History: –Open source, Java, Linux the WWW threatened MS –Especially Java was gaining widespread support amongst developers for its ease of use, productivity and heterogeneity –The Empire Strikes back = the.NET Framework –Windows Winform has two succesors Windows Presentation Foundation and ASP.NET

18 Ingeniørhøjskolen i Århus Slide 18 Benefits of.NET Changes EVERYTHING –Takes all that was nice in Java and enhances it –End of DLL incompatibilities, DLL Hell, COM registry hell (GAC’en) –Enter world of VM’s and Java like code (C#) including Garbage collection – BUT with easy support for legacy unmanaged code (e.g. C++ for speed) and COM –Performs much better than Java, and almost equivalent with Win32 C++ applications –Still only support Microsoft operating systems BUT –.NET and C# is ECMA standard: Mono (www.go-mono.org) for Linux is being developedwww.go-mono.org

19 Ingeniørhøjskolen i Århus Slide 19 The.NET Framework Components Win32 MessageQueuingCOM+ (Transactions, Partitions, Object Pooling) IISWMI Common Language Runtime.NET Framework Class Library ADO.NET and XML XML Web Services User Interface Visual Basic C++C#PerlJScript… Application services. E.g. IIS, Message Queuing etc. CLR:runtime execution environment. Equlas JVM of Java. Microsoft Intermediate Language (MSIL) equals Java ByteCode A collection of class libraries (over 9000) wraps Win32 like Java API ActiveX Data Objects 3 types of user interfaces: Web forms, Windows forms (Win32), Command Console For inter-process communications Any language conforming to the Common Language Specification (CLS) may be used. Source code Source code Compiler Intermediate Language Intermediate Language.NET Framework.NET Framework Other IL libraries Other IL libraries Linker Linked Program Linked Program JIT-compiler Executable Program Executable Program JIT-compiler only runs first time!

20 Ingeniørhøjskolen i Århus Slide 20 Multiple Language Support The.NET Framework is designed to support many languages –More than 25 languages currently supported –Microsoft provides Visual Basic.NET, C#, Visual J#.NET, and JScript.NET (backwards compatible with JScript) – others: Delphi (object pascal) Benefits of multiple-language support –Code modules are reusable –API access is the same for all languages –The right language is used for the right task –Performance is roughly equal between all languages (which was not the case in ASP)

21 Ingeniørhøjskolen i Århus Slide 21 The Common Language Runtime One runtime for all. NET-Based Languages Manages threads and memory –Garbage collection Enforces code security Eliminates DLL versioning problems –Multiple versions of a DLL can run simultaneously –Applications can specify a version of a DLL to use Reminds one of the Java Virtual Machine

22 Ingeniørhøjskolen i Århus Slide 22 Runtime Compilation and Execution Native code C# code Visual Basic.NET code Which language? A Basic.NET compiler C# compiler MSIL JIT compiler Web form (default.aspx) or Web service (default.asmx) Runtime HTML / SOAP

23 Ingeniørhøjskolen i Århus Slide 23 Intermediate Language code

24 Ingeniørhøjskolen i Århus Slide 24 What Is a Web Form?.aspx extension Page attributes –@ Page directive Body attributes Form attributes

25 Ingeniørhøjskolen i Århus Slide 25 Creating a Web Form with Visual Studio.NET New ASP.NET Web Application: –create a default Web Form: WebForm1.aspx Create additional Web Forms from the Solution Explorer Upgrade existing HTML pages into Web Forms

26 Ingeniørhøjskolen i Århus Slide 26 Types of Server Controls HTML server controls Web server controls –Intrinsic controls –Validation controls –Rich controls –List-bound controls –Internet Explorer Web controls –User Controls (controls made by the user)

27 Ingeniørhøjskolen i Århus Slide 27 HTML Server Controls Based on HTML elements Exist within the System.Web.UI.HtmlControls namespace Are transformed into plain HTML elements

28 Ingeniørhøjskolen i Århus Slide 28 What is a Server Control? Equals JSP taglibs Runat="server" –Events happen on the server –View state saved Have built-in functionality Common object model –All have Id and Text attributes Create browser-specific HTML

29 Ingeniørhøjskolen i Århus Slide 29 Web Server Controls Exist within the System.Web.UI.WebControls namespace Control syntax HTML that is generated by the control Text_to_Display Text_to_Display

30 Ingeniørhøjskolen i Århus Slide 30 You need specific functionality such as a calendar or ad rotator The control will interact with client and server script You are writing a page that might be used by a variety of browsers You are working with existing HTML pages and want to quickly add ASP.NET Web page functionality You prefer a Visual Basic-like programming model You prefer an HTML-like object model Use Web Server Controls if: Use HTML Server Controls if: Bandwidth is not a problemBandwidth is limited Selecting the Appropriate Control

31 Ingeniørhøjskolen i Århus Slide 31 How to Implement Code Three methods for adding code: –Put code in the same file as content (mixed) –Put code in a separate section of the content file (inline code) –Put code in a separate file (code-behind pages) Code-behind pages are the Visual Studio.NET default

32 Ingeniørhøjskolen i Århus Slide 32 Writing Inline Code Code and content in the same file Different sections in the file for code and HTML Sub btn_Click(s As Object, e As EventArgs) Handles btn.Click... End Sub Sub btn_Click(s As Object, e As EventArgs) Handles btn.Click... End Sub private void btn_Click(object sender, System.EventArgs e) {... } private void btn_Click(object sender, System.EventArgs e) {... }

33 Ingeniørhøjskolen i Århus Slide 33 A Simple ASP.NET Example Scripting delimiters –Wrapped around CLS/C#/VB/Jscript.NET code –Compiled and executed on the server –@ Page directive Specifies information needed by CLR to process file element –needed for definitions of variables and functions –Language attribute Specifies JScript.NET as scripting language –runat attribute with value “server” Indicates script should be processed on server

34 Ingeniørhøjskolen i Århus Slide 34 af 57 date.aspx (1 of 2) use

35 Ingeniørhøjskolen i Århus Slide 35 af 57 date.aspx (2 of 2) use for inline executable scriplet code

36 Ingeniørhøjskolen i Århus Slide 36 Second Approach: Code-Behind Pages Separation of code from content –Developers and UI designers can work independently Form1.aspxForm1.aspx Form1.aspx.vb Form1.aspx.vb or Form1.aspx.cs code code Separate filesSingle file

37 Ingeniørhøjskolen i Århus Slide 37 Understanding How Code-Behind Pages Work Create separate files for user interface and interface logic Use @ Page directive to link the two files Pre-compile or JIT-compile Page1.aspx Page1.aspx.cs public class WebForm1 { private void cmd1_Click() { … }

38 Ingeniørhøjskolen i Århus Slide 38 Code-Behind Screenshot and.aspx Reference to it

39 Ingeniørhøjskolen i Århus Slide 39 What are Event Procedures? Action in response to a user’s interaction with the controls on the page

40 Ingeniørhøjskolen i Århus Slide 40 Traditional: Client-Side Event Procedures Internet.HTM Pages Typically, used only with HTML controls Interpreted by the browser and run on the client Does not have access to server resources Uses

41 Ingeniørhøjskolen i Århus Slide 41 New: Server-Side Event Procedures Used with both Web and HTML server controls Code is compiled and run on the server Have access to server resources Use or Internet.ASPX Pages

42 Ingeniørhøjskolen i Århus Slide 42 Creating Event Procedures Visual Studio.NET declares variables and creates an event procedure template Using the Handles keyword adds many event procedures to one event protected System.Web.UI.WebControls.Button cmd1; private void InitializeComponent() { this.cmd1.Click += new System.EventHandler(this.cmd1_Click); this.Load += new System.EventHandler(this.Page_Load); } private void cmd1_Click(object s, System.EventArgs e) protected System.Web.UI.WebControls.Button cmd1; private void InitializeComponent() { this.cmd1.Click += new System.EventHandler(this.cmd1_Click); this.Load += new System.EventHandler(this.Page_Load); } private void cmd1_Click(object s, System.EventArgs e) Protected WithEvents cmd1 As System.Web.UI.WebControls.Button Private Sub cmd1_Click(ByVal s As System.Object, _ ByVal e As System.EventArgs) Handles cmd1.Click Protected WithEvents cmd1 As System.Web.UI.WebControls.Button Private Sub cmd1_Click(ByVal s As System.Object, _ ByVal e As System.EventArgs) Handles cmd1.Click

43 Ingeniørhøjskolen i Århus Slide 43 Understanding the Page Event Life Cycle Page_Load Page_Unload Textbox1_Changed Button1_Click Page is disposed Page_Init Control events Change Events Action Events

44 Ingeniørhøjskolen i Århus Slide 44 Handling Page.IsPostback Events Page_Load fires on every request –Use Page.IsPostBack to execute conditional logic –Page.IsPostBack prevents reloading for each postback Private Sub Page_Load(ByVal s As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then 'executes only on initial page load End If 'this code executes on every request End Sub Private Sub Page_Load(ByVal s As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then 'executes only on initial page load End If 'this code executes on every request End Sub private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { // executes only on initial page load } //this code executes on every request } private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { // executes only on initial page load } //this code executes on every request }

45 Ingeniørhøjskolen i Århus Slide 45 Comparison J2EE JSP/Servlet technology is –Just for Java –Many platforms –Object-oriented and easy to maintain ASP.NET technology is –Many languages –Just one platform (but maybe not much longer) –Object-oriented and easy to maintain ASP –VBScript og JScript –Just one platform (but: Windows CE!) –Not object-oriented (but object-based) and hard to maintain –Will soon be obsolete


Download ppt "Internetteknologi (ITNET2) Præsentation 20: ASP.NET Introduktion ”Med Active Server Pages (ASP)”"

Lignende præsentationer


Annoncer fra Google