ShowMessage.java Servlet that demonstrates the use of initialization parameters. Remember that, to use this servlet, you have to do three things: * Put the modified web.xml file in the WEB-INF directory. * Restart the server. * Use the registered servlet name (i.e., the URL http://host/servlet/ShowMsg), not the raw servlet name (i.e., the URL http://host/servlet/cwp.ShowMessage). package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Example using servlet initialization. Here, the message * to print and the number of times the message should be * repeated is taken from the init parameters. * * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class ShowMessage extends HttpServlet { private String message; private String defaultMessage = "No message."; private int repeats = 1; public void init() throws ServletException { ServletConfig config = getServletConfig(); message = config.getInitParameter("message"); if (message == null) { message = defaultMessage; } try { String repeatString = config.getInitParameter("repeats"); repeats = Integer.parseInt(repeatString); } catch(NumberFormatException nfe) { // NumberFormatException handles case where repeatString // is null *and* case where it is in an illegal format. } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "The ShowMessage Servlet"; out.println(ServletUtilities.headWithTitle(title) + "\n" + " " + title + " "); for(int i=0; i" + message + " "); } out.println(""); } } web.xml ? ShowMsg cwp.ShowMessage ? message Shibboleth ? repeats 5
ShowMessage.java Servlet that demonstrates the use of initialization parameters.
Permanent link to this article: http://bangla.sitestree.com/showmessage-java-servlet-that-demonstrates-the-use-of-initialization-parameters/