SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics.

Slides:



Advertisements
Similar presentations
4 Copyright © 2005, Oracle. All rights reserved. Creating the Web Tier: Servlets.
Advertisements

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.
Apache Tomcat as a container for Servlets and JSP
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.
Introduction to Servlets Based on: Hall, Brown, Core Servlets and JavaServer Pages.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
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.
10-Jun-15 Servlets. 2 Servers A server is a computer that responds to requests from a client Typical requests: provide a web page, upload or download.
An introduction to Java Servlet Programming
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Core Servlets Chapter 3 Link for Core Servlets code: om/archive/ om/archive/
1 CS6320 – Servlet Structure and Lifecycle L. Grewe.
Introduction to Java Servlets Vijayan Sugumaran School of Business Administration Oakland University Note: Some of the slides were imported from
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Java Server and Servlet CS616 Team 9 Kim Doyle, Susan Kroha, Arunima Palchowdhury, Wei Xu.
Server Side Programming Web Information Systems 2012.
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 Life Cycle. The Servlet Life Cycle A servlet life cycle can be defined as the entire process from its creation till the destruction. The following.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
A Servlet’s Job Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit.
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.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
AN OVERVIEW OF SERVLET TECHNOLOGY SERVER SETUP AND CONFIGURATION WEB APPLICATION STRUCTURE BASIC SERVLET EXAMPLE Java Servlets - Compiled By Nitin Pai.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
CSC 2720 Building Web Applications
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
111 Java Servlets Dynamic Web Pages (Program Files) Servlets versus Java Server Pages Implementing Servlets Example: F15 Warranty Registration Tomcat Configuration.
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.
Servlet Lifecycle Lec 28. Servlet Life Cycle  Initialize  Service  Destroy Time.
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.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
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.
S ERVLETS Hits Counter 21-Nov-15. S ERVLETS - H ITS C OUNTER Many times you would be interested in knowing total number of hits on a particular page of.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
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.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Configuration Web Server Tomcat - Install JDK Install Tom cat Configure Tom cat for running Servlet C:\Program Files\Apache Software Foundation\Tomcat.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
Java Servlets and Java Server Pages
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
 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,
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.
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.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Distributed Web Systems Java Servlets Lecturer Department University.
Programming with Java Lecture 6 Elements of a Java Servlet
CS3220 Web and Internet Programming Introduction to Java Servlets
Servlet Fudamentals.
Introduction to Java Servlets
Course Outcomes of Advanced Java Programming AJP (17625, C603)
Servlets Hits Counter 20-Jul-18.
Servlets and JSP 20-Nov-18 servletsJSP.ppt.
Servlets.
Java Servlets and JSP.
CS3220 Web and Internet Programming Introduction to Java Servlets
Basic servlet structure
Presentation transcript:

SKT-SSU IT Training Center Servlet and JSP. Chapter Three: Servlet Basics

SKT-SSU IT Training Center Servlet and JSP. Chapter Goals The basic structure of servlets A simple servlet that generates plain text A servlet that generates HTML Servlets and packages Some utilities that help build HTML The servlet life cycle Servlet debugging strategies

SKT-SSU IT Training Center JAVA Concepts. Read explicit data sent by client (form data) Read implicit data sent by client (request headers) Generate the results Send the explicit data back to client (HTML) Send the implicit data to client (status codes and response headers) A Servlet’s Job

SKT-SSU IT Training Center JAVA Concepts. A Servlet That Generates Plain Text (HelloWorld.java) import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); } URL assumes you have deployed from an Eclipse project named "intro". Code was in src/HelloWorld.java

SKT-SSU IT Training Center JAVA Concepts. A Servlet That Generates HTML Tell the browser that you’re sending it HTML –response.setContentType("text/html"); Modify the println statements to build a legal Web page –Print statements should output HTML tags Check your HTML with a formal syntax validator – –

SKT-SSU IT Training Center JAVA Concepts. A Servlet That Generates HTML (Code) public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+ "Transitional//EN\">\n"; out.println(docType + " \n" + " Hello \n"+ " \n" + " Hello \n" + " "); }

SKT-SSU IT Training Center JAVA Concepts. A Servlet That Generates HTML (Result) Assumes project named intro. Code in src/HelloServlet.java.

SKT-SSU IT Training Center JAVA Concepts. Using Packages Create a package –R-click on src folder –New  JAVA  Package Dropping servlet in package –Copy/paste from filesystem or existing project –Eclipse will automatically change the package statement at the top of the.java file Development strategy –Start with existing servlets and use them as the starting points for later servlets Start with HelloServlet2 at beginning –Always use packages –Do not do New  Servlet Results in ugly code with unnecessary parts

SKT-SSU IT Training Center JAVA Concepts. Using Packages (Continued) Manual packaging (no IDE) –Move the files to a subdirectory that matches the package name For example, I’ll use the coreservlets package for most of the rest of the servlets in this course. So, the class files need to go in a subdirectory called coreservlets. –Insert a package statement in the class file E.g., top of HelloServlet2.java: package coreservlets; Include package name in URL – This assumes you have enabled the "invoker servlet" that lets you run servlets without explicitly giving them addresses (good for testing and learning). Assumes project is "intro". You can also give explicit addresses as briefly mentioned in the last lecture and as will be discussed in more detail later (always used for real-life apps). Dot. Not slash!

SKT-SSU IT Training Center JAVA Concepts. Using Packages: HelloServlet2 (Code) package coreservlets; … public class HelloServlet2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+ "Transitional//EN\">\n"; out.println(docType + " \n" + " Hello (2) \n"+ " \n" + " Hello (2) \n" + " "); }

SKT-SSU IT Training Center JAVA Concepts. Using Packages: HelloServlet2 (Result) Assumes project named intro. Code in src/coreservlets/HelloServlet2.java. If you made the web.xml entries from the previous lecture, you could also use the URL

SKT-SSU IT Training Center JAVA Concepts. Some Simple HTML-Building Utilities public class ServletUtilities { public static final String DOCTYPE = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">"; public static String headWithTitle(String title) { return(DOCTYPE + "\n" + " \n" + " " + title + " \n"); }... } Don’t go overboard –Complete HTML generation packages usually work poorly –The JSP framework is a better solution

SKT-SSU IT Training Center JAVA Concepts. HelloServlet3: HelloServlet with Packages and Utilities package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Hello (3)"; out.println(ServletUtilities.headWithTitle(title)+ " \n" + " " + title + " \n" + " "); }

SKT-SSU IT Training Center JAVA Concepts. HelloServlet3: Result Assumes project named intro. Code in src/coreservlets/HelloServlet3.java and src/coreservlets/ServletUtilities.java.

SKT-SSU IT Training Center JAVA Concepts. The Servlet Life Cycle init –Executed once when the servlet is first loaded. Not called for each request. service –Called in a new thread by server for each request. Dispatches to doGet, doPost, etc. Do not override this method! doGet, doPost, doXxx –Handles GET, POST, etc. requests. –Override these to provide desired behavior. destroy –Called when server deletes servlet instance. Not called after each request.

SKT-SSU IT Training Center JAVA Concepts. Why You Should Not Override service The service method does other things besides just calling doGet –You can add support for other services later by adding doPut, doTrace, etc. –You can add support for modification dates by adding a getLastModified method –The service method gives you automatic support for: HEAD requests OPTIONS requests TRACE requests Alternative: have doPost call doGet

SKT-SSU IT Training Center JAVA Concepts. Debugging Servlets Use print statements; run server on desktop Use Apache Log4J Integrated debugger in IDE –Right-click in left margin in source to set breakpoint (Eclipse) –R-click Tomcat and use "Debug" instead of "Start" Look at the HTML source Return error pages to the client –Plan ahead for missing or malformed data Use the log file –log("message") or log("message", Throwable) Separate the request and response data. –Request: see EchoServer at –Response: see WebClient at Make sure browser is not caching –Internet Explorer: use Shift-RELOAD –Firefox: use Control-RELOAD Stop and restart the server

SKT-SSU IT Training Center JAVA Concepts. Summary Main servlet code goes in doGet or doPost: –The HttpServletRequest contains the incoming information –The HttpServletResponse lets you set outgoing information Call setContentType to specify MIME type Call getWriter to obtain a Writer pointing to client (browser) One-time setup code goes in init –Servlet gets initialized and loaded once –Servlet gets invoked multiple times –Initialization parameters set in web.xml Covered in later lecture

SKT-SSU IT Training Center JAVA Concepts. Question “I know Taekwondo, Kung Fu, and 47 other dangerous words.” --Source unknown