Advanced Java Session 6 New York University School of Continuing and Professional Studies.

Slides:



Advertisements
Similar presentations
Servlets & JSPs - Sharad Ballepu.
Advertisements

Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
JSP and Servelets.
CGI programming. Common Gateway Interface interface between web server and other programs (cgi scripts) information passed as environment variables passed.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
Java Server Pages (JSP)
JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
Servlets CS-328 Dick Steflik. What is a servlet A Java application run on a thread of the webserver in response to an HTTP GET or POST request. The servlet.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Web programming for project students Dr Jim Briggs.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
Definition Servlet: Servlet is a java class which extends the functionality of web server by dynamically generating web pages. Web server: It is a server.
Servlets Compiled by Dr. Billy B. L. Lim. Servlets Servlets are Java programs which are invoked to service client requests on a Web server. Servlets extend.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
Java Servlets. What Are Servlets? Basically, a java program that runs on the server Basically, a java program that runs on the server Creates dynamic.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Objectives Java Servlet Web Components
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Java Servlets & Java Server Pages Lecture July 2013.
1 CS122B: Projects in Databases and Web Applications Spring 2015 Notes 03: Web-App Architectures Professor Chen Li Department of Computer Science CS122B.
Servlets O. De Pertat. Servlets Overview Generic Server Business logic API Java Syntax: classes extending the javax.servlet.Servlet interface or any sub-class.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
Introduction to Server-Side Web Development Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets.
Java Servlets and Java Server Pages Norman White Stern School of Business.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
Servlets.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
Advanced Java Session 6 New York University School of Continuing and Professional Studies.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
® IBM Software Group © 2007 IBM Corporation Servlet API (Part II)
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Chapter 4 Request and Response. Servlets are controlled by the container.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
1 Lecture 8 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
CS 562 Advanced Java and Internet Application Computer Warehouse Web Application By Team Alpha :-  Puja Mehta (102163)  Mona Nagpure (102147)
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
Java Servlets References: Karen Anewalt, Mary Washington College.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Pre-assessment Questions
Chapter 26 Servlets.
CS122B: Projects in Databases and Web Applications Winter 2018
CS122B: Projects in Databases and Web Applications Spring 2018
Servlet APIs Every servlet must implement javax.servlet.Servlet interface Most servlets implement the interface by extending one of these classes javax.servlet.GenericServlet.
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Advanced Java Session 6 New York University School of Continuing and Professional Studies

2 Objectives Web Application Architecture and HTTP Java Servlets –BasicServlet –Accessing Form Data, HTTP Headers, CGI Variables –Cookies and Sessions Java Server Pages –Overview of Java Server Pages –Using Java Beans in JSP Integrating Java Servlets and JSP

3 Web Applications Architecture client Web server App server Backend Services - db, quote server IE, safari,.. Apache, IIS,.. Tomcat, jBoss, WebLogic, WebSphere, IIS,.. Tier 1 Tier 2Tier 3

4 HTTP Client makes an HTTP request – – : / / Server simply picks up the document and returns it back to the client

5 Sample Request GET / / HTTP/1.0 User-agent: Internet Explorer Host: mymachine Request Headers End of Headers

6 Sample Response HTTP/ OK Content-Length: 1433 Content-Type: text/html Last-Modified: Fri, 21 Feb :48:30 GMT Server: Microsoft-IIS/6.0 Date: Sun, 08 Jul :08:52 GMT Under Construction Headers End of Headers Body

7 CGI Common Gateway Interface allows a response to be generated by a program Server simply invokes the program and passes it the “socket stream”. Program writes the response to the stream – and thereby to the client Parameters are sent on the URL (or in the body in POST) GET POST - body after the headers will contain “q=servlets” Special characters must be “encoded”

8 Java Servlets Modern replacement for CGIs Server components written in Java Advantages of Servlets –Efficient –State Management –Portable –Robust –Extensible –Quick Development –Widespread Acceptance

9 Application Server Model services Servlet container servlets

10 Java Servlets Are not complete programs with a “main” Run inside a “container” such as Tomcat, or WebLogic When the “container” receives the first request for a servlet, it creates an instance of it and calls its “init” method and then calls the “service()” method which in turn calls doGet() or doPost All subsequent requests directly call the service() method (doGet method or doPost method depending on the type of request) If multiple requests are received simultaneously then the container creates multiple threads and calls the “same” instance of the servlet from multiple threads Current Version of the Specification is 2.5

11 Java Servlets Container GET /myservlet Load class myservlet Constructor init() service() destroy() (depends)

12 Classes and Interfaces Servlet ServletConfig ServletContext GenericServlet ServletRequest ServletResponse HttpServlet HttpServletRequest HttpServletResponse HttpSession Cookie

13 GenericServlet Abstract class public void init() throws ServletException public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException public void destroy()

14 HttpServlet Abstract class protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

15 BasicServlet example tomcat env javac BasicServlet.java servit BasicServlet.class tomcat start

16 Accessing Form Data String getParameter(String) Enumeration getParameterNames() ShowParameters.java

17 Accessing HTTP Headers String getMethod() String getRequestURI() String getProtocol() Enumeration getHeaderNames() ShowRequestHeader.java

18 Accessing CGI Variables String getAuthType() String getContentLength() String getContentType() ……. More complete list is in the handout. ShowCGIVariables.java

19 Generating Response setContentType() getWriter() …… More in the handout.

20 Handling Sessions Object getAttribute(String name) void setAttribute(String name, Object value) Enumeration getAttributeNames() void removeAttribute(String name) void Invalidate() ShowSession.java

21 Java Server Pages Use embedded java code in HTML files “automatically generated” servlet Part of the J2EE framework Supported by most Application Servers Current version of specification is 2.1

22 Jsp Servlet Java Server Pages turn into a servlet Engine translates JSP into a java servlet, compiles it and executes it. HttpJspPage interface is a subclass of Servlet JSP engine provides a class that implements HttpJspPage interface _jspService method is the main entry point

23 Embedded Tags

24 Embedded Java code hello item number

25 page directives Language Import contentType errorPage Extends isErrorPage isThreadSafe Session autoFlush Buffer

26 Using Java Beans in Java Server Pages Basic Bean Use

27 Accessing Bean Properties <jsp:setProperty name=“book1” property=“title” value=“Developing Applications in J2EE”/>

28 Associating properties with HTML input parameters BookEntry example

29 Extending JSP with custom tags body text

30 Making a Tag Handler Write a class that extends SimpleTagSupport Implement doTag() method Implement any “set…” methods if your tag expects attributes (e.g setAttr1) Create a TLD (tag library descriptor) for the tag Deploy the tag handler and the TLD (put it in WEB-INF/classes using the package directory structure)

31 Integrating Servlets and Java Server Pages Use servlets for presentation logic User jsp for layout Jsp can also generate XML Examples: ShowPage.java ShowError.java

32 Web Applications Collection of Servlets, JSPs, other Java Classes, HTML pages, CSS, images, and any other elements (such as Flash, Applets, and others) Packaged together in a single “war” file for easy transportability and deployment

33 Attributes “Temporary” storage for Servlet and JSP components Can be placed in ServletContext, Request, or Session Any object can be placed using the setAttribute( key, value ) call and can be reetrieved using the getAttribute(key) call

34 Scopes ServletContext: available throughout your application to all components of your application Request: available only during the processing of a request to the components which have the “request” object Session: available to all components of your application during its lifetime that begins with the first request from a client (end is determined by configuration)

35 HttpSessionBindingListener interface Classes that are used to “store” temporary data using setAttribute can implement this interface. valueBound is called when an instance is “bound” to an active HttpSession valueUnbound is called when an instance is “unbound” from a HttpSession – in other words – when the “session” is closed or is destroyed

36 Thread Safety issues Servlets are called from multiple threads by the container – therefore must be coded in a Thread Safe manner All access to context variables must be “synchronized” All access to session variables must also be “synchronized”

37 Clustering Issues Application Servers may be “clustered” – i.e. more than one server may be set up to service clients In that event, sessions must be migrated to all “active” servers

38 Thank you