HTTP. HTTP HTML Parsing. Java

Similar documents
"protocol stack! chat. Java

Advanced Internet Technology Lab # 4 Servlets

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

Session 9. Introduction to Servlets. Lecture Objectives

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

Session 8. Introduction to Servlets. Semester Project

Servlets and JSP (Java Server Pages)

Java Servlets. Preparing your System

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

COURSE DETAILS: CORE AND ADVANCE JAVA Core Java

Servlet Fudamentals. Celsina Bignoli

JAVA SERVLET. Server-side Programming INTRODUCTION

SSC - Web applications and development Introduction and Java Servlet (I)

JavaServer Pages (JSP)

COMP9321 Web Application Engineering

Type of Classes Nested Classes Inner Classes Local and Anonymous Inner Classes

COMP9321 Web Application Engineering

HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests

Servlets. An extension of a web server runs inside a servlet container

Servlet and JSP Review

Servlet Basics. Agenda

Page 1

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

Application Development in JAVA. Data Types, Variable, Comments & Operators. Part I: Core Java (J2SE) Getting Started

Database Applications Recitation 6. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Backend. (Very) Simple server examples

Getting started with Winstone. Minimal servlet container

Session 8. JavaBeans. Reading & Reference. Reading. Reference. Session 8 Java Beans. 2/27/2013 Robert Kelly, Head First Chapter 3 (MVC)

Session 20 Data Sharing Session 20 Data Sharing & Cookies

Servlets Basic Operations

Servlet. 1.1 Web. 1.2 Servlet. HTML CGI Common Gateway Interface Web CGI CGI. Java Applet JavaScript Web. Java CGI Servlet. Java. Apache Tomcat Jetty

JAVA SERVLET. Server-side Programming PROGRAMMING

Get the cookies from the service request: Cookie[] HttpServletRequest.getCookies() Add a cookie to the service response:

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

Introduction to Java Servlets. SWE 432 Design and Implementation of Software for the Web

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

CIS 3952 [Part 2] Java Servlets and JSP tutorial

Component Based Software Engineering

Advanced Web Technology

Networking Basics. network communication.

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

DVS WEB INFOTECH DEVELOPMENT TRAINING RESEARCH CENTER

Java4570: Session Tracking using Cookies *

Advanced Java Programming

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

3. The pool should be added now. You can start Weblogic server and see if there s any error message.

Distributed Systems COMP 212. Lecture 8 Othon Michail

Introduction. This course Software Architecture with Java will discuss the following topics:

Module 4: SERVLET and JSP

Networking Code CSCI 201 Principles of Software Development

Course Content for Java J2EE

Oracle 10g: Build J2EE Applications

Cloud Computing Platform as a Service

Enterprise Java Unit 1- Chapter 4 Prof. Sujata Rizal Servlet API and Lifecycle

ADVANCED JAVA COURSE CURRICULUM

Java J Course Outline

JAVA Training Overview (For Demo Classes Call Us )

Unit-4: Servlet Sessions:

UNIT-V. Web Servers: Tomcat Server Installation:

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

Web based Applications, Tomcat and Servlets - Lab 3 -

AN ISO 9001:2008 CERTIFIED COMPANY ADVANCED. Java TRAINING.

CS506 Web Design & Development Final Term Solved MCQs with Reference

J2EE Web Development 13/1/ Application Servers. Application Servers. Agenda. In the beginning, there was darkness and cold.

COMP9321 Web Application Engineering

CE212 Web Application Programming Part 3

ICOM 5016 Database Systems. Database Users. User Interfaces and Tools. Chapter 8: Application Design and Development.

Chapter 2 How to structure a web application with the MVC pattern

Steps for Implementing a Server

Core Java. Basics of Java. String Handling. OOPS Concepts. Exception Handling

MARATHWADA INSTITUTE OF TECHNOLOGY, AURANGABAD DEPARTMENT OF MASTER OF COMPUTER APPLICATIONS ADVANCE JAVA QUESTION BANK

Network Programming: Servers

This course is intended for Java programmers who wish to write programs using many of the advanced Java features.

Servlets by Example. Joe Howse 7 June 2011

SERVLETS INTERVIEW QUESTIONS

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

Session 10. Form Dataset. Lecture Objectives

1Z Java EE 6 Web Component Developer Certified Expert Exam Summary Syllabus Questions

********************************************************************

Construction d Applications Réparties / Master MIAGE

Lab session Google Application Engine - GAE. Navid Nikaein

Fast Track to Java EE 5 with Servlets, JSP & JDBC

CPSC 441 Tutorial TCP Server. Department of Computer Science University of Calgary

Introduction. This course Software Architecture with Java will discuss the following topics:

Chapter 17. Web-Application Development

Distributed Systems 1

J2EE Development. Course Detail: Audience. Duration. Course Abstract. Course Objectives. Course Topics. Class Format.

/ / JAVA TRAINING

JSP. Common patterns

Table of Contents. Introduction... xxi

Three hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 21 st May Time:

STLDD Software Top Level Design Document: BaseBlockSystem

Advanced Java Programming. Networking

LTBP INDUSTRIAL TRAINING INSTITUTE

JAVA MICROSERVICES. Java Language Environment. Java Set Up. Java Fundamentals. Packages. Operations

CSC309: Introduction to Web Programming. Lecture 10

Java Training Center, Noida - Java Expert Program

Accessing EJB in Web applications

Transcription:

ђѕђяѡъ ьэющ

HTTP

HTTP TCP/IP HTTP HTTP HTML Parsing

HTTP HTTP GET <filename> <version> < > web servers GET

HTTP Port 80 HTTP GOOGLE HTML

HTTP Port 80 HTTP URL (Uniform Resource Locator)

HTTP URL http://www.cs.tau.ac.il:80/cs/index.html protocol host port file

HTTP GET Request

HTTP GET Response

HTTP HTML Gmail http://www.google.co.il/search?hl=iw&q=cow&btn&meta= HTTP

framework web container servlets Tomcat, OC4J

Web Container GET web container

Web Container service doget

Web Container accept Thread session, context

Web Container HTTP HttpServletRequest HttpServletResponse request HTTP Servlet response HTTP

GET

Servlet Life Cycle

HttpServletRequest, HttpServletResponse public interface HttpServletRequest { public java.util.set<string> getparameternames(); public String getparameter(string name); public interface HttpServletResponse { public java.io.printwriter getwriter(); public void setcontenttype(string type);

public class ServletInvocation implements HttpServletRequest, HttpServletResponse { private java.util.map<string,string> parameters; private PrintWriter writer; public ServletInvocation(java.util.Map<String,String> parameters, PrintWriter writer) { this.parameters = parameters; this.writer = writer; public String getparameter(string name) { return parameters.get(name); public Set<String> getparameternames() { return parameters.keyset(); public PrintWriter getwriter() { return writer; public void setcontenttype(string type) { writer.print("http/1.1 200\r\n"); writer.print("content-type: "+type+"\r\n"); writer.print("connection: close\r\n"); writer.print("\r\n"); // end of header

TrivialServlet HttpServer TrivialServlet aspects hook doget Component web container

TrivialServlet package examples.servletserver; public class TrivialServlet extends HttpServlet { public TrivialServlet() { @Override protected void doget(httpservletrequest req, HttpServletResponse resp) { java.io.printwriter writer = resp.getwriter(); resp.setcontenttype("text/plain"); java.util.set<string> parameters = req.getparameternames(); writer.println("this is servlet "+this.getclass().getname()); for (String pname: parameters) { writer.println("parameter "+pname+" = "+req.getparameter(pname));

public class FileServlet extends HttpServlet { public FileServlet() { @Override protected void doget(httpservletrequest req, HttpServletResponse resp) { java.io.printwriter writer = resp.getwriter(); String path = req.getparameter("path"); if (path == null) return; // we really should be more clever if (path.endswith(".html")) resp.setcontenttype("text/html"); else resp.setcontenttype("text/text"); Servlet try { BufferedReader in = null; String servlet = null; java.util.map<string,string> parameters = null; in = new BufferedReader(new InputStreamReader(new FileInputStream(path))); while (true) { String line = in.readline(); if (line==null) break; // end of file writer.println(line); catch (IOException ioe) { System.err.println(ioe.getMessage());

ChatServlet Servlet Singletons ChatServlet form html

public class ChatServlet extends HttpServlet { private String[] lastmessages = new String[10]; private String[] lastnames = new String[10]; public ChatServlet() { @Override protected void doget(httpservletrequest req, HttpServletResponse resp) { java.io.printwriter writer = resp.getwriter(); String name = req.getparameter("name"); String text = req.getparameter("text"); resp.setcontenttype("text/html"); if (text!= null && text.length() > 0) { for (int i=9; i>0; i--) { System.out.println("i="+i); lastmessages[i] = lastmessages[i-1]; lastnames [i] = lastnames [i-1]; lastmessages[0]= text; lastnames[0] = name == null name.length()==0? "anonymous" : name;

writer.println("<html><head>"); writer.println("<title>chat Servlet</TITLE>"); writer.println("</head><body>"); writer.println("<h1>let's Chat!</H1>"); writer.println("<form name=\"input\" action=\"" +this.getclass().getname() +"\" method=\"get\">"); writer.println("name: <input type=\"text\" name=\"name\""); if (name!= null && name.length() > 0) writer.println(" value=\""+name+"\">"); else writer.println(">"); writer.println(" Message: <input type=\"text\" name=\"text\">"); writer.println(" <input type=\"submit\" value=\"send\">"); writer.println("</form>"); for (int i=0; i<10; i++) if (lastnames[i]!= null && lastmessages[i]!= null) writer.println("<p>"+lastnames[i]+": " +lastmessages[i]+"</p>"); writer.println("</body></html>");

Singletons HttpServlet public abstract class HttpServlet { private static java.util.map<string,httpservlet> servlets = new java.util.treemap<string,httpservlet>(); static HttpServlet getinstance(string class_name) { HttpServlet servlet = servlets.get(class_name); if (servlet!= null) return servlet; try { java.lang.reflect.constructor constructor = Class.forName(class_name).getConstructor(new Class[] { ); servlet = (HttpServlet) constructor.newinstance(new Object[] { ); catch (Exception e) { return null; servlets.put(class_name,servlet); return servlet; protected HttpServlet() { protected abstract void doget(httpservletrequest req, HttpServletResponse resp);

HttpServer header accept GET 1 2 1 2 Servlet 1 getinstance HttpServletRequest HttpServletResponse 2 3 doget 4

public class HttpServer { public static void main(string[] arguments) { int port = 8888; ServerSocket server_socket = null; // dummy initialization try { server_socket = new ServerSocket(port); catch (IOException ioe) { System.out.println("Failed to bind to port "+port+": "+ioe.getmessage()); System.exit(1); while (true) { Socket connection_socket = null; try { connection_socket = server_socket.accept(); catch (IOException ioe) { System.out.println("Accept failed: "+ioe.getmessage()); System.exit(1); BufferedReader in = null; String servlet = null; java.util.map<string,string> parameters = null; try { in = new BufferedReader( new InputStreamReader(connection_socket.getInputStream()));

String line; while (true) { line = in.readline(); if (line==null) break; if (line.length() == 0) break; if (line.startswith("get ")) { String get_params[] = line.split(" "); String path = get_params[1]; servlet = path.substring(1); int index_of_question = servlet.indexof('?'); int index_of_slash = servlet.indexof('/'); parameters = new java.util.treemap<string,string>(); if (index_of_question!= -1) { String params = servlet.substring(index_of_question+1); String[] params_values = params.split("&"); for (String pv: params_values) { String[] p_and_v=pv.split("="); parameters.put(p_and_v[0], p_and_v.length==2? p_and_v[1] : ""); servlet = servlet.substring(0,index_of_question); else if (index_of_slash!= -1) { String path_param = servlet.substring(index_of_slash+1); parameters.put("path",path_param); servlet = servlet.substring(0,index_of_slash);

PrintWriter out = null; try { out = new PrintWriter(connection_socket.getOutputStream()); catch (IOException ioe) { System.out.println("Could not send the output: "+ioe.getmessage()); System.exit(1); HttpServlet s = HttpServlet.getInstance(servlet); if (s!= null) { ServletInvocation invocation = new ServletInvocation(parameters,out); s.doget(invocation,invocation); else { out.print("http/1.1 404\r\n"); out.print("content-type: text/html\r\n"); out.print("connection: close\r\n"); out.print("\r\n"); // end of header out.println("<html><head>"); out.println("<title>404 Not Found</TITLE>"); out.println("</head><body>"); out.println("<h1>not Found</H1>"); out.println("<p>the requested URL was not found on this server.</p>"); out.println("</body></html>"); try { out.close(); in.close(); connection_socket.close(); catch (IOException ioe) { // who cares?

J2EE Web Containers Web Apache Tomcat Sun System Web Server Oracle Containers for (OC4J Many more Servlet Servlets Servlet 2.4 Specification JSR-000154 TM

Servlet Framework

Hello World Servlet

forms

GET

POST

GET vs. POST

Session Management Web HTTP Container HttpSession

Session Management Strategies Cookies name=value URL Rewriting HTML href

Web Container Aspects Sessions Web Container Aspect Servlet URL Filters Aspect Filter Filter Filter Filter

Data Access Object Pattern

Data Access Object Pattern The data access object (DAO) pattern separates the business logic from the data access (data storage) logic The data access implementation (usually JDBC technology calls) is encapsulated in DAO classes. The DAO pattern permits the business logic and the data access logic to change independently. For example, if the DB schema changes, then you would only need to change the DAO methods, and not the business services or the domain objects.

/ HTML HTML HTML Model/View Separation html JSP JSP Servlet JSP

JSP

Servlet JSP

JSP Page Life Cycle

JSP / JSP HTML JSP JSP HTML JSP JSP JSP Beans JSP

JSP Custom Tags JSP

Job Roles Web Applications

Model 2 Framework

Types of View Components

Types of Controller Components

Entity Component Limitations

MVC Using Struts Web Container MVC Controller Controller View Controller Controller Action Controller Struts Framework Controller

Web Frameworks Frameworks Servlets Frameworks Art of Web Development Struts, Tapestry, Commons, Velocity, JUnit, Axis, Cocoon, InternetBeans, WebWork Neal Ford