1 50 package org.apache.avalon.fortress.examples.servlet; 51 52 import java.io.IOException ; 53 import javax.servlet.ServletException ; 54 import javax.servlet.ServletRequest ; 55 import javax.servlet.ServletResponse ; 56 57 65 public final class ServletContainer extends org.apache.avalon.fortress.impl.DefaultContainer 66 { 67 public static final String KEY = "hello-world"; 68 69 private org.apache.avalon.fortress.examples.components.Translator m_translator; 70 71 76 public void initialize() 77 throws Exception 78 { 79 super.initialize(); 80 81 m_translator = (org.apache.avalon.fortress.examples.components.Translator)m_serviceManager.lookup( org.apache.avalon.fortress.examples.components.Translator.ROLE ); 82 } 83 84 95 public void handleRequest( ServletRequest request, ServletResponse response ) 96 throws ServletException , IOException 97 { 98 java.io.PrintWriter out = response.getWriter(); 99 String selected = request.getParameter( "language" ); 100 String [] languages = m_translator.getSupportedLanguages( KEY ); 101 102 out.println( "<html>" ); 103 out.println( "<head><title>Hello World!</title></head>" ); 104 out.println( "<body>" ); 105 out.println( "<hr>" ); 106 107 out.println( "<h1>" ); 108 109 if( selected == null ) 110 { 111 out.println( "Please select your language" ); 112 } 113 else 114 { 115 out.println( m_translator.getTranslation( KEY, selected ) ); 116 } 117 118 out.println( "</h1>" ); 119 out.println( "<hr>" ); 120 121 out.println( "Available languages:" ); 122 123 out.println( "<form action='' name='languagelist'>" ); 124 out.println( "<select size='1' name='language'>" ); 125 126 for( int i = 0; i < languages.length; ++i ) 127 { 128 String lang = languages[ i ]; 129 out.print( "<option value='" + lang + "'" ); 130 131 133 if( lang.equals( selected ) ) 134 { 135 out.print( " selected" ); 136 } 137 138 out.println( ">" + lang + "</option>" ); 139 } 140 141 out.println( "</select>" ); 142 out.println( "<input value='OK' type='submit'>" ); 143 out.println( "</form>" ); 144 145 out.println( "</body>" ); 146 out.println( "</html>" ); 147 148 out.close(); 149 } 150 151 154 public void dispose() 155 { 156 if( m_translator != null ) 157 m_serviceManager.release( m_translator ); 158 159 super.dispose(); 160 } 161 } 162 163 | Popular Tags |