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 > Servlets Code Examples

Servlet initialisation parameters

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
Servlet initialisation parameters package com.ack.web.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * initialisation parameters inside the web.xml file <servlet> <servlet-name>initialisation</servlet-name> <servlet-class>com.ack.web.servlet.ServletInitialisationParameters</servlet-class> <init-param> <param-name>jdbcurl</param-name> <param-value>someurl</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>bah</param-value> </init-param> <init-param> <param-name>username</param-name> <param-value>foo</param-value> </init-param> </servlet> */ public class ServletInitialisationParameters extends HttpServlet { public void init() throws ServletException { String jdbcURL = getInitParameter( "jdbcurl" ); String username = getInitParameter( "username" ); String password = getInitParameter( "password" ); try { if( jdbcURL == null || username == null || password == null ) { StringBuffer buf = new StringBuffer( "you must specify the " ); buf.append( " 'jdbcurl', 'username', 'password' init params to -> " ); buf.append( getClass().getName() + " within you web.xml file" ); String msg = buf.toString(); System.out.println( buf ); // log( msg ); // throw new ServletException( msg ); } else { // possible get connection to database } } catch( Exception ex ) { log( ex.toString() ); throw new ServletException( ex.toString() ); } finally { // do any clean-up if you need to } log( "Completed Initialisation ->" + getClass().getName() ); } public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { res.setContentType( "text/html" ); PrintWriter pw = res.getWriter(); pw.println( "<h3>Servlet Initialisation Parameters</h3>" ); pw.println( "<p><hr noshade><p>" ); Enumeration e = getInitParameterNames(); while( e.hasMoreElements() ) { String paramName = (String) e.nextElement(); pw.println( "Initialisation Parameter: [ " ); pw.println( paramName + " = " + getInitParameter( paramName ) ); pw.println( " ]<br>" ); } } }