getServletContext().getAttribute( attribute )); } private String getServerInfoName(String serverInfo) { int

getServletContext().getAttribute( attribute )); } private String getServerInfoName(String serverInfo) { int slash = serverInfo.indexOf( / ); if (slash == -1) return serverInfo; else return serverInfo.substring(0, slash); } private String getServerInfoVersion(String serverInfo) { int slash = serverInfo.indexOf( / ); if (slash == -1) return null; else return serverInfo.substring(slash + 1); } } This servlet also directly subclasses GenericServlet, demonstrating that all the information about a server is available to servlets of any type. The servlet outputs simple raw text. When accessed, this servlet prints something like: req.getServerName(): localhost req.getServerPort(): 8080 getServletContext().getServerInfo(): JavaWebServer/1.1.1 getServerInfo() name: JavaWebServer getServerInfo() version: 1.1.1 getServletContext().getAttribute(”attribute”): null Unfortunately, there is no server-independent way to determine the server’s root directory, referred to in this book as server_root. However, some servers including the Java Web Server save the server’s root directory name in the server.rootsystem property, where it can be retrieved using System.getProperty( server.root ). Locking a Servlet to a Server This server information can be put to more productive uses. Let’s assume you’ve written a servlet and you don’t want it running just anywhere. Perhaps you want to sell it and, to limit the chance of unauthorized copying, you want to lock the servlet to your customer’s machine with a software license. Or, alternatively, you’ve written a license generator as a servlet and want to make sure it works only behind your firewall. This can be done relatively easily because a servlet has instant access to the information about its server. Example 4-4 shows a servlet that locks itself to a particular server IP address and port number. It requires an init parameter key that is appropriate for its server IP address and port before it unlocks itself and handles a request. If it does not receive the appropriate key, it refuses to continue. The algorithm used to map the key to the IP address and port (and vice-versa) must be secure. Example 4-4. A servlet locked to a server import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; public class KeyedServerLock extends GenericServlet { // This servlet has no class or instance variables // associated with the locking, so as to simplify // synchronization issues.
Hint: If you are looking for good and high quality web space to host and run your java application check Vision java web hosting services

Comments are closed.