学习java(一)——An Example Using Initialization Parameters
来源:互联网 发布:福大软件学院 编辑:程序博客网 时间:2024/04/30 21:42
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.
- 学习java(一)——An Example Using Initialization Parameters
- an example of using automake.
- An example of using Thrift
- Initialization Parameters
- An easy example of using AJAX
- An example of using STL multimap
- An Example Using boto Amazon MWS Package
- #150 – An Example of Using PropertyChanged and CoerceValue Callbacks(PropertyChanged和CoerceValue例子)
- Oracle Initialization Parameters:DEFERRED_SEGMENT_CREATION
- EBS initialization parameters - Healthcheck
- An example of using libexif to set JPEG Orientation Tag
- An example of using libexif to set JPEG Orientation Tag
- an example of drawing beast tree using ggtree
- An example
- an example
- SQlite数据库的C编程接口(四) 绑定参数(Bound Parameters) ——《Using SQlite》读书笔记
- SQlite数据库的C编程接口(四) 绑定参数(Bound Parameters) ——《Using SQlite》读书笔记
- SQlite数据库的C编程接口(四) 绑定参数(Bound Parameters) ——《Using SQlite》读书笔记
- asp.net 随机验证码
- 《COM原理及应用》学习笔记之第十四章
- Oracle系统紧急故障处理(数据文件、日志文件以及表空间损坏的处理)
- 悲惨的阿尔塞斯(全介绍+堕落全解析阿斯)
- GDB调试例子1
- 学习java(一)——An Example Using Initialization Parameters
- 关于暂住证的问题
- 最近完成 http://www.knowledge.com.cn 一体化设计
- “神奇铃声”你听到吗?
- 头痛啊...
- ASP.NET 中的正则表达式
- 根据数据库取得的值,在列表框中显示此值
- 化解办公室疲劳症(转贴)
- 学习使用PHP的类