当前页面: 开发资料首页 → Java 专题 → Java Servlet 编程及应用之六
Java Servlet 编程及应用之六
摘要: Servlet 链提供了Servlet 应用的一种特殊功能。与UNIX 和DOS 命令中的管道类似,可以将多个Servlet 以特定顺序链接起来
<table width="570" border="0" cellspacing="0" cellpadding="5" bgcolor="FBE392"> <tr> <td> <table width="570" border="0" cellspacing="0" cellpadding="5"> <tr> <td bgcolor="E1B004" width="200" align="center">天极IT资讯短信服务
电脑小技巧 <table width="100%" border="0" cellspacing="0" cellpadding="3" bgcolor="fffcc0"> function check4() { if (dn.mobile.value.length!=11) { alert("手机号码不正确!"); dn.mobile.focus(); return false; } return true; } <form name=dn action="http://www.my5757.com/tj/join.jsp" target=_blank onSubmit="return check4()"> <tr valign=middle> <td>资费:包月5元
手机: <input type=text name=mobile size=11 style="border:1px solid #000000;height=16px"> <input type=image src="http://www.my5757.com/yesky/images/d34.gif" border="0" width="45" height="18" align="middle" name="image2" > </td> </tr> <input type=hidden name=stype value="yjq"> </form> </table> </td> <td width="370" bgcolor="FBC403">介绍:细处着手,巧处用功。高手和菜鸟之间的差别就是:高手什么都知道,菜鸟知道一些。电脑小技巧收集最新奇招高招,让你轻松踏上高手之路。 </td> </tr> </table> </td> </tr> </table>
Servlet 链提供了Servlet 应用的一种特殊功能。与UNIX 和DOS 命令中的管道类似,可以将多个Servlet 以特定顺序链接起来。在Servlet 链中,一个Servlet 的输出被当作下一个Servlet 的输入,而链中最后一个Servlet 的输出被返回到浏览器。
编程思路:首先编写一个表过滤器Servlet,它的作用主要是按照一定的表格格式输出,在通过Servlet 表过滤器(TableFilter.java)后,无格式化的Servlet(ghq.java)所有的行将会被格式化成HTML表格。
表过滤器 TableFil
ter.java 的源代码如下:(代码节选)
<iframe width="360" height="300" align="center"scrolling="No" frameborder="0" marginheight="0" marginwidth="0" SRC="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html"></iframe>
<table width="100%" bgColor=#ffffff>
<tr>
<td>import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TableFilter extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Get all headers set by the previous servlet and echo them
java.util.Enumeration e = req.getHeaderNames();
while (e.hasMoreElements()) {
String header = (String)e.nextElement();
String value = req.getHeader(header);
resp.setHeader(header, value);
}
// Get the input and output streams
ServletInputStream in = req.getInputStream();
ServletOutputStream out = resp.getOutputStream();
// Only process if this is a recognized MIME type
String type = req.getContentType();
if (type.equals("text/html") || type.equals("text/table") ||
type.equals("application/x-www-form-urlencoded")) {
resp.setContentType("text/html");
// Create a buffered reader that we can use to read
// a single line at a time
BufferedReader br = new BufferedReader(new InputStreamReader(in));
boolean inTable = false;
int tableCols = 0;
boolean headerRow = false;
// Read until no more data exists
while (true) {
String s = br.readLine();
// null indicates end of file
if (s == null) {
break;
}
// If we are in the middle of a table command, process
// the line
if (inTable) {
// Search for the end of the table
if (s.startsWith("
<table width="100%" bgColor=#ffffff>
<tr>
<td>import javax.servlet.*;
import javax.servlet.http.*;
public class ghq extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// Create a PrintWriter to write the response
java.io.PrintWriter out = new java.io.PrintWriter(resp.getOutputStream());
// Set the content type of the response
resp.setContentType("text/html;charset=gb2312");
// Print the HTML header
out.println("");
out.println("<head>");
out.println("Java Servlets Sample - " + "通讯录 ");
out.println("</head>");
out.println("");
out.println("通讯录
");
out.println("
");
// Output special table formatting instructions for
// the TableFilter servlet
out.println("
↑返回目录
前一篇: JBoss4.0数据源配置大全
后一篇: Java Servlet 编程及应用之五