Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

peer-topeer Networks phần 5 pdf
Nội dung xem thử
Mô tả chi tiết
P1: OTE/SPH P2: OTE
SVNY285-Loo October 18, 2006 7:8
100 8. Testing and Enhancements of Servlets
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class home extends HttpServlet
{
String GlobalFile;
String LocalFile;
public void init() throws ServletException
{
System.out.println(*** home program started ***);
ServletConfig cfg = getServletConfig();
LocalFile= cfg.getInitParameter(FileName);
System.out.println(Local value: +LocalFile);
ServletContext global = getServletContext();
GlobalFile=global.getInitParameter(global);
System.out.println(Global value:+GlobalFile);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter output=response.getWriter();
output.println(<html>);
output.println(<head>);
output.println(<title> home</title>);
output.println(</head>);
output.println(<body>);
output.println(<h1>);
output.println(Globle File: +GlobalFile);
output.println(<br/>);
output.println( Local File: +LocalFile);
output.println(</h1>);
output.println(</body>);
output.println(</html>);
output.close();
} // end of method
public void destroy()
{
System.out.println(destroy method of home called);
}
}
Figure 8.8. home.java
P1: OTE/SPH P2: OTE
SVNY285-Loo October 18, 2006 7:8
Synchronization 101
Figure 8.9. Screen of browser—home.java
Figure 8.10. Screen of server—home.java
8.4 Synchronization
A web server creates a thread of the servlet to handle a browser’s request. Multiple
threads can be created at the same time. The advantage is that the creation of
threads is handled automatically by the server. We do not need to code it in the
servlet. On the other hand, this feature could be a problem for some applications.
Let us consider the following examples:
A client sends a number to the servlet.
The servlet adds the number to the variable ‘sum’.
The value of ‘sum’ might not be correct if we allow multiple threads to update
the variable ‘sum’ at the same time. Locking mechanism is required, and it can be
achieved by synchronization.
Syntax of the synchronization is:
public class ExampleServelt extends HttpServlet
{
Declare your variable here (e.g. sum)
. . . . . . . . . . . . . . . ..
public void init() throws ServletException