37
Server Side Web Programming Web Information Systems 2015

Lecture 7: Server side programming

Embed Size (px)

Citation preview

Page 1: Lecture 7: Server side programming

Server Side Web Programming

Web Information Systems 2015

Page 2: Lecture 7: Server side programming

Preliminaries

• HTML– Standard mark up language to create web pages

• CSS– Style sheets for web pages

• Javascript– Client side programming

• Java, Perl, Python, Php

Page 3: Lecture 7: Server side programming

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

Page 4: Lecture 7: Server side programming

knoesis.org(Web Server)

http://knoesis.org/amit

Page 5: Lecture 7: Server side programming

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

Page 6: Lecture 7: Server side programming

Apache Download

• Packages – LAMP -- http://www.lamphowto.com/

– WAMP -- http://www.wampserver.com/en/

– MAMP -- http://sawmac.com/mamp/

• Apache Installation– Ubuntu – sudo apt-get install apache2/Synaptic

Package manager

– Windows and Mac – Download Apache • http://httpd.apache.org/download.cgi

Page 7: Lecture 7: Server side programming

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

Page 8: Lecture 7: Server side programming

Friends Likes and Comments

Friends Updates

Facebook

Page 9: Lecture 7: Server side programming

Forms with Values to input

Page 10: Lecture 7: Server side programming

Dynamic Web Pages

Web Server

Script

Request

Response

DB

Page 11: Lecture 7: Server side programming

Email already registered

Page 12: Lecture 7: Server side programming

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

Page 13: Lecture 7: Server side programming

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

Page 14: Lecture 7: Server side programming

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.html

Page 15: Lecture 7: Server side programming

Example

<BODY>

<FORM action=”destination script/url” method="post">

<INPUT type=“submit” value=" submit” >

</FORM>

</BODY>

Page 16: Lecture 7: Server side programming

Technologies at the Server Side

Page 17: Lecture 7: Server side programming

Technologies

• CGI

– Common Gateway interface

• Servlets – Need to know Java

• Php

– Hypertext Preprocessor

Page 18: Lecture 7: Server side programming

CGI

• Common Gateway Interface (CGI)

– Runs a scripts for every request – New process

• Drawbacks

– Limited Functionality

– Scalability Issues

Page 19: Lecture 7: Server side programming

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

Page 20: Lecture 7: Server side programming

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

Page 21: Lecture 7: Server side programming

Tomcat Installation

• Follow

– http://tomcat.apache.org/tomcat-7.0-doc/appdev/installation.html

• Eclipse + Tomcat

– http://www.coreservlets.com/Apache-Tomcat-Tutorial/tomcat-7-with-eclipse.html

Page 22: Lecture 7: Server side programming

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");

}

}

Page 23: Lecture 7: Server side programming

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

Page 24: Lecture 7: Server side programming

Lets see how to generate a simple HTML

<!DOCTYPE html><html lang="en"><head>..</head><body>...</body></html>

Page 25: Lecture 7: Server side programming

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>");

}

}

Page 26: Lecture 7: Server side programming

Modifying web.xml<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4”... >

<servlet>

<servlet-name>TestServlet</servlet-name>

<display-name>Print Text</display-name>

<servlet-class>testPackage.TestServlet</servlet-class>

<init-param>

<param-name> param1 </param-name>

<param-value> value1 </param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>TestServlet</servlet-name>

<url-pattern>/test2</url-pattern>

</servlet-mapping>

</web-app>

Passing parameters

Custom URLs

Page 27: Lecture 7: Server side programming

Php

• General purpose server-side scripting language originally designed for web development to produce dynamic web pages

• Hypertext Preprocessor

• Dynamically Typed Language

Page 28: Lecture 7: Server side programming

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>

– <% ... %>

Page 29: Lecture 7: Server side programming

Php – Hello World

<html><body>

<?phpecho "Hello World";?>

</body></html>

Delimiters – seperates PHP to non-PHP code

<?php ?>

Page 30: Lecture 7: Server side programming

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

Page 31: Lecture 7: Server side programming

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.

Page 32: Lecture 7: Server side programming

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>

Page 33: Lecture 7: Server side programming
Page 34: Lecture 7: Server side programming

References

• Common– http://coronet.iicm.edu/lectures/mmis/material/s

lides_serverside_main.pdf

• Servlets– http://courses.coreservlets.com/Course-

Materials/pdf/csajsp2/02-Servlet-Basics.pdf

• Php– http://www.php.net/

– http://www.w3schools.com/php/

Page 35: Lecture 7: Server side programming

Friends Likes and Comments

Friends Updates

Partial Changes

Page 36: Lecture 7: Server side programming

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

Page 37: Lecture 7: Server side programming

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