Servlets - cs.bham.ac.ukrxb/Teaching/SSC2/slides1.pdf · Servlets Servlets are server-side...
Transcript of Servlets - cs.bham.ac.ukrxb/Teaching/SSC2/slides1.pdf · Servlets Servlets are server-side...
Servletsand a little bit ofWeb Services
Russell Beale
Overview In general
Provide remote access to applications
Servlets What are servlets How can we use them
Web Services What are web services…
Objectives Learn about using servlets as one way of providing web based
interfaces to databases and other applications.
Learn how to create and deploy servlets using the NetBeans IDE andTomcat server
Learn about Web Services and their advantages in relation to providingweb based interfaces to databases and other applications
See how to create and deploy Web Services using Java, ApacheTomcat, and Apache Axis
Be aware of other tools for developing, deploying, and consuming webservices
Providing remote access
RMI CORBA
DCOMWeb/HTTP
Application
Access over the WebWeb
Application
Web Server
WebApplication
WebBrowser
HTTP
WebServiceClient
HTTP
Web Pages ApplicationInterface
Servlets and Web Services
Servlets providing generic access to an application,
using a web interface we need to build both client and server
Web Services providing generic access with a defined API allows custom interface at the client we can just build the server
Using servlets A user (1) requests some
information by filling out aform containing a link to aservlet and clicking theSubmit button (2).
The server (3) locates therequested servlet (4).
The servlet then gathers theinformation needed to satisfythe user's request andconstructs a Web page (5)containing the information.
That Web page is thendisplayed on the user'sbrowser (6).
(bit like CGI scripts, bitlike applets)
(from Sun)
Servlets Servlets are server-side resources
Servlets are Java objects that act as compact webservers
Can support all protocols, but are not asflexible/powerful as full servers
Need to run inside a web server that supportsservlets
Take in requests re-directed from the web-server,write HTML back to the client
Advantages of servlets
Based on Java: convenient & powerful,can talk directly to the server
Efficient – lightweight Java processes,servlet code loads only once
Free/very cheap
Typical uses
Processing and/or storing datasubmitted by an HTML form.
Providing dynamic content from, forexample, a database query
Managing state information on top ofHTTP (which is stateless) e.g. an online shopping cart which
manages baskets for many concurrentcustomers and maps every request to theright customer.
Servlets
Servlets are part of J2EE
All servlets implement interfacejavax.servlet.Servlet
We will be usingjavax.servlet.http.HttpServlet
HTTP protocol
8 request methods: GET – retrieve content POST – send data, retrieve content HEAD – retrieve headers only PUT – upload content DELETE – remove content TRACE – echos the request, showing servers etc OPTIONS – returns list of supported methods CONNECT – used with SSL proxy tunnels
Lifecycle
init() set up the servlet
service() respond to requests, after init()
destroy() shutdown the servlet
Using HttpServlet
By extending HttpServlet, we only haveto over-ride the methods we need to
E.g., doGet(), doPost()
HelloWorld servlet
Using NetBeans, we can easily create servletsunder Tomcat
Tomcat is a Java server that supports servlets
Tomcat is bundled with NetBeans IDE
HelloWorld servlet
POST and GET GET and POST allow information to be
sent back to the webserver from abrowser (or other HTTP client for thatmatter)
Imagine that you have a form on aHTML page and clicking the "submit"button sends the data in the form backto the server, as "name=value" pairs.
HTML forms<form action= "PostExample" method=POST>
<input type=text size=20 name=firstname><br>
<input type=text size=20 name=lastname><br><input type=submit></form>
GET… Choosing GET as the "method" will append all
of the data to the URL and it will show up inthe URL bar of your browser.
The amount of information you can send backusing a GET is restricted as URLs can only be1024 characters.
POST… A POST will send the information
through a socket back to the webserverand it won't show up in the URL bar.
It is stored on the request object You can send much more information to
the server this way not restricted to textual data - you can
send files and even binary data such asserialized Java objects
Handling GET requests
GET requests call the doGet() methodon your servlet
Put code in that method to handle GET,or call another method to do it
GET can pass in data through URLencoding
Handling POST requests
POST requests call the doPost() method
Put code in this method, or call another one
Post data is stored on the request object
PostExample.htm
Storing Data
We often want to store some dataabout the user and their requests
We can do this in 2 ways:
Client-side - cookies
Server-side – session data, database etc
What are cookies?
HTTP protocol is stateless Browser contacts server ata URL, requests a page,
provides its capabilities Server sends info to client Connection closed
So to mark one visitor to track visit to site,need to store a piece of information on theclient side
This is the cookie HTTP header that contains text string
Two sorts
Session Temporary, erased when you close
browser Often used by e-commerce sites for
shopping carts Persistent
Written to hard drive Remain until erased or expire Used to store user preferences
Sessions
Live on the server Actually built on top of cookies or URL
rewritin, but you don’t have to bother withthis
HttpSession object Stores all the information for a session Saves you having to access the cookies
yourself
Servlets and JSP
Putting large amounts of HTML intoservlets is a bit cumbersome
JSP pages let you use Java code directlyin a HTML document
The Java code is then executed as aservlet at runtime