Internet Programming - 05 06
Internet Programming - 05 06
jQuery
Web Servers
jQuery
• jQuery is a fast, small, and feature-rich JavaScript library.
• It makes things like HTML document traversal and manipulation, event handling, animation,
and Ajax much simpler with an easy-to-use API that works across a multitude of browsers.
• There are two versions of jQuery available for downloading:
◦ Production version - this is for your live website because it has been minified and compressed
◦ Development version - this is for testing and development (uncompressed and readable code)
◦ https://jquery.com/download/
jQuery (cont.)
• jQuery CDN (Content Delivery Network): such as Google
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js">
</script>
</head>
• ViewData maintains data when you move from controller to view. It is also a dictionary object
and derived from ViewDataDictionary.
◦ Null checking and typecasting is required while using ViewData.
• ViewBag is a dynamic type property of ControllerBase class which is the base class of all the
controllers. It’s a dynamic wrapper around ViewData casting is not required when you use
ViewBag. ViewBag only transfers data from controller to view, not visa-versa.
Cookies
• Cookies provide web developers with a tool for personalizing web pages.
• A cookie is a piece of data that is stored on the user’s computer to maintain information about
the client during and between browser sessions.
• A website may store a cookie on the client’s computer to record user preferences or other
information that the website can retrieve during the client’s subsequent visits. For example, a
website can retrieve the user’s name from a cookie and use it to display a personalized
greeting.
• Web Browsers store cookies as small text files on the client’s hard drive.
• When a user visits a website, the browser locates any cookies written by scripts on that site
and makes them available to any scripts located on the site.
• The cookies may be accessed only by scripts belonging to the same website from which they
originated .
Cookies (cont.)
• Cookies are accessible in JavaScript through the document object’s cookie property. JavaScript
treats a cookie as a string of text.
• A cookie has the syntax “identifier=value,” where identifier is any valid JavaScript variable
identifier, and value is the value of the cookie variable.
• When multiple cookies exist for one website, identifier-value pairs are separated by semicolons
in the document.cookie string.
• Each cookie has an expiration date, after which the web browser deletes it. This date can be
defined by setting the expires property in the cookie string.
• If a cookie’s expiration date is not set, then the cookie expires by default after the user closes
the browser window.
• A cookie can be deleted immediately by setting the expires property to a date and time in the
past.
ASP.NET Ajax
• Ajax: Asynchronous JavaScript and XML.
• ASP.NET Ajax is used to quickly and easily improve the user experience for the web applications.
• In a traditional web application, the user first fills in the form’s fields, then submits the form . The
browser generates a request to the server, which receives the request and processes it .
• The server generates and sends a response containing the exact page that the browser renders ,
which causes the browser to load the new page and temporarily makes the browser window blank.
• The client waits for the server to respond and reloads the entire page with the data from the
response. While such a synchronous request is being processed on the server, the user cannot
interact with the web page.
• If the user interacts with and submits another form, the process begins again.
• Every full-page refresh required users to reload the full page. Users began to demand a more
responsive model.
Traditional web application reloading the page for every user interaction
ASP.NET Ajax (cont.)
• Ajax web applications add a layer between the client and the server to manage communication between the two
• When the user interacts with the page, the client requests information from the server .
• The request is intercepted by the ASP.NET Ajax controls and sent to the server as an asynchronous request - the
user can continue interacting with the application in the client browser while the server processes the request.
Other user interactions could result in additional requests to the server.
• Once the server responds to the original request , the ASP.NET Ajax control that issued the request calls a client-
side function to process the data returned by the server. This function—known as a callback function—uses
partial-page updates to display the data in the existing web page without reloading the entire page.
• At the same time, the server may be responding to the second request and the client browser may be starting
another partial-page update.
• The callback function updates only a designated part of the page. Such partial-page updates help make web
applications more responsive, making them feel more like desktop applications.
• The web application does not load a new page while the user interacts with it.
Ajax-enabled web application interacting with the server asynchronously
AJAX XMLHttpRequest
• XMLHttpRequest object can be used to exchange data with a web server behind the scenes
Method Description
new XMLHttpRequest() Creates a new XMLHttpRequest object
abort() Cancels the current request
getAllResponseHeaders() Returns header information
getResponseHeader() Returns specific header information
open(method, url, async, user, psw) Specifies the request
method: the request type GET or POST
url: the file location async: true (asynchronous) or false (synchronous)
user: optional user name psw: optional password
send() Sends the request to the server. Used for GET requests
send(string) Sends the request to the server. Used for POST requests
setRequestHeader() Adds a label/value pair to the header to be sent
Property Description
onload Defines a function to be called when the request is recieved (loaded)
onreadystatechange Defines a function to be called when the readyState property changes
readyState Holds the status of the XMLHttpRequest.
0: request not initialized
1: server connection established
2: request received
3: processing request
4: request finished and response is ready
responseText Returns the response data as a string
responseXML Returns the response data as XML data
status Returns the status-number of a request
200: "OK"
403: "Forbidden"
404: "Not Found"
statusText Returns the status-text (e.g. "OK" or "Not Found")
Database
• A database is an organized collection of data.
• Database management system (DBMS) provides mechanisms for storing, organizing, retrieving
and modifying data. Database management systems allow for the access and storage of data
without concern for the internal representation of the data in the database.
• Programs connect to, and interact with, a relational database via an interface—software that
facilitates communication between a database management system and a program.
• For example, Java developers can use the JDBC interface to interact with databases. ASP.NET
programmers communicate with databases and manipulate their data through the interface
provided by ADO.NET.
Relational Databases
• A relational database is a logical representation of data that allows the data to be accessed
without consideration of its physical structure.
• A relational database stores data in tables.
• Each column represents a data attribute.
Relational Databases (cont.)
• Entity-relationship (ER) diagram shows the database tables and the relationships among them.
• The first compartment in each box contains the table’s name.
• The names in italic are primary keys. A table’s primary key uniquely identifies each row in the
table. Every row must have a primary-key value, and that value must be unique in the table.
This is known as the Rule of Entity Integrity.
• A foreign key creates a relationship between two tables in which the record with a given
primary key can be referenced many times in the foreign key’s table.
SQL
• SQL (Structured Query Language) is a standard language for storing, manipulating and
retrieving data in databases.
ADO.NET Object Model
• The ADO.NET object model provides an API for accessing database management systems
programmatically.
• ADO.NET namespaces, System.Data.OleDb and System.Data.SqlClient, contain classes that
enable programs to connect with and manipulate data sources—locations that contain data,
such as a database or an XML file.
• Namespace System.Data.OleDb contains classes that are designed to work with any data
source that supports the OLE DB API.
• Namespace System.Data.SqlClient contains classes that are optimized to work with Microsoft
SQL Server databases.
ADO.NET Object Model (cont.)
• An object of class SqlConnection (namespace System.Data.SqlClient) represents a connection
to Microsoft SQL Server database.
• A SqlConnection object keeps track of the location of the data source and any settings that
specify how the data source is to be accessed. A connection is either active (i.e., open and
permitting data to be sent to and retrieved from the data source) or closed.
• An object of class SqlCommand represents a SQL command that a DBMS can execute on a
database. A program can use SqlCommand objects to manipulate a data source through a
SqlConnection. The program must open the connection to the data source before executing one
or more SqlCommands and close the connection once no further access to the data source is
required.
• A connection that remains active for some length of time to permit multiple data operations is
known as a persistent connection.
ADO.NET Object Model (cont.)
• Class DataTable (namespace System.Data) represents a table of data.
• A DataTable contains a collection of DataRows that represent the table’s data. A DataTable also
has a collection of DataColumns that describe the columns in a table.
• An object of class System.Data.DataSet, which consists of a set of DataTables and the
relationships among them, represents a cache of data—data that a program stores temporarily
in local memory. The structure of a DataSet mimics the structure of a relational database.
• An advantage of using class DataSet is that it is disconnected—the program does not need a
persistent connection to the data source to work with data in a DataSet.
• Connection string example
data source=(local);Integrated Security=True; initial catalog=“MyDatabase”
Language Integrate Query (LINQ)
• LINQ allows you to write query expressions, similar to SQL queries, that retrieve information
from a wide variety of data sources, not just databases.
• int[] values = { 2, 9, 5, 0, 3, 7, 1, 4, 8, 5 };
Language Integrate Query (LINQ) (cont.)
• Any method returns true if there’s at least one element, and false if there are no elements.
• First method returns the first element in the result. You should check that the query result is
not empty before calling First.
• Count method returns the number of elements in the results.
• let clause creates a new range variable. This is useful if you need to store a temporary result for
use later in the LINQ query.
LINQ to SQL
▪ LINQ to SQL enables you to access data in SQL Server
databases using the same LINQ syntax.
▪ All LINQ to SQL queries occur via a DataContext class,
which controls the flow of data between the program and
the database.
NoSQL Databases
• “NoSQL” databases such as MongoDB or Redis are alternatives to relational databases.
• There are many different types of databases that are classified as NoSQL. The most common
types are key-value stores , document stores and Graph databases.
• Many NoSQL databases are promoted based on the performance advantages they offer over
relational databases, and the benefit of avoiding doing any schema design or migrations.
Key-value stores
• A key-value store can be thought of as similar to a hash or a dictionary in dynamic
programming languages, in that the only way of addressing content is by using a key, and the
actual data stored at that key is opaque.
• Many key-value stores do offer some abstractions over this; for example, Redis allows a key to
refer to a set or a list, but the rule of finding a value using only a key still applies.
• The actual value stored at each key need not be alike, and could widely differ in structure
between keys.
Document stores
• Document stores like MongoDB.
• In a document store, although a document will often have a unique ID like a key, it is often just a
field inside a larger document.
• These “documents” tend to hold data structured as JSON or XML, and the key difference
between these types of stores and key-value stores is that you can look up data based on the
content of the document, not just the key.
• There are also hybrids between key-value stores and document stores. DynamoDB, for
example, behaves like a key-value store, but you can also add a schema to describe the type of
data stored at a key that allows you to query on the value too. This schema is used to define an
index.
• For performance, both relational and NoSQL databases allow you to specify an index on a table
or collection, which can be used for fast lookups by looking up the data in an index, rather than
searching all the raw data in response to a query.
Graph databases
• Graph databases such as Neo4J represent data as a graph and allow you to write queries by
traversing the graph.
• Columnar databases such as Cassandra work by storing data by the column, rather than by the
row, as in a traditional relational database. This is usually done for performance reasons when
dealing with very large data sets.
SQL or NoSQL
• Whether or not to go relational or NoSQL depends on the type of data you have.
• Most NoSQL databases do not allow joining, so can work out to be less performant for certain types
of query.
• This is often worked around by repeating information inside documents (for example, author name,
rather than linking to an author table), a process known as denormalization.
• A database that is normalized does not contain any repeated or redundant information. Although
denormalized tables are not necessarily problematic, they do make it easier to introduce
inconsistencies, and can make it harder to update data that appears in many places in the database.
• Although NoSQL databases avoid the constraint of formally specifying a schema, you will often
want to have some type of schema to keep a reasonable data structure, either to be read from in
your application or to create indexes.
• NoSQL databases can also be faster to write to, so if you have many more writes than reads, this
can be a better fit, but for most other types of data, a relational database will serve you well.
Thank You
References
▪ Deitel, Harvey M., Paul J. Deitel, and Tem R. Nieto. Internet & world wide web: how to
program.
▪ The Full Stack Developer: Your Essential Guide to the Everyday Skills Expected of a Modern Full
Stack Web Developer, Northwood, Chris.
▪ https://www.dotnetperls.com/sqlconnection