330 likes | 416 Views
Web programming for project students. Dr Jim Briggs. What is the web?. Distributed system Client-server system Characteristics of clients and servers Servers always on / Clients choose when on Clients do not need high performance if the work is done on the server Protocol based.
E N D
Web programmingfor project students Dr Jim Briggs
What is the web? • Distributed system • Client-server system • Characteristics of clients and servers • Servers always on / Clients choose when on • Clients do not need high performance if the work is done on the server • Protocol based
Browsers Microsoft Internet Explorer Netscape Navigator Mozilla (Firefox) Opera Safari Konqueror Lynx Servers Apache Internet Information Server (Microsoft) Sun Netscape Omni Roxen NCSA Common web tools
HTTP protocol • Specified by • http://www.w3.org/Protocols/rfc2616/rfc2616.html • Based on requests and responses • A response can contain any document • MIME (Multipurpose Internet Mail Extensions) types • http://www.iana.org/assignments/media-types/ • A stateless protocol • Normally transported via a TCP/IP connection • Default port is TCP 80
HTTP requests • Requests • GET • POST • PUT • HEAD • Example request GET http://www.port.ac.uk/index.htm HTTP/1.1
HTTP responses HTTP/1.1 200 OK Server: Microsoft-IIS/4.0 Date: Mon, 29 Apr 2002 08:50:53 GMT Content-Type: text/html Accept-Ranges: bytes Last-Modified: Wed, 10 Apr 2002 16:12:34 GMT ETag: "085fb85aae0c11:54fb" Content-Length: 13845 <HTML> <HEAD> <TITLE>University of Portsmouth - Our University</TITLE> ...
An error response HTTP/1.1 404 Object Not Found Server: Microsoft-IIS/4.0 Date: Mon, 29 Apr 2002 08:58:12 GMT Content-Length: 11891 Content-Type: text/html <HTML> <HEAD> <TITLE>University of Portsmouth - Our University</TITLE> ...
Dynamic web pages • Four models: • Server-side includes • CGI • Server modules • Auxiliary servers
CGI specification • Specified at http://hoohoo.ncsa.uiuc.edu/cgi/
Common web languages • Java/JSP • ASP (.NET) / C# • PHP • Perl • Ruby on Rails
Introduction to Java web apps • Servlets and JSPs • Servlet container • Tomcat • A container may run several (independent) web applications (webapps) • Each must have a WEB-INF directory: • web.xml configuration file • classes directory • lib directory
Important classes and interfaces 1 • All servlets must implement the Servlet interface • Class HttpServlet • init/destroy • doGet/doPut • Your servlet will derive from this
Important classes and interfaces 2 • 2 parameters to a request handling method • Class HttpServletRequest • String param = request.getParameter(name); • Class HttpServletResponse • PrintWriter out = response.getWriter(); • Class HttpSession • Holds data common to related requests
JavaServer Pages (JSP) • Distinction: • servlets: HTML embedded in program • JSP: program embedded in HTML • Useful where majority of effort is page design • Translated automatically into a servlet • Retranslated if changed (no need to restart server) • Can be placed anywhere in a web application • but not visible to client if in the WEB-INF directory
JSP elements • Scriptlets • Actions • Directives • Standard tags • Custom tags • Expression language
Scriptlets • Any Java code between <% … %> • Expressions • <%= name %> • Declarations • <%! String name %> • DEPRECATED • Do not use - not XML • Much easier to use JSTL
JSP actions • Including other files • <jsp:include page="path"/> • Request time inclusion • Accessing beans • <jsp:usebean id="beanName" class="package.class" scope="session"/> • <jsp:getproperty name="beanName" property="propertyName"/> • <jsp:setproperty name="beanName" property="propertyName" value="newValue"/>
JSP directives • Page directive • <%@page import="package.class"%> • <%@page contentType="text/html"%> • <%@page errorPage="URL"%> • Include directive • <%@include file="filename"%> • Translation time inclusion
Java Standard Tag Library (JSTL) • Taglib directive • <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> • Core • <c:out value="${anExpression}"/> • SQL • XML • Format
Custom tags • Implement your own tags • Create a Tag Library Definition (tld) file • Extend predefined classes • Specify your library in a @taglib directive • Use like JSTL tags
Expression language • Refer to Java Beans and other common classes • ${expression} can appear as tag attributes or (since JSP 2.0) in the page itself • Several implicit objects: • header • ${header["user-agent"]} • param • ${param['name']} • ${param.name}
Java tools • NetBeans • IDE • edit, compile, build, run, debug • lots of wizards • support for Struts • support for Hibernate (through nbxdoclet) • Eclipse