Pace CS 396S - Web Application Programming Using Java

Unformatted text preview:

Web Application Programming Using JavaThe Client’s Web PageThe ServletThe Web Application Deployment DescriptorFinding an Email Address in a DatabaseWeb Application Programming Using JavaWeb applications are used for a number of different purposes including e-commerce, on-line library access, clubs and associations, and school classes. They consist of a collection of programs and web pages written in Hypertext Markup Language (HTML). The programs can be in a number of computer languages including Java, Visual Basic, Perl, PHP, Python, and more.Hypertext Markup Language (HTML) was developed by Tim Berners-Lee in 19921 along with his invention of Hypertext Transfer Protocol (HTTP). Together HTML and HTTP created the World Wide Web. Originally the web was designed to display hypertext2 documents, i.e. documents containing links to other web pages. Consequently HTTP was designed for rapid ‘hops’ from one web page to another.Because web users were expected to remain a relatively brief time on any one page, HTTP does not maintain a connection for more than a quick page request and server response. It is said to be ‘stateless’. That means that the server does not store information about recent requests. This works very well for web surfing, but it is a problem for web applications that have to track users doing business on a site.3This document will consider ways to create and manage a web application written using Java servlets and Java Server Pages (JSP). We will see how to get a request from the client, process it by either accessing or modifying a database, and then create a response to send back to the client. Setup information for Java, the Apache Tomcat server, and the JCreator IDE (Integrated Development Environment) can be found in an Appendix.The Client’s Web PageThere are many objects that can be placed on a web page, but the only one of interest for web programming is that of a form. A form is used to collect information from the client and submit it to the server for processing. It contains an action attribute that tells the server what program to use to process the data and a method attribute that shows which method in the program should be called. An example ofan action attribute is action="http://localhost:8080/servlet/client-server.EmailServlet/".The form can collect data in a number of different ways, but the first one we will consider is that of a text box. A text box provides a box on the screen that accepts text input. Whatever the user types into the boxcan then be submitted to the server by clicking a button.An example of an HTML page containing a single form is shown below.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><title>E-Mail Form</title></head><body>1 Dave Raggett , A History of HTML, Chapter 2, Addison Wesley Longman, 1998, http://www.w3.org/People/Raggett/book4/ch02.html.2 The term, hypertext, was coined by Ted Nelson around 1965.3 Solutions include cookies placed on the client’s computer or session IDs encoded into the URL string. Both will bediscussed later.1<h3>Enter your name and e-mail address.<br />Then click the Send button to send the data to the server.</h3><form method = "get" action="http://localhost:8080/servlet/echo.EmailServlet"><p><input type = "text" name = "name" value = "" size = 30 /> Name </p><p><input type = "text" name = "email" value = "" size = 30 /> E-Mail Address </p><p><input type= "submit" value="Send" /></p></form></body> </html>The first line, <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">, is a declaration4 that should begin web pages. There are three types of declarations, Transitional, Strict, and Frameset. Strict pages must use Cascading Style Sheets5 (CSS) for all layout information. Transitional pages may still have some tags with styles, such as <body bgcolor="blue">. The Frameset declaration is for all pages that contain a frameset.The form contains a method attribute, method = "get", and an action attribute, action="http://localhost:8080/servlet/echo.EmailServlet"The method attribute tells the server what method to run in the Java servlet given by the action attribute. The method, get, means that the server is to run the doGet method in the servlet. The action attribute tellsthe server where to find the servlet that will do the processing.The example action attribute says that the servlet is located on the localhost6. It is to be accessed using port 8080. The name, servlet, in the path tells the server to look in its webapps/ROOT directory. All servlet classes are stored in the classes folder under that folder, but in addition, this servlet is in a package called echo. Finally the name of the servlet is EmailServlet.The form also contains two text boxes, one called name and the other called email. They are initially empty and have space for 30 characters. The names used for the text boxes must agree exactly with the parameters used in the servlet. Case differences between the form and servlet are a common cause of error. Finally the form has a button with the caption Send. It is used to submit the data in the form to the server.When the user clicks the submit button, the browser creates a URL string that looks like the following: http://localhost:8080/servlet/echo.EmailServlet?name=Alice+Lee&[email protected] section that precedes the question mark (?) is taken directly from the action attribute. The rest of the string consists of the data typed in by the user. In this case, the user typed “Alice Lee” into the box for the name and “[email protected]” into the box for the email address. (Spaces are replaced by the ‘+’ sign in the string.)The ServletWhen the form is sent to the server, the servlet named in the URL string is executed. It can request the data from the client and then formulate and send a response. A servlet is a subclass of the abstract class, 4 See the website of the W3C consortium, http://www.w3.org/MarkUp/, for further information.5 The W3C recommendations are at http://www.w3.org/Style/CSS/.6 Localhost is the standard name given to the local loop. It has IP address 126.0.0.1.2HttpServlet. 7 HttpServlet is contained in the Java packages javax.servlet and javax.servlet.http. These both must be imported into the program. They can be found in an archive called servlet.jar.8 HttpServlet has several methods that can be over-ridden. The two most important ones are doGet and doPost. They both


View Full Document
Download Web Application Programming Using Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Web Application Programming Using Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Web Application Programming Using Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?