2. Preliminaries
• HTML
– Standard mark up language to create web pages
• CSS
– Style sheets for web pages
• Javascript
– Client side programming
• Java, Perl, Python, Php
3. What are web servers
• Hardware or software
– Helps deliver the web content that can be
accessed through internet (Wikipedia)
Web Server
File System
Request
Response
Read Static
Files
5. 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
7. Web servers
• Delivers static 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
12. Frontend
• Data has to 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
• Pass Data to the server
– Text fields, check box, radio button, submit button
etc.
• For More
– http://www.w3.org/TR/html401/interact/forms.ht
ml
18. CGI
• Common Gateway Interface (CGI)
– Runs a scripts for every request – New process
• Drawbacks
– Limited Functionality
– Scalability Issues
19. Servlets
• Java Programming Class 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
• Component of 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
22. Plain HTML Text Servlet
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 how to generate a simple
HTML
<!DOCTYPE html>
<html lang="en">
<head>
..
</head>
<body>
...
</body>
</html>
25. Servlet that generates HTML
@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>");
}
}
27. Php
• General purpose server-side scripting
language originally designed for web
development to produce dynamic web pages
• Hypertext Preprocessor
• Dynamically Typed Language
28. Php – Hello World
• Embed PHP script into an HTML file
• Upload the file onto a Web server using
extension .php (Apache)
• Embed using the following delimiters
– < ? ... ? >
– <?php ... ?>
– <script language=”php”> ... </script>
– <% ... %>
30. Php Applications
• Wide range 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 fields 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 numbers until
“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>
36. 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
Hello, This video we will be talking about server side web programming for the Web Information Systems class of 2015
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.
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.
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.
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.
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.
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?
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.
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.
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.
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.
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.
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
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.
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.
Now lets get on to the technologies we can use at the server side, to respond to requests from the client side.
Although there are many, I will focus on a few of those in this video. CGI, Java Servlets and Php
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.
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.
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.
Tomcat can be installed. Check these websites for it.
As you can see servlets are simple java programs that extends a HttpServlet and implements a doGet and/or a doPost method.
The HTTPSevletRequest and Response objects contains anything that is sent from the browser and has to be sent back to the browser
You can modify the URL and parameters for the servlets using the web.xml file.
Php which is an abbreviation for Hypertext preprocessor is also a popular server side language. It is also a dynamically typed language.
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.
This is the hello world program. You can see how the Php code is embedded in the page.
They have a wide range of applications and there are many libraries which can be reused.
Similar to HTTPServletRequest and Response objects, you can access the form fields through array objects in Php