260 likes | 278 Views
Learn about using servlets and web services to provide web-based interfaces, create and deploy servlets using NetBeans IDE, apache Tomcat server, and Apache Axis, understand advantages of web services, and develop web applications with remote access.
E N D
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 and Tomcat server • Learn about Web Services and their advantages in relation to providing web based interfaces to databases and other applications • See how to create and deploy Web Services using Java, Apache Tomcat, and Apache Axis • Be aware of other tools for developing, deploying, and consuming web services
Providing remote access RMI CORBA Application Web/HTTP DCOM
WebApplication WebApplication Web Server HTTP HTTP Web Service Client WebBrowser Web Pages Application Interface Access over the Web
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
A user (1) requests some information by filling out a form containing a link to a servlet and clicking the Submit button (2). The server (3) locates the requested servlet (4). The servlet then gathers the information needed to satisfy the user's request and constructs a Web page (5) containing the information. That Web page is then displayed on the user's browser (6). (bit like CGI scripts, bit like applets) (from Sun) Using servlets
Servlets • Servlets are server-side resources • Servlets are Java objects that act as compact web servers • Can support all protocols, but are not as flexible/powerful as full servers • Need to run inside a web server that supports servlets • 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 data submitted by an HTML form. • Providing dynamic content from, for example, a database query • Managing state information on top of HTTP (which is stateless) • e.g. an online shopping cart which manages baskets for many concurrent customers and maps every request to the right customer.
Servlets • Servlets are part of J2EE • All servlets implement interface javax.servlet.Servlet • We will be using javax.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 have to over-ride the methods we need to • E.g., doGet(), doPost()
HelloWorld servlet • Using NetBeans, we can easily create servlets under 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 a browser (or other HTTP client for that matter) • Imagine that you have a form on a HTML page and clicking the "submit" button sends the data in the form back to 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 in the URL bar of your browser. • The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters.
POST… • A POST will send the information through a socket back to the webserver and 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 as serialized Java objects
Handling GET requests • GET requests call the doGet() method on your servlet • Put code in that method to handle GET, or call another method to do it • GET can pass in data through URL encoding
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 data about 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 the client 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 with this • 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 into servlets is a bit cumbersome • JSP pages let you use Java code directly in a HTML document • The Java code is then executed as a servlet at runtime