Java Servlets References: Karen Anewalt, Mary Washington College.

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

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.
1 Web Search Interfaces. 2 Web Search Interface Web search engines of course need a web-based interface. Search page must accept a query string and submit.
1 Servlets Based on Notes by Dave Hollinger & Ethan Cerami Also, the Online Java Tutorial by Sun.
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.
Technology Overview JAVA Servlets CS-611 S. Witherspoon.
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.
Servlets and a little bit of Web Services Russell Beale.
An introduction to Java Servlet Programming
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 34 Servlets.
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 10 Object Oriented Programming in Java Advanced Topics Servlets.
Comp2513 Java Servlet Basics Daniel L. Silver, Ph.D.
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
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.
Chapter 10 Servlets and Java Server Pages. A servlet is a Java class designed to be run in the context of a special servlet container An instance of the.
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.
Java Servelets. What Is a Servlet? A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
CSC 2720 Building Web Applications
COMP 321 Week 7. Overview HTML and HTTP Basics Dynamic Web Content ServletsMVC Tomcat in Eclipse Demonstration Lab 7-1 Introduction.
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.
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 & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
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.
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.
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.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 21 Java Servlets Wed. 11/22/00 based on material.
How CGI and Java Servlets are Run By David Stein 14 November 2006.
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.
CS122B: Projects in Databases and Web Applications Spring 2017
Introduction to Servlets
CS122B: Projects in Databases and Web Applications Winter 2017
Servlets.
Servlets.
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
CSE 403: Servlet Technology
Pre-assessment Questions
HTTP Servlet Overview Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers. For example, a servlet might.
Servlets.
Servlet.
Chapter 26 Servlets.
Servlets and Java Server Pages
CS122B: Projects in Databases and Web Applications Winter 2018
Introduction to Servlets
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.
Objectives In this lesson you will learn about: Need for servlets
Web Search Interfaces.
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
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.
CS122B: Projects in Databases and Web Applications Winter 2019
Presentation transcript:

Java Servlets References: Karen Anewalt, Mary Washington College

Servlet overview Servlets are java programs that run on a web server within the environment of a servlet engine. Servlets facilitate the creation of dynamic web pages. More efficient than CGI. Platform independent.

Web Servers A web server is a program running on the server machine that listens for incoming requests and services those requests as they come (find a page, execute a program on the server,… and then returns results to the web browser). Examples: Apache is the most frequently used (

Servlet engine Servlets require a servlet engine. A servlet engine is a software to handle requests to and responses from a servlet. Examples: Tomcat servlet engine ( Jserv, Jrun...

URLs Uniform resource locator –Address for a file stored on a web browser : / Servers usually listen for browser requests on port 80. Tomcat uses the 8080 port by default. BUT you were assigned a particular port number (port 2) on vulcan.

Servlets on vulcan Configuration: –echo $JAVA_HOME –echo $CLASSPATH –echo $HOME Starting the servlet engine: –cd tomcat/bin/ –./startup.sh Stopping the servlet engine: – cd tomcat/bin/ –./shutdown.sh

Servlets on vulcan Where to put your java files: –In the directory: tomcat/webapps/pace/WEB- INF/classes Try: – et/HelloWorldServlet –Be sure that HelloWorldServlet is referenced in the tomcat/webapps/pace/WEB-INF/web.xml file

HTML files on vulcan You can put them in the directory: –public_html or –tomcat/webapps/pace You can then access them respectively by: – – tml

HTML Hypertext Markup Language Formats web page layout

Creating forms in HTML Delimit form using Your form items here YourProgram is the file (with its location) to execute to process the form. The method specified must be GET or POST. Can use text boxes, radio buttons, checkboxes, pull-down lists…

Text boxes BoxLabel: Optional text box tag components: Size: size of box Maxlength: max number of characters Value: some default text

Radio buttons choice 1 choice 2 …

Checkboxes label

Pull down menus TextForChoice1 TextForChoice2 …

Textarea

Get versus Post There are two types of actions for requesting a servlet to be executed. Basic outcome is the same either way. Method of taking actions differs.

Get All form data that has been entered is appended to the request string using “key- value”pairs. Example: bin/hello.cgi?name=ASHLEY Name=ASHLEY is the key-value pair. Get is the default method of web requests.

Get Downsides to get: –If previous results for same request URL exist in the cache, older results may be displayed. –Limits exist on the amount of data that can be included in the string.

Post Packages all form data as part of the request body. Server program is able to read contents of input file and parse out variable names and values. Advantages: –Allows more data to be passed –Always sends request to server (rather than looking in cache)

Choosing get or post Use get to retrieve info Use post to make requests that will modify contents of data stored on the server

Writing Servlets Put your.java and.class files in the right directory –tomcat/webapps/pace/WEB-INF/classes To compile a Servlet you must have your CLASSPATH environment variable set to the location of the servlet.jar file. –/tomcat/common/lib/servlet.jar (or servlet- bin.jar)

Servlet API JSDK (Java Servlet Development Kit) It contains 2 packages: –Generic Servlet Interface: javax.servlet –Classes derived from generic servlet interface for HTTP requests: javax.servlet.http

Java classes and interfaces http is the most commonly used protocol w.r.t. Servlets HTTPServlet provides an abstract class to be subclassed to create an HTTP Servlet suitable for a Web site. HTTPServletRequest interface –Allows submission of key/value pairs to the server HTTPServletResponse interface –Creates object that can be modified by the Servlet and returned from the Servlet

HTTPServlet class Protected void doGet(HttpServletRequest req, HttpServletResponse resp) –Called by the server to allow a Servlet to handle a GET request. Protected void doPost(HttpServletRequest req, HttpServletResponse resp) –Called by the server to allow a Servlet to handle a POST request.

HTTPServletRequest interface Public String getParameter(String key) Public String[] getParameterValues(String key) Public Enumeration getParameterNames()

HTTPServletResponse interface Public void setContentType(string type) ServletOutputStream getOutputStream() Public PrintWriter getWriter() throws IOException

Servlet format Import necessary packages –javax.servlet.* and javax.servlet.http.* –java.io-.* for any IOException that may occur Write a class that extends the HTTPServlet Class Write the doGet or doPost method Create your response using the res parameter of doGet or doPost

Sending a response to the browser Use the HTTPServletResponse object Set the type for the response (using setContentType) Create a ServletOutputStream object suitable for writing data in response using getOutputStream, use println to place text into this object and close the object. –Alternative: Create a PrintWriter object

Getting data from the Browser Use the HTTPServletRequest object Call getParameter() method passing the key of the key/value pair as a parameter Example: To retrieve the value of an identifier called name from the html form, use: req.getParameter(“name”);

Performance notes Servlet is instantiated once but run many times (each request generates a new thread) For optimal performance: –Avoid concatenating String objects (makes extra objects for JVM garbage collection)

Using Sessions HTTP is a stateless protocol Transactions are viewed as isolated events If multiple transactions need to be linked (example: shopping cart), you must maintain state Create a connection between client&server that persists over multiple requests

Using sessions HTTPSession interface Create a session using getSession(true) –The true parameter indicates: Check if session is there & return handle to it if it exists; create a new session otherwise. Use methods in HTTPSession to store and retrieve session values Session is established between client machine and web server (separate for each machine).