Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Java Programming Code Examples

Java > Core Java Code Examples

Information Servlet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
Information Servlet // Import standard networking I/O packages import java.net.*; import java.io.*; // Enumeration from util import java.util.Enumeration; // Import servlet packages import javax.servlet.*; import javax.servlet.http.*; // // // InfoServlet // // public class InfoServlet extends HttpServlet { // Get method of servlet public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException { // Define content type response.setContentType("text/html"); // Get information about client and server String clientBrowser = request.getHeader("User-Agent"); String clientReferer = request.getHeader("Referer"); String clientIP = request.getRemoteAddr(); String serverOS = System.getProperty("os.name"); String serverOSVersion = System.getProperty("os.version"); String serverOSArch = System.getProperty("os.arch"); // Get a servlet output stream for the response ServletOutputStream sout = response.getOutputStream(); // Print header information sout.println ("<HTML><HEAD><TITLE>InfoServlet Response</TITLE></HEAD>"); // Print body information sout.println ("<BODY BGCOLOR='white' COLOR='black'>"); sout.println ("<H2>InfoServlet</H2><HR>"); // Print information about client sout.println ("<H3>Client :-</H3>"); // Check for presence of user-agent header field if (clientBrowser != null) sout.println ("User-Agent : " + clientBrowser + "<BR>"); // Print IP address sout.println ("IP Address : " + clientIP + "<BR>"); // Check for presence of referer header field if (clientReferer != null) sout.println ("Last page : " + clientReferer + "<BR>"); // Print information about server sout.println ("<H3>Server</H3>"); // Check to see if each property is valid, and if so, output it if ( (serverOS != null) & (serverOSVersion != null) ) sout.println ("Server O/S : " + serverOS + " v" + serverOSVersion + "<BR>"); if ( serverOSArch != null) sout.println ("Server CPU : " + serverOSArch + "<BR>"); // Obtain a reference to the server context ServletContext context = getServletContext(); if (context != null) { // Display information about servlets sout.println ("<h3> Servlet information </h3>"); // Table for servlet info sout.println ("<table border='1' width=70%>"); sout.println ("<tr><td><b>Servlet Name</b></td></tr>"); for (Enumeration e = context.getServletNames(); e.hasMoreElements();) { // Get name of servlet String name = (String) e.nextElement(); sout.println ("<tr> <td>"); sout.println (name); sout.println ("</td> </tr>"); } sout.println ("</table>"); } else sout.println ("Could not determine servlet context"); // Write footer information sout.println ("</BODY></HTML>"); // Flush sout.flush(); } // Post method of servlet public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException { // Perform same action as get method doGet(request, response); } public String getServletInfo() { return new String ( "InfoServlet - reports information on client connection and server state" ); } }