This example shows the servlet reads the
message and the repeats initialization parameters when initialiazed
import javax.servlet.http. * ;
import javax.servlet. * ;
import java.io. * ;
public class ShowMessage extends HttpServlet {
private String message;
private String defaultMessage = "No Message";
private int repeats = 1;
public void init( ServletConfig config ) throws
ServletException {
super.init( config );
message = config.getInitParameter("message");
if( message == null ){
message = defaultMessage;
}
try {
String repeateString = config.getInitParameter("repeats");
repeats = Integer.parseInt(repeateString);
} catch( NumberFormatException e ){
// NumberFormatException handles case where repeatString
// is null *and* case where it is something in an
// illegal format. Either way, do nothing in catch,
// as the previous value (1) for the repeats field will
// remain valid because the Integer.parseInt throws
// the exception *before* the value gets assigned
// to repeats.
}
}
public void doGet( HttpServletRequest request,
HttpServletResponse response) throws
IOException, ServletException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("<html><head><title>The showmessage example</title></head>");
writer.print("<body bgcolor="#fdf5e6">");
writer.print("<h1>The showMessage Servlet</h1>");
for( int i=0; i<repeats; i++ ) {
writer.print(message+"<br>");
}
writer.print("</body></html>");
}
}
Core Approach
For complex initializations, store the data in a separate file and use the init
parameters to give the location of that file.
import javax.servlet.http. * ;
import javax.servlet. * ;
import java.io. * ;
public class ShowMessage extends HttpServlet {
private String message;
private String defaultMessage = "No Message";
private int repeats = 1;
public void init( ServletConfig config ) throws
ServletException {
super.init( config );
message = config.getInitParameter("message");
if( message == null ){
message = defaultMessage;
}
try {
String repeateString = config.getInitParameter("repeats");
repeats = Integer.parseInt(repeateString);
} catch( NumberFormatException e ){
// NumberFormatException handles case where repeatString
// is null *and* case where it is something in an
// illegal format. Either way, do nothing in catch,
// as the previous value (1) for the repeats field will
// remain valid because the Integer.parseInt throws
// the exception *before* the value gets assigned
// to repeats.
}
}
public void doGet( HttpServletRequest request,
HttpServletResponse response) throws
IOException, ServletException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("<html><head><title>The showmessage example</title></head>");
writer.print("<body bgcolor="#fdf5e6">");
writer.print("<h1>The showMessage Servlet</h1>");
for( int i=0; i<repeats; i++ ) {
writer.print(message+"<br>");
}
writer.print("</body></html>");
}
}
Core Approach
For complex initializations, store the data in a separate file and use the init
parameters to give the location of that file.