6b99fd94e8a3612926d87134cffc6bbf.ppt
- Количество слайдов: 31
Java the UML Way http: //www. tisip. no/Java. The. Uml. Way/ Web Programming With Java. Server Pages Programming for the Web Servlets Java. Server Pages (JSP) basics Inputting data from the user Client side validation with Java. Script Databases Storing state information Programming cookies Programming sessions The problem with reloading pages versjon 2002 -04 -17 page 2 -3 page 4 -6 page 7 -12 page 13 -18 page 19 page 20 -24 page 25 -27 page 28 page 29 -30 page 31 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ Chapter 21
Searching for Hotels on the Internet? • You enter a city and get back a list of possible hotels. • You examine the source code, and you find the hotel names hardcoded. • Why doesn’t the page contain program logic that searches for the hotels in a database? • The answer is that the HTML code is specially adapted. • The Web server receives your query for hotels in a specific city, sends this query to a program that searches the database, and generates HTML code with the applicable hotels. • This code is then sent to the client in response to the query. • This chapter is about this kind of programming. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 2
Different Ways of Programming for the Web • Dynamic on the client side, in the client’s browser – An applet is a program that is downloaded to a Web client in connection with an HTML page. • The download takes a little longer than normal • The browser prints an alert in the status line that Java is being started. • In return, the user gets a page with far greater functionality than a purely static HTML page offers. – Various scripting languages have gradually made simple dynamics possible without costing too much in download time. – The user may examine the source code of these scripts. • Dynamic on the server side – We fill in forms and push the Send-button. – The data are handled by a program on the server side. – This handling may involve specially created HTML pages that are sent back to the client. – We’ll use Java. Server Pages (JSP) to program the server side. Something called servlets are the basis of JSP. • Software – You’ll need a Web server that supports servlets and JSP. – You’ll need the API online documentation for these classes. – The book tells you how to install and use Lite. Web. Server from Gefion Software. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 3
Servlets A servlet is a Java program that runs on a Web server. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 4
The Servlet Object • All clients work with the same servlet object. • Any instance variables there might be can be updated by multiple clients. • Methods that work with the instance variables should be synchronized. – For example, the synchronized java. util. Vector class should be used in stead of the unsynchronized java. util. Array. List. • When a servlet is initialized, a pool of threads is usually generated that the service() method runs in. – A client that requests the servlet (and thereby the service() method) will be assigned its own thread. – This thread is returned to the pool after use. – If a thread is requested and the pool is empty, the client has to wait, or the pool can be expanded. – By using a pool of threads, we avoid resource-intensive initialization every time a request comes from a client. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 5
Example of Server-Side Programming public class Simple. Servlet extends Http. Servlet { public void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { response. set. Content. Type("text/html"); source code at the server side Print. Writer out = response. get. Writer(); out. print(""); …see program listing 21. 1… out. println(""); } }
Java. Server Pages (JSP) • A jsp file contains HTML code with elements of Java code. • The user requests the jsp file directly. – If this is a first request for this file, it will be converted to a servlet. – The servlet is compiled and starts automatically. – The result of the servlet’s service() method will be an HTML page that is sent to the client and presented in the client’s browser. • A JSP page is thus an HTML page that is created by a servlet based on a jsp file.
Why JSP? • • • Only to avoid writing out. print() around the HTML elements? No! HTML is presentation. A jsp file mainly contains HTML. Web designers create HTML code. Java programmers create Java classes dealing with the problem that’ll be solved. • Only small snippets of Java code are needed to link the jsp file to the Java classes. • However, this book is about programming, not design. • In this book the jsp files are rather untypical, they’ll contain relatively little HTML. Solve the problems, page 658. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 8
JSP Predefined Objects and JSP Expressions • Predefined objects – – request, type: Http. Servlet. Request response, type: Http. Servlet. Response out, type: Print. Writer session, type: Http. Session • JSP expressions – Surrounded by <%= and %> – Examples: This page was downloaded: <%= new java. util. Date() %> // No semicolon! The server calculates 17 * 7 and gets <%= 17 * 7 %> The client machine is: <%= request. get. Remote. Host() %> Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 9
JSP Scriptlets Java code surrounded by <% and %>, the same example in two ways: Five random numbers in the interval [1, 100]: <% int sum = 0; for (int i = 0; i < 5; i++) { int number = (int) (100 * Math. random() + 1); out. println(" " + number); sum += number; } out. println(" " + "The sum is " + sum); %> The client output looks like this: Five random numbers in the interval [1, 100]: <% int sum = 0; for (int i = 0; i < 5; i++) { int number = (int) (100 * Math. random() + 1); %> <%= number %> <% sum += number; } %> The sum is <%= sum %> Five random numbers in the interval [1, 100]: 86 7 43 91 3 The sum is 230 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 10
JSP Declarations • • Local variables are declared inside scriptlets. Every client gets its own. Variables declared outside become instance variables (common for all clients), and they must be surrounded by <%! and %> <%! private int no. Of. Visits = 0; %> <% no. Of. Visits++; %> No. of visits since servlet started = <%= no. Of. Visits %> <% int counter = 0; %> <% counter++; %> Counter = <%= counter %> • The first time the servlet is accessed, we get this printout in the browser window: No. of visits since servlet started = 1 Counter = 1 • The next time it looks like this: No. of visits since servlet started = 2 Counter = 1 • And so on: No. of visits since servlet started = 3 Counter = 1 • Every time the servlet restarts, the variable no. Of. Visits is set to zero. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 11
Comments and Directives • Regular Java comments in the Java code • JSP comments in the JSP code <%-- this is a JSP comment, it can be several lines long --%> • HTML comments for comments that should be sent to the browser • A JSP directive controls the construction of the servlet that is created from the jsp file. • A directive is enclosed between <%@ and %>. • The page directive: <%@ page import="my. Library. Book, java. text. Number. Format" %> • The include directive, the contents of the file named are inserted before the servlet is generated: <%@ include file = "Person. Form. jsp"%> Solve problems 1 and 2, page 662. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 12
Inputting Data from the User
What Happens After the User Presses the Send Button? Read. Country. jsp
Back Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 14
parameter value parameter name String the." src="https://present5.com/presentation/6b99fd94e8a3612926d87134cffc6bbf/image-15.jpg" alt="Parameter Name and Value parameter value parameter name String the." />
Parameter Name and Value parameter value parameter name String the. Country = request. get. Parameter("country"); Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 15
Error Message on the Same Page As the Input Field
<% String country = request. get. Parameter("country"); /* country is null the first time the page is downloaded by a user */ if (country != null) { country = country. trim(); if (country. equals("")) out. println("You have to enter data!"); else out. println("Hallo, you are from " + country + "!"); } %> Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 16
What If We’d Used “get” Instead of “post”? • In the HTML file we now write:
Show program listings 21. 4 and 21. 5, pp. 667 -669. Solve problem 1, page 670. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0 -470 -84386 -1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http: //tisip. no/engelsk/ 18
Client Side Validation with Java. Script • • • As a supplement to server side checking, data should be checked before they are sent to the server. The HTML code has to be expanded with script code. Java. Script is the de facto standard for client-side script programming.













