Java Servlets and Java Server Pages Carol Wolf Computer Science.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

Java Servlets Java Server Pages (JSP)
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Servlets. A form The HTML source Chapter 1 Please enter your name and password then press start Name: Password: In Netbeans you can graphically create.
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.
CSE 190: Internet E-Commerce Lecture 7. HTML Templates Designed to separate server side logic from HTML presentation Key features –Escapes from HTML into.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Michael Brockway Application Integration Servlets l Introduction & Overview l HTTP Servlets l HTTP get Requests l HTTP post Requests l Multi-tier Applications.
Java Servlets and JSP.
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.
Servlet and JSP Programming: An Introduction Spiros Papadimitriou
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Examples of Using Servlets and JSP Representation and Management of Data on the Internet.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 30 - Servlets: Bonus for Java Developers Outline 30.1 Introduction 30.2 Servlet Overview and.
CSC 2720 Building Web Applications
Chapter 5 Java Servlets. Objectives Explain the nature of a servlet and its operation Use the appropriate servlet methods in a web application Code the.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
DataBases and SQL INFSY 547 Spring Course Wrap Up April 12: Complete Work on Servlets Review of Team Projects Close of Portfolio Work April 19:
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
MDCFUG Is Java in Your Future? Tyler Williams Principal dataTerrace
Java Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface Servlet and the Servlet Life Cycle HttpServlet Class.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaServer Page.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
JavaServer Page by Antonio Ko. Overview ► Introduction ► What is a servlet? ► What can servlets do? ► Servlets Vs JSP ► Syntax ► Samples ► JavaBean ►
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
 2002 Prentice Hall. All rights reserved. 9.8 Multi-Tier Applications: Using JDBC from a Servlet Three-tier distributed applications –User interface –Business.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 24: Servlets Outline 24.1 Introduction 24.2 Servlet Overview and Architecture Interface.
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.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Vakgroep Informatietechnologie – Onderzoeksgroep (naam) Web Centric Design of Distributed Software.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 43 JavaServer Page.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
1 Chapter 27 JavaServer Page. 2 Objectives F To know what is a JSP page is processed (§27.2). F To comprehend how a JSP page is processed (§27.3). F To.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
Introduction To HTML Dr. Magdi AMER. HTML elements.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
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.
S ERVLETS Form Data 19-Mar-16. F ORM P ROCESSING You must have come across many situations when you need to pass some information from your browser to.
JSP Java Server Pages. Hello, !
Java Database Connectivity JDBC. Open Database Connectivity developed by Microsoft to provide interaction with databases using SQL. Use the JDBC-ODBC.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CS3220 Web and Internet Programming Introduction to Java Servlets
COMP9321 Web Application Engineering Semester 2, 2017
JDBC & Servlet CSE 4504/6504 Lab.
Servlets and Java Server Pages
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Java Servlets Servlet Overview Servlets and HTML Forms Servlet Basics
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Presentation transcript:

Java Servlets and Java Server Pages Carol Wolf Computer Science

The Request Form Enter your name and address. Then click the Send button to send the data to the server. Name Address

The Servlet package echo; /* Servlet processes a request from a web page. It responds to the request by echoing back the name and address that was sent in. */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Servlet extends HttpServlet {

protected void doGet (HttpServletRequest request, HttpServletResponse response) {try {// Set the content type for the output and then get a PrintWriter object. response.setContentType ("text/html"); PrintWriter out = response.getWriter (); // Get the form data from the request. String name = request.getParameter ("name"); String = request.getParameter (" "); // Write the output header, the output data, and the footer. createHeader (out, "Test Data"); out.println (" Hello. "); out.println (" " + name+ " "); out.println (" Your address is " + + " "); createFooter (out); }catch (IOException e) {System.out.println ("Servlet Exception");} } // doGet

The Page File // createHeader adds standard HTML lines to the beginning of the output page. protected void createHeader (PrintWriter out, String title) { out.println (" "); out.println (" " + title + " "); out.println (" "); } // createHeader // createFooter adds standard HTML lines to the end of the output page. protected void createFooter (PrintWriter out){out.println (" ");} } // Servlet

The Web Application Deployment Descriptor Servlet echo. Servlet Servlet /servlet/echo. Servlet

The Response Page

Java Server Pages  Kinds of tags  is used for expressions.  is used for declarations.  is used for straight Java code.  is used to include another file such as an HTML file or a package such as java.sql.*.  Some reserved words  request – an instance of HttpServletRequest.  response – an instance of HttpServletResponse.  out – a PrintWriter object for the response.  session – the HttpSession object associated with the session.  application – an instance of ServletContext

Java Beans  Each Java server page is associated with a Java bean.  These are Java programs and reside on the server.  The constructor has no parameters  All variables have accessor (get) and mutator (set) methods.  Example with action hello.jsp Hello Enter your name and address: Name

JSP File Hello JSP <jsp:setProperty name="hello" property="name" value=' ‘ /> <jsp:setProperty name="hello" property=" " value=‘ ‘ /> <% name = hello.getName(); = hello.get (); out.println (" Hello, your name is " + name); out.println (" and your address is " + + ". "); %>

The Bean  This is a very simple bean with just getters and setters. public class HelloBean { private String name = ""; private String = ""; public String getName() {return name;} public String get () {return ;} public void setName (String n) {name = n;} public void set (String e) { = e;} } // HelloBean

Example to find a name in a database Find Address Enter the name : Name

The JSP File Find Address JSP The requested address: The name was not in the database.

The Bean package address_book; import java.sql.*; // FindBean is a Java bean that is used to locate an address in a database. public class FindBean { private String name, , telephone; private boolean found; // The accessor and mutator methods. public String getName() {return name;} public String get () {return ;} public String getTelephone () {return telephone;} public boolean getFound () {return found;} public void setName (String n) {name = n;}

More Bean /* processRequest connects to the database, gets a ResultSet, and stores the data in the instance variables. */ public void processRequest () {try { // Get a jdbc-odbc bridge and connect to addresses.mdb. Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:addresses"); // Create a query and get a ResultSet. Statement stmt = con.createStatement (); String query = "Select * From AddressTable Where Name = '" + name + "'"; ResultSet rs = stmt.executeQuery (query);

Rest of Bean // If the name is in the database, store the address in the instance variables. if (rs.next ()) {name = rs.getString ("Name"); = rs.getString (" "); telephone = rs.getString ("Telephone"); found = true; } // If the address was not found, set the value of the variable found to false. else found = false; } catch (ClassNotFoundException e) {System.out.println ("Class Not Found Exception.\n");} catch (SQLException e){System.out.println ("SQL Exception");} } // processRequest } // FindBean

The Web Application Deployment Descriptor org.apache.jsp.find_jsp and org.apache.jsp.find_jsp /find/*