This document provides an overview of server-side web programming. It discusses how web servers deliver static and dynamic web pages using technologies like HTML, CSS, JavaScript, Java, PHP, and databases. It then covers common web servers like Apache and IIS. The main server-side technologies - CGI, servlets, and PHP - are explained. Servlets are Java classes that extend server capabilities, while PHP is a scripting language designed for web development. Examples are given to demonstrate basic servlets and PHP scripts. The document also mentions modifying configuration files, handling GET/POST requests, and returning JSON/XML data.
Preliminaries
⢠HTML
â Standardmark up language to create web pages
⢠CSS
â Style sheets for web pages
⢠Javascript
â Client side programming
⢠Java, Perl, Python, Php
3.
What are webservers
⢠Hardware or software
â Helps deliver the web content that can be
accessed through internet (Wikipedia)
Web Server
File System
Request
Response
Read Static
Files
Web Servers
⢠History
âCERN httpd
⢠Apache â 60%
â Open source
â Linux, Windows, Mac
⢠IIS -- Internet Information Service
â Microsoft
â Windows
⢠Mostly installed automatically
⢠Generally run on Windows Server edition
⢠High Performance
⢠Nginx
⢠Lighttpd
â Both usually used as forward and reverse Proxy
Web servers
⢠Deliversstatic web pages
⢠Donât we see a lot of dynamic pages on the
web?
â Weather reports
â Date time changes on web sites
â User information updates
Frontend
⢠Data hasto be sent to the server and response
has to displayed.
⢠How do we send data to the server
â HTTP methods
â Javascript
⢠Ajax
⢠JSON, XML
â HTML Forms
13.
HTTP Methods
⢠Get
âData encoded in the URL
â For idempotent data
⢠No changes are done except on the users screen
⢠Basically for retrieving data
â http://knoesis.org
/<pagename>?<param1>=<value1>&<param2>=<value2>âŚ
â Param and value is the data sent.
⢠Post
â Data goes with the message body
â Changes the state
⢠Adds or deletes data at the server
â Cannot backtrack the history because the URL will be the same
14.
HTML Forms
⢠PassData to the server
â Text fields, check box, radio button, submit button
etc.
⢠For More
â http://www.w3.org/TR/html401/interact/forms.ht
ml
CGI
⢠Common GatewayInterface (CGI)
â Runs a scripts for every request â New process
⢠Drawbacks
â Limited Functionality
â Scalability Issues
19.
Servlets
⢠Java ProgrammingClass used to extend the
capabilities of server that host applications
accessed via a request-response programming
model.
⢠Java Technologyâs answer to CGI.
⢠Advantages of Java?
⢠Needs a servlet container
20.
Servlet Container
⢠Componentof a web server that interacts
with the Servlets
â Manages the Lifecycle of a servlet
â Mapping URL to appropriate servlets
â Security, concurrency, deployment etc .
⢠Examples
â Apache Tomcat (opensource)
â Jboss (opensource)
â IBM Websphere
Plain HTML TextServlet
package testPackage; // Always use packages.
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
@WebServlet("/hello")
public class HelloWorld extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
23.
Interpreting the Servlet
â˘@WebServlet("/helloâ)
â This is the URL relative to the app name
â http://knoesis.org:8080/hello
⢠doGet
â Code for an HTTP GET request. doPost also common.
⢠HttpServletRequest, HttpServletResponse
â Contains anything that comes from the browser
â Send data back to the browser
⢠@Override
24.
Lets see howto generate a simple
HTML
<!DOCTYPE html>
<html lang="en">
<head>
..
</head>
<body>
...
</body>
</html>
25.
Servlet that generatesHTML
@WebServlet("/test1")
public class TestServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println ("<!DOCTYPE html>n"
+"<html>n" +
"<head><title>A Test Servlet</title></head>n" +
"<body bgcolor="#fdf5e6">n" +
"<h1>Test</h1>n" +
"<p>Simple servlet for testing.</p>n" +
"</body></html>");
}
}
Php
⢠General purposeserver-side scripting
language originally designed for web
development to produce dynamic web pages
⢠Hypertext Preprocessor
⢠Dynamically Typed Language
28.
Php â HelloWorld
⢠Embed PHP script into an HTML ďŹle
⢠Upload the ďŹle onto a Web server using
extension .php (Apache)
⢠Embed using the following delimiters
â < ? ... ? >
â <?php ... ?>
â <script language=âphpâ> ... </script>
â <% ... %>
Php Applications
⢠Widerange of applications (similar to CGI)
â Forms handling, etc.
⢠Wide range of PHP libraries
â Network connectivity (e.g. access FTP, IMAP, SMTP, etc
â Database connectivity (e.g. MySQL, dBase, Oracle,
etc.)
â XML/XSLT manipulation
â Image manipulation
31.
GET â POST
â˘Access form ďŹelds through PHP array
â $HTTP_POST_VARS for POST method
â $HTTP_GET_VARS for GET method
â $_POST for POST method (>=PHP4.1.0)
â $_GET for GET method (>=PHP4.1.0)
$name = $_POST["name"];
$name = $_GET["name"];
⢠Similar to request.getParameter(ânameâ) in
doPost/doGet in Servlets.
32.
Prints even numbersuntil
âmaxâ
<html>
<head><title>A Test Php</title></head>
<body bgcolor="#fdf5e6">
<p> <ul>
<?php
$maximum = $_GET["max"];
for($i=0; $i<=$maximum ; $i=$i+2){
echo "<li>$i</li>";
}
?>
</ul>
</p>
</body>
</html>
JSON, XML Handling
â˘Change the content type
⢠Construct a Json/XML
⢠Pass it as it is done for plain text or html
⢠More about this in the next class.
⢠Might be a part of the assignment
37.
CGI/Perl
⢠1987 âBefore the web
⢠Text manipulation language
⢠CGI â Common Gateway interface
â Can run any language
â Commonly perl
⢠Advantages
â Mature language over a decade of history
â Free and most os support perl
â Vast network of perl developers
⢠Disadvantage
â Scalability
â Optimized for unix platform
â Different ways of doing the same thing hence not easy to learn
Editor's Notes
#2Â Hello, This video we will be talking about server side web programming for the Web Information Systems class of 2015
#3Â In the previous videos, you have learnt about HTML , CSS and javascript as a client side programming. For this lecture, you will need some background in one of these languages. Java, Perl, Python and Php. I will touch upon Java and Php in this video and then hopefully if we have sometime talk about Python in the class.
#4Â Before we dwell into the programming languages, first, let us learn some basics about web servers. Webservers are programs that delivers web content. As shown in the Figure, the web server receives a request, accesses its static files from the file system and sends the response.
#5Â For example, if you type a URL as shown on your browser, the browser sends a request to a server that runs a web server program. The webserver reponds to the request with the appropriate html and css files. Those are displayed on your browser.
#6Â Since we now know what web servers do, I will give you some details on the history of it. In 1990 TBL wrote two programs, one of the browser and the other one was the web server. He named it as CERN httpd, since it was developed in CERN. However, lately Apache is the most commonly used web server, i.e 60% of the web servers running are apache. It is opensource and can run on all the Oss. The next popular one is from Microsoft and is called IIS (Internet Information Service). It runs of Windows. There are a few others that are used for high performance.
#7Â In this class, we will be working with Apache web server. If you want to run it on your manchines, I would advice on installing either LAMP for linux, Wamp for windows and MAMP for mac. AMP stands for apache, mysql and php. If you just need apache, you can download if from their website.
#8Â I just mentioned that web server delivers static web pages. However we see a lot of dynamic information on the web such as weather reports, date and time changes and user information updates. How are these dynamic information updated?
#9Â More examples that you are familiar with would be. Facebook news feed updates, likes and comments updates. These happen in near real time. I mean as soon as your friedns on FB updates you get their updates on your page.
#10Â User information update. You update your information and then its stored in the server, where you will be able to access or login whenever you get on these websites.
#11Â In order to perform these tasks, the data has to be sent from your browser as a request to the web server. The webserver call an appropriate script with the data and perfroms the requested functionality (adds/deletes/modifies or accesses the db) and responds to the request.
We need to note here that the request sends some data for the web server to work on.
#12Â For example, you put up your email id for registration and then this information is sent to the web server. If the email id is already registered which the web server checks in its database and responds that the email is already registered.
#13Â In order to get information to be updated dynamically on the web page. The frontend such as the browser has to send some data for the web server to respond appropriated. For this, we call the http methods on the client side either with Javascript of html forms. In the previous videos, we have learnt about JS, Ajax, JSON and XML. In this video we will learn a little about the HTML forms.
#14Â From the front end, one of these http methods can be called that is executed at the server. One is the GET method and the other is the POST method. The difference between get and post method is that, in the GET method data is encoded in the URL where as in the post method data goes with the body. An example is shown where the parameters are sent in the URL. If you use GET, the url with the data will be there in the history. Hence for sensitive information such as passwords it is necessary to use POST method where the data goes in the body. POST is also used to change the state. i.e. adding, deleting the data in the server db where as get is used for idempotent data, just retrieving the data.
Idempotent â making multiple requests and getting the same results from the web api. No change in state
#15Â As explained, HTML Forms are used to pass the data to the server. It has many features such as text fields, check box and radio button. To learn more about it you can go to the URL mentioned in the presentation.
#16Â This is an example, where where we call the method post using the destination url. The input type is a button with value click me.
#17Â Now lets get on to the technologies we can use at the server side, to respond to requests from the client side.
#18Â Although there are many, I will focus on a few of those in this video. CGI, Java Servlets and Php
#19Â CGI is common gateway interface. This is the first server side technology which was started in 1993 but formally defined in 1997. CGI is an interface which calls perl or python scripts at the server side for generating the response. The main drawback is scalability, each request is run as a different process.
#20Â Servlets are the Java technologyâs answer to CGI. It incorporates all the advantages of Java. However needs a servlet container. If you know Java, it is simply extending a servlet interface in which we define the get and post functions.
#21Â The servlet container is a component of web server. It manages the lifecycle of a servlet. It maps the url to appropriate servlets. It also manages many features such as security, concurrency and deployment. Some examples are, Apache Tomcat, Jboss and IBM Websphere.
#22Â Tomcat can be installed. Check these websites for it.
#23Â As you can see servlets are simple java programs that extends a HttpServlet and implements a doGet and/or a doPost method.
#24Â The HTTPSevletRequest and Response objects contains anything that is sent from the browser and has to be sent back to the browser
#28Â You can modify the URL and parameters for the servlets using the web.xml file.
#29Â Php which is an abbreviation for Hypertext preprocessor is also a popular server side language. It is also a dynamically typed language.
#30Â Php embeds html in it. So instead of referring to a html page url in the browser, we will type a .php page. The php code can be embedded using the following delimiters.
#31Â This is the hello world program. You can see how the Php code is embedded in the page.
#32Â They have a wide range of applications and there are many libraries which can be reused.
#33Â Similar to HTTPServletRequest and Response objects, you can access the form fields through array objects in Php
#35Â These are the differences between java and Php