Download presentation
Presentation is loading. Please wait.
1
Introduction to Server-Side Programming
2
Application Programs and User Interfaces
Most of the time, persistent data requires some sort of database Nowadays, most database users do not directly use a query language such as SQL An application program acts as the intermediary between users and the database (think of your student account on the JUST Univ. system) Applications split into front-end middle layer backend Front-end: user interface Forms Graphical user interfaces Many interfaces are Web-based Intro. to Server-Side Programming
3
Application Architecture Evolution
Three distinct era’s of application architecture mainframe (1960’s and 70’s) personal computer era (1980’s) Web era (1990’s onwards) Intro. to Server-Side Programming
4
Client Side Programming / Scripting
Intro. to Server-Side Programming
5
Intro. to Server-Side Programming
Web Interface Nowadays, web browsers have become the de-facto standard user interface to databases Advantages: Enable large numbers of users to access databases from anywhere Avoid the need for downloading/installing specialized programs, while providing a good graphical user interface Enables scripting technologies Javascript, Flash and other scripting languages run in browsers Examples: Banks airline booking rental car reservations university course registration and grading, etc. Intro. to Server-Side Programming
6
Intro. to Server-Side Programming
The World Wide Web The Web is a distributed information system based on hypertext. Most Web documents are hypertext documents formatted via the HyperText Markup Language (HTML) HTML documents contain text along with font specifications, and other formatting instructions hypertext links to other documents, which can be associated with regions of the text. forms, enabling users to enter data which can then be sent back to the Web server Intro. to Server-Side Programming
7
Sample HTML Source Text
<body> <table border> <tr> <th>ID</th> <th>Name</th> <th>Department</th> </tr> <tr> <td>10</td> <td>AA</td> <td>Comp. Sci.</td> </tr> …. </table> <form action="PersonQuery" method=get> Search for: <select name="persontype"> <option value="student" selected> Student </option> <option value="instructor"> Instructor </option> </select> <br> Name: <input type=text size=20 name="name"> <input type=submit value="submit"> </form> </body> </html> Intro. to Server-Side Programming
8
Display of Sample HTML Source
Intro. to Server-Side Programming
9
Uniform Resources Locators
In the Web, functionality of pointers is provided by Uniform Resource Locators (URLs). URL example: The first part indicates how the document is to be accessed “http” indicates that the document is to be accessed using the Hyper Text Transfer Protocol. The second part gives the unique name of a machine on the Internet. The rest of the URL identifies the document within the machine. The local identification can be: The path name of a file on the machine, or An identifier (path name) of a program, plus arguments to be passed to the program E.g., …./PersonQuery?persontype=Instructor&name=AA Intro. to Server-Side Programming
10
Intro. to Server-Side Programming
HTML and HTTP HTML provides formatting, hypertext link, and image display features including tables, stylesheets (to alter default formatting), etc. HTML also provides input features Select from a set of options Pop-up menus, radio buttons, check lists Enter values Text boxes Selected/Filled-in input sent back to the server, to be acted upon by an executable at the server HyperText Transfer Protocol (HTTP) used for communication with the Web server Intro. to Server-Side Programming
11
Intro. to Server-Side Programming
Client Side Scripting Browsers can fetch certain scripts (client-side scripts) or programs along with documents, and execute them in “safe mode” at the client site Javascript & VBScript Macromedia Flash and Shockwave for animation/games VRML (Virtual Reality Modeling Language, pronounced vermal) Applets Client-side scripts/programs allow documents to be active E.g., animation by executing programs at the local site (client side) E.g., ensure that values entered by users satisfy some correctness checks Permit flexible interaction with the user. Executing programs at the client site speeds up interaction by avoiding many round trips to server Intro. to Server-Side Programming 11
12
Client Side Scripting and Security
Security mechanisms needed to ensure that malicious scripts do not cause damage to the client machine Easy for limited capability scripting languages, harder for general purpose programming languages like Java E.g., Java’s security system ensures that the Java applet code does not make any system calls directly Disallows dangerous actions such as file writes Notifies the user about potentially dangerous actions, and allows the option to abort the program or to continue execution. Intro. to Server-Side Programming 12
13
Intro. to Server-Side Programming
Javascript Javascript very widely used forms basis of new generation of Web applications (called Web applications) offering rich user interfaces Javascript functions can check input for validity modify the displayed Web page, by altering the underling document object model (DOM) tree representation of the displayed HTML text communicate with a Web server to fetch data and modify the current page using fetched data, without needing to reload/refresh the page forms basis of AJAX technology used widely in Web 2.0 applications AJAX : Asynchronous JavaScript And XML. E.g. on selecting a country in a drop-down menu, the list of states in that country is automatically populated in a linked drop-down menu Intro. to Server-Side Programming
14
Intro. to Server-Side Programming
Javascript Examples Intro. to Server-Side Programming
15
Intro. to Server-Side Programming
Javascript Example: 1 Example of Javascript used to validate form input <html><head> <script type="text/javascript"> function validate() { var credits=document.getElementById("credits").value; if (isNaN(credits)|| credits<=0 || credits>=16) alert("Credits must be a number greater than 0 and less than 16"); return false; } </script> </head><body> <form action="createCourse" onsubmit="return validate()"> Title: <input type="text" id="title" size="20"><br/> Credits: <input type="text" id="credits" size="2"><br/> <Input type="submit" value="Submit"> </form> </body></html> Intro. to Server-Side Programming
16
Server Side Programming / Scripting
Intro. to Server-Side Programming
17
Intro. to Server-Side Programming
Web Servers A Web server can easily serve as a front end to a variety of information services. The document name in a URL may identify an executable program, that, when run, generates an HTML document. When an HTTP server receives a request for such a document, it executes the program, and sends back the HTML document that is dynamically generated. The Web client can pass extra arguments with the name of the document. To install a new service on the Web, you simply need to create and install an executable that provides that service. Web browser provides a graphical user interface to the information service. Common Gateway Interface (CGI): a standard interface between web and application server Intro. to Server-Side Programming
18
Intro. to Server-Side Programming
Web Server A Web server is a program that uses: the client/server model the HyperText Transfer Protocol ( HTTP ), and serves the files that form static and dynamic Web pages and send them back to the Web users Webpages can be: an HTML page (static content) or Dynamic webpages that are created and customized based on the user request The web server may delegate the generation of a particular user response to some other program such as CGI scripts, Servlets, JSPs, ASPs, or PHP scripts in the application server. Intro. to Server-Side Programming
19
Two-Layer Web Architecture
Multiple levels of indirection have overheads Alternatives: two-layer architecture But what about other features such as: security, scalability, etc. Intro. to Server-Side Programming
20
Three-Layer Web Architecture
Intro. to Server-Side Programming
21
Intro. to Server-Side Programming
HTTP and Sessions The HTTP protocol is connectionless That is, once the server replies to a request, the server closes the connection with the client, and forgets all about the request In contrast, Unix logins, and JDBC/ODBC connections stay connected until the client disconnects retaining user authentication and other information Motivation: reduces load on server operating systems have tight limits on number of open connections on a machine Information services need session information E.g., user authentication should be done only once per session Solution: we may use a cookie ?? Intro. to Server-Side Programming
22
Intro. to Server-Side Programming
Sessions and Cookies A cookie is a small piece of text containing identifying information Sent by server to browser Sent on first interaction, to identify session Sent by browser to the server that created the cookie on further interactions part of the HTTP protocol and its headers A Web Server saves information about cookies it issued, and can use it when serving a request E.g., authentication information, and user preferences Cookies can be stored permanently or for a limited time Intro. to Server-Side Programming
23
Intro. to Server-Side Programming
Example Servlet Code import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class PersonQueryServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HEAD><TITLE> Query Result</TITLE></HEAD>"); out.println("<BODY>"); ….. BODY OF SERVLET (next slide) … out.println("</BODY>"); out.close(); } Intro. to Server-Side Programming
24
Intro. to Server-Side Programming
Example Servlet Code String persontype = request.getParameter("persontype"); String number = request.getParameter("name"); if(persontype.equals("student")) { ... code to find students with the specified name ... ... using JDBC to communicate with the database .. out.println("<table BORDER COLS=3>"); out.println(" <tr> <td>ID</td> <td>Name: </td>" + " <td>Department</td> </tr>"); for(... each result ...){ ... retrieve ID, name and dept name ... into variables ID, name and deptname out.println("<tr> <td>" + ID + "</td>" + "<td>" + name + "</td>" + "<td>" + deptname "</td></tr>"); }; out.println("</table>"); } else { ... as above, but for instructors ... Intro. to Server-Side Programming
25
Intro. to Server-Side Programming
Servlet Sessions Servlet API supports handling of sessions Sets a cookie on first interaction with browser, and uses it to identify session on further interactions To check if session is already active: if (request.getSession(false) == true) .. then existing session else .. redirect to authentication page authentication page check login/password request.getSession(true): creates new session Store/retrieve attribute value pairs for a particular session session.setAttribute(“userid”, userid) session.getAttribute(“userid”) Intro. to Server-Side Programming
26
Intro. to Server-Side Programming
Servlet Support Servlets run inside application servers such as Apache Tomcat, Glassfish, JBoss BEA Weblogic, IBM WebSphere and Oracle Application Servers Application servers support deployment and monitoring of servlets Java 2 Enterprise Edition (J2EE) platform supporting objects, parallel processing across multiple application servers, etc Intro. to Server-Side Programming
27
Server-Side Scripting
Server-side scripting simplifies the task of connecting a database to the Web Define an HTML document with embedded executable code/SQL queries. Input values from HTML forms can be used directly in the embedded code/SQL queries. When the document is requested, the Web server executes the embedded code/SQL queries to generate the actual HTML document. Numerous server-side scripting languages JSP, PHP General purpose scripting languages: VBScript, Perl, Python Intro. to Server-Side Programming
28
Java Server Pages (JSP)
A JSP page with embedded Java code <html> <head> <title> Hello </title> </head> <body> <% if (request.getParameter(“name”) == null) { out.println(“Hello World”); } else { out.println(“Hello, ” + request.getParameter(“name”)); } %> </body> </html> JSP is compiled into Java + Servlets JSP allows new tags to be defined, in tag libraries such tags are like library functions, can are used for example to build rich user interfaces such as paginated display of large datasets Intro. to Server-Side Programming
29
Intro. to Server-Side Programming
PHP PHP is widely used for Web server scripting Extensive libaries including for database access using ODBC <html> <head> <title> Hello </title> </head> <body> <?php if (!isset($_REQUEST[‘name’])) { echo “Hello World”; } else { echo “Hello, ” + $_REQUEST[‘name’]; } ?> </body> </html> Intro. to Server-Side Programming
30
Intro. to Server-Side Programming
Java Servlets Java Servlets are programs that run on a: Web server or Application server and act as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server Servlets enable you to: collect input from users through web page forms, present records from a database or another source, and create web pages dynamically (dynamically create and modify the contents that is to be presented to the end user based on various parameters that are presented in the request) Intro. to Server-Side Programming
31
Intro. to Server-Side Programming
Servlets Advantages Servlets execute within the address space of a Web server. It is not necessary to create a separate process to handle each client request; like what happens in CGI Servlets are platform-independent because they are written in Java Java security manager on the server enforces a set of restrictions to protect the resources on a server machine. So servlets can be trusted The full functionality of the Java class libraries is available to a servlet. A servlet can communicate with: applets, databases, or other software via sockets and/or RMI mechanisms Intro. to Server-Side Programming
32
Servlets Architecture
Intro. to Server-Side Programming
33
Intro. to Server-Side Programming
Servlets Packages Java Servlets are Java classes run by a web server that has: A java interpreter Support the Java Servlet specification Servlets can be created using java pakages such as: javax.servlet javax.servlet.http These packages are standard part of the Java’s Enterprise Edition (JEE) an expanded version of the Java class library that supports large-scale development projects Intro. to Server-Side Programming
34
Intro. to Server-Side Programming
Sample Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { // Extend HttpServlet class private String message; public void init() throws ServletException { // Do required initialization message = "Hello World"; } public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); // Set response content type PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>"); out.println("<b><font color='blue'>" + "IP Address of request : </font></b>" + request.getRemoteAddr()+"<h3>"); public void destroy() { Intro. to Server-Side Programming
35
Deployment Descriptor – web.xml
<servlet> <servlet-name>HelloWorld</servlet-name <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> Intro. to Server-Side Programming
36
Intro. to Server-Side Programming
Java Server Pages Java Server Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %> Designed to fulfill the role of a user interface for a Java web application Intro. to Server-Side Programming
37
Intro. to Server-Side Programming
JSP cont’d Enables you to collect input from users through web page forms, present records from a database or another source, and create web pages dynamically. JSP tags can be used for a variety of purposes such as: retrieving information from a database registering user preferences accessing JavaBeans components passing control between pages sharing information between requests Intro. to Server-Side Programming
38
Intro. to Server-Side Programming
JSP Advantages JSP allows embedding Dynamic Elements in HTML Pages itself. JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP etc. JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines. Intro. to Server-Side Programming
39
Intro. to Server-Side Programming
JSP Processing The browser sends an HTTP request to the web server The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the URL or JSP page which ends with .jsp instead of .html The JSP engine loads the JSP page from disk and converts it into a servlet content The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format, which the servlet engine passes to the web server inside an HTTP response The web server forwards the HTTP response to your browser in terms of static HTML content Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if it were a static page Intro. to Server-Side Programming
40
Intro. to Server-Side Programming
JSP Processing Intro. to Server-Side Programming
41
Intro. to Server-Side Programming
JSP Example <html> <head> <title>Hello World</title> </head> <body> Hello World!<br/> <% out.println("Your IP address is " + request.getRemoteAddr()); %> </body> </html> Intro. to Server-Side Programming
42
Intro. to Server-Side Programming
Another JSP Example main.html <html> <head><title>Add Two Numbers in JSP</title></head> <body> <form action=“add.jsp" method="get"> First Number: <input type="text" name="firstNumber"> Second Number: <input type="text" name=“secondNumber"><br> <input type="submit"> </form> </body> </html> add.jsp <% int firstNumber = Integer.parseInt(request.getParameter("firstNumber")); int secondNumber = Integer.parseInt(request.getParameter("secondNumber")); int result = firstNumber + secondNumber; %> Output: <br> <%=firstNumber%> + <%=secondNumber%> = <%=result%> Intro. to Server-Side Programming
43
Hypertext Markup Language - HTML
The standard markup language used to create web pages <html> <head> <title>Page Title</title> </head> <body> <h1>Hello SE432 Students</h1> <p>Enjoy the course material!</p> </body> </html> Intro. to Server-Side Programming
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.