In recent years, a large amount of software development activity has migrated from



tải về 2.32 Mb.
Chế độ xem pdf
trang8/16
Chuyển đổi dữ liệu20.09.2022
Kích2.32 Mb.
#184179
1   ...   4   5   6   7   8   9   10   11   ...   16
Core JSP
A Simple JSP Page 
Often the easiest way to understand something is to see it. 
Listing 1.1
shows a very 
simple JSP page. 
Example 1.1. simpleDate.jsp 
 
 
A simple date example 
 
 
The time on the server is 
<%= new java.util.Date() %> 
The time on the server is
Wed Dec 08 16:17:57 PST 1999 
The current date is: 
<% out.println(new java.util.Date()); %> 
The current date is: 
<%= new java.util.Date() %> 
014 <$nopage> 
015 The current date is: 
016 <%= PrintDate() %> 
017 <$nopage> 
018
CELLSPACING="0"> 
 
 
 
Control Flow Statements 
 

Decision Making Statements

 

if . . . else Statements

 
 
<% if (day == 1 | day == 7) { %> 
It's the 
weekend! 
<% } else { %> 
Still in the work week. 
<% } %> 
 

switch . . . case Statements

 
 
The current day is:
<% switch (day) { %> 
<% case 1: %> 
Sunday 
<% break; %> 
<% case 2: %> 
Monday 
<% break; %> 
<% case 3: %> 
Tuesday 
<% break; %> 
<% case 4: %> 
Wednesday 
<% break; %> 
<% case 5: %> 
Thursday 
<% break; %> 
<% case 6: %> 
Friday 
<% break; %> 
<% case 7: %> 
Saturday 
<% break; %> 
<% default: %> 
Error! Bad day! 
<% break; %> 
<% } %> 
 
 


Loop Statements

 

for Statements

 
 
<% for (int fontSize=1; fontSize <= 5; fontSize++) { %> 
"> 
The Quick Brown Fox . . .
<% } %> 
 

while Statements

 
 
<% int counter = 0; %> 
<% while( counter <= 4) { %> 
" SIZE="+1"> 
<%= wordArray[counter] %> 
<% counter++; %> 
<% } %> 
 
 

Exception Statements

 

try . . . catch Statements

 
Trying to divide by 
this random number: <%= randomNumber %>
<% try { %> 
<% int result = 100 / randomNumber; %> 
Success! 
<% } catch (Exception e) { %> 
Failure!
Error Message: <%= e.getMessage() %> 
<% } %> 
 
 
<%-- This is a JSP Style comment --%> 
URL of the current page: <%= request.getRequestURI()%> --> 
<%-- 
Here I am commenting out the following JSP code: 
<%= request.getServerName()%> 
--%> 


 
 


 
 
 
 
 
 
 
 
 
<% // Set the current day of the month 
int currentDay = 
currentCal.get(currentCal.DAY_OF_MONTH); 
// Calculate the totals days in the month 
int daysInMonth = 
currentCal.getActualMaximum(currentCal.DAY_OF_MONTH); 
// Calculate the day of the week for the first 
currentCal.set(currentCal.DAY_OF_MONTH, 1); 
int dayOfWeek = currentCal.get(currentCal.DAY_OF_WEEK); 
// Prefill the calendar with blank spaces 
if (dayOfWeek != 1) { 
out.println(" "); 

// Fill in dates 
for (int day=1; day <= daysInMonth; day++) { 
if (day == currentDay) { 
out.println(" "); 
} else { 
out.println(" "); 

if (dayOfWeek == 7) { 
out.println(" \n\n "); 
dayOfWeek = 1; 
} else { 
dayOfWeek++; 


// Postfill the calendar with blank spaces 
if ((8-dayOfWeek) != 0) { 
out.println(" "); 

%> 
 
 
<%= FormatTitle(currentCal) %> 
SUNMONTUEWEDTHUFRISAT
 " + day + 
"
" + day + 
"
 
 
Here I am including the page /core-jsp/date.jsp:
 
 


 
This page has been accessed 
<% counter++; 
out.print(counter); 
%> 
times since last restarted. 
 
 
This page has been accessed 
<% synchronized (page) { 
counter++; 
out.print(counter); 

%> 
times since last restarted.
 


 
This page has been accessed 
<% 
synchronized(page) { 
counter++; 
out.print(counter); 

%> 
times. 
 
\r\nCLASS=\"pageFooter\">\r\nThis page has been accessed 
\r\n"); 
// end 
// begin [file="/counter3.jsp";from=(50,2);to=(55,0)] 
synchronized(page) { 
counter++; 
out.print(counter); 

// end 
// begin [file="/counter3.jsp";from=(55,2);to=(62,0)] 
out.write("\r\ntimes.\r\n
\r\nBODY>\r\n\r\n\r\n\r\n"); 
// end 
} catch (Exception ex) { 


if (out.getBufferSize() != 0) 
out.clear(); 
pageContext.handlePageException(ex); 
} finally { 
out.flush(); 
_jspxFactory.releasePageContext(pageContext); 



4.6 Performance Tuning the Servlet 
There are several aspects of Java programming that can have heavy impacts on the 
performance of a JSP page. 
Some of these are not JSP specific, but simply good Java programming techniques 
overall. The ones listed are the most common efficiency mistakes. 
Avoid appending with concatenation 
In the course of development it is very easy to use the concatenation operator (+) to 
join String objects. For example: 
String output; 
output += "Item: " + item + " "; 
output += "Price: " + price + " "; 
println (output); 
Unfortunately, it is easy to forget that the 
String
object is immutable and is not 
designed to contain modifiable data. The 
StringBuffer
object is designed to perform 
manipulation of strings. Any time a 
String
object is modified it really just creates a 
new 
StringBuffer
and a series of new 
String
objects, then all the strings are 
appended into the 
StringBuffer,
and the old 
String
is replaced with the results of 
StringBuffer.toString()
While processing the above code there will be several new 
String
and 
StringBuffer
objects created. It's usually significantly more efficient to convert the 
String
into a 
StringBuffer
or even start out with a 
StringBuffer
object. For 
example: 
<$nopage> 
001 StringBuffer output; 
002 <$nopage> 
003 output.append(new String(“Item: ”)); 
004 <$nopage> 
005 output.append(item); 
006 <$nopage> 
007 output.append(new String(“ “)); 
008 <$nopage> 
009 output.append(new String(“Price: ”)); 
010 <$nopage> 


011 output.append(price) 
012 <$nopage> 
013 output.append(new String(“ “)); 
014 <$nopage> 
015 println (output.toString()); 
016 <$nopage> 
Use syncronize() carefully 
While protecting objects from thread Race Conditions is extremely important, it is 
easy to create performance bottlenecks in the process of synchronizing. 
Make sure that synchronized blocks contain the fewest lines of code possible. When 
many threads are waiting on a block of code, even reducing one line of code within 
the synchronized block can make a large difference. 
Also, it is always good to synchronize on the most appropriate locking object as 
possible. Usually it is best to synchronize on the object that is threatened by the Race 
Condition. Avoid using the this or page object as the locking object as much as 
possible. 
Overall it is obvious that the JSP engine is built to take advantage of the extremely 
powerful Java Servlet architecture. While threading is a significant enhancement, care 
needs to be taken to prevent the "Race Condition." Other techniques based on Java 
Servlet tuning can be used to fine-tune the performance of the JSP page and the 
underlying Java Servlet. 
Chapter 5
moves on to further discuss the 
request
object 
and how it can be used to create dynamic Web applications. 
Chapter 5. RETRIEVING INFORMATION 
Topics in this Chapter:
• 
The Request 
• 
Receiving parameter information 
• 
Receiving header information 
• 
Receiving attribute information 
• 
Getting information about the server 
JSP pages, like most Web application platforms, use HTTP as a transport protocol. To 
understand how a JSP program changes for different requests, one must understand 
the Web transaction model, which is built on HTTP. The model itself is very simple, 
and it applies to many different Web-programming technologies. A good analogy for 
the Web transaction model is a math function box. Math function boxes are a tool to 
explain how mathematical functions work. The important thing to understand is that 
the function box itself never changes. The only reason an output changes is if input 
changes. It is exactly the same with JSP pages. The JSP page itself does not change, 
but it generates different output based on different input. (see 
Figure 5-1
). 

tải về 2.32 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   4   5   6   7   8   9   10   11   ...   16




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©tieuluan.info 2022
được sử dụng cho việc quản lý

    Quê hương
Application form