Thứ Sáu, 13 tháng 6, 2014

Servlet Context

Link bài demo : Demo
Servlet context dùng để duy trì trạng thái cho ứng dụng web.Nó được tạo ra bởi các đối tượng container và sử dụng thông tin từ class web.xml

Để hiểu chi tiết hơn về servlet context tôi xin trích dẫn những thông tin cần thiết tại nguồn sau

Các phương thức của Servlet Context

 java.lang.Object
getAttribute(java.lang.String name)
          Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
 java.util.Enumeration
getAttributeNames()
          Returns an Enumeration containing the attribute names available within this servlet context.
getContext(java.lang.String uripath)
          Returns a ServletContext object that corresponds to a specified URL on the server.
 java.lang.String
getInitParameter(java.lang.String name)
          Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.
 java.util.Enumeration
getInitParameterNames()
          Returns the names of the context's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the context has no initialization parameters.
 int
getMajorVersion()
          Returns the major version of the Java Servlet API that this servlet container supports.
 java.lang.String
getMimeType(java.lang.String file)
          Returns the MIME type of the specified file, or null if the MIME type is not known.
 int
getMinorVersion()
          Returns the minor version of the Servlet API that this servlet container supports.
getNamedDispatcher(java.lang.String name)
          Returns a RequestDispatcher object that acts as a wrapper for the named servlet.
 java.lang.String
getRealPath(java.lang.String path)
          Returns a String containing the real path for a given virtual path.
getRequestDispatcher(java.lang.String path)
          Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
 java.net.URL
getResource(java.lang.String path)
          Returns a URL to the resource that is mapped to a specified path.
 java.io.InputStream
getResourceAsStream(java.lang.String path)
          Returns the resource located at the named path as an InputStream object.
 java.lang.String
getServerInfo()
          Returns the name and version of the servlet container on which the servlet is running.
getServlet(java.lang.String name)
          Deprecated. As of Java Servlet API 2.1, with no direct replacement.
This method was originally defined to retrieve a servlet from a ServletContext. In this version, this method always returns null and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.
In lieu of this method, servlets can share information using the ServletContext class and can perform shared business logic by invoking methods on common non-servlet classes.
 java.util.Enumeration
getServletNames()
          Deprecated. As of Java Servlet API 2.1, with no replacement.
This method was originally defined to return an Enumeration of all the servlet names known to this context. In this version, this method always returns an empty Enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.
 java.util.Enumeration
getServlets()
          Deprecated. As of Java Servlet API 2.0, with no replacement.
This method was originally defined to return an Enumeration of all the servlets known to this servlet context. In this version, this method always returns an empty enumeration and remains only to preserve binary compatibility. This method will be permanently removed in a future version of the Java Servlet API.
 void
log(java.lang.Exception exception, java.lang.String msg)
          Deprecated. As of Java Servlet API 2.1, use log(String message, Throwable throwable) instead.
This method was originally defined to write an exception's stack trace and an explanatory error message to the servlet log file.
 void
log(java.lang.String msg)
          Writes the specified message to a servlet log file, usually an event log.
 void
log(java.lang.String message, java.lang.Throwable throwable)
          Writes an explanatory message and a stack trace for a given Throwable exception to the servlet log file.
 void
removeAttribute(java.lang.String name)
          Removes the attribute with the given name from the servlet context.
 void
setAttribute(java.lang.String name, java.lang.Object object)
          Binds an object to a given attribute name in this servlet context.

Demo : bài làm đếm số lượt truy cập vào website 
Hướng dẫn làm bài : tạo 1 web application trong đó ta tạo thêm 2 servlet là page1 và page2.các bạn vui lòng trờ về các blog trước để xem hướng dẫn cụ thể.Sau đó thêm phương thức đếm cho các class như sau
Sau đó ở trong thẻ <body> của mỗi servlet ta thêm đoạn code out.println("<h1>Số lượt xem trang :"+count+ "</h1>")    để hiển thị ra giá trị count
Sau đó ta sẽ được kết quả :

Không có nhận xét nào:

Đăng nhận xét