什么是servlet ?
一个
servlet
就是
Java
编
程
语
言中的一个
类
,
是用
Java
编
写的
Server
端程序
.
客户机提交
”
请求
”
到服务器
,
服务器中的
servlet
对请求进行处理
,
将处理的结果反馈回客户机
(
即
”
响应
”
客户机的请求
),
这就是
servlet
在
“
请
求
-
响
应
”
编
程模型
中扮演的角色
,
它被用来
扩
展服
务
器的性能
。服
务
器
上
驻
留着可以通
过
“
请
求
-
响
应
”
编
程模型来
访问
的
应
用程序。
虽
然
servlet
可以
对
任何
类
型的
请
求
产
生响
应
,但通常只用来
扩
展
Web
服
务
器
中
的
应
用程序
。
编写并运行你的Servlet!
当一个servlet已经写好怎样来运行测试呢?下面通过一个例子详细的说说,这样会有一个感性的把握。我用的运行环境是Tomcat5.0
首先我们来写一个最简单的servlet: 命名为HelloServlet.java
package test;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out=response.getWriter();
out.println("<HTML>");
out.println("<BODY>");
out.println("<p>Hello!这是我的第一个Java Servlet程序。</p>");
out.println("</BODY>");
out.println("</HTML>");
}
}
把编译好的HelloServlet.class文件放到\Tomcat文件夹\webapps\ourappfiles\WEB-INF\classes\test的文件夹下。
接着编写我们的web.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>helloservlet</servlet-name>
<servlet-class>test.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloservlet</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
把编写好的web.xml文件放到\Tomcat文件夹\webapps\ourappfiles\WEB-INF下。
好了,现在万事俱备,就差启动Tomcat运行我们的Servlet了。
在IE地址栏中输入http://localhost:8080/ourappfiles/Hello,就可以看到结果了。
以上就是运行Servlet的几个步骤。
通过这个例子可以看出纯粹的Servlet编程的缺点.所有的内容------甚至是基本的HTML格式------都必须由Servlet输出,你不得不在Servlet中放置大量的print语句来结束,唯一的目的就是向用户返回内容.另外,甚至最简单的HTML格式变化都必须在JAVA源代码中解决,这意味着非JAVA开发人员不能进行WEB站点的设计.
为了克服上述缺点, JavaServer Pages(JSP)应运而生. JSP表面上看是"嵌入了Java 的HTML", 而实际上JSP是"嵌入了HTML的Java Servlet ". 要明白这一点,需要了解一下JSP是如何响应请求的.
当浏览器向服务器请求一个JSP页面(A.JSP)时, 源页面(A.JSP)通过转换器(在TOMCAT中,这个转换器叫做Jasper)被转换成一个Java类(如:A_jsp.java),该类继承于org.apache.jasper.runtime.HttpJspBase基类,而HttpJspBase实现了HttpServlet接口。可见,JSP在运行前首先被编译为一个Servlet, 这个Servlet对请求进行处理,将结果内容发送回客户端.所以说, JSP是"嵌入了HTML的Java Servlet ", 其本质就是Servlet.
如果将上面的servlet程序中的doGet方法写成下面的样子:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/vnd.ms-excel");
PrintWriter out = response.getWriter();
out.println("\tQ1\tQ2\tQ3\tQ4\tTotal");
out.println("Apples\t78\t87\t92\t29\t=SUM(B2:E2)");
out.println("Oranges\t77\t86\t93\t30\t=SUM(B3:E3)");
}
运行后,就可以输出一个excel文件啦!
附: 对
Content-type的详细说明:Content-type 的说明 <?php
$mimetypes = array(
'ez' => 'application/andrew-inset',
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'doc' => 'application/msword',
'bin' => 'application/octet-stream',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'so' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => 'application/pdf',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'wbxml' => 'application/vnd.wap.wbxml',
'wmlc' => 'application/vnd.wap.wmlc',
'wmlsc' => 'application/vnd.wap.wmlscriptc',
'bcpio' => 'application/x-bcpio',
'vcd' => 'application/x-cdlink',
'pgn' => 'application/x-chess-pgn',
'cpio' => 'application/x-cpio',
'csh' => 'application/x-csh',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'spl' => 'application/x-futuresplash',
'gtar' => 'application/x-gtar',
'hdf' => 'application/x-hdf',
'js' => 'application/x-javascript',
'skp' => 'application/x-koan',
'skd' => 'application/x-koan',
'skt' => 'application/x-koan',
'skm' => 'application/x-koan',
'latex' => 'application/x-latex',
'nc' => 'application/x-netcdf',
'cdf' => 'application/x-netcdf',
'sh' => 'application/x-sh',
'shar' => 'application/x-shar',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'sv4cpio' => 'application/x-sv4cpio',
'sv4crc' => 'application/x-sv4crc',
'tar' => 'application/x-tar',
'tcl' => 'application/x-tcl',
'tex' => 'application/x-tex',
'texinfo' => 'application/x-texinfo',
'texi' => 'application/x-texinfo',
't' => 'application/x-troff',
'tr' => 'application/x-troff',
'roff' => 'application/x-troff',
'man' => 'application/x-troff-man',
'me' => 'application/x-troff-me',
'ms' => 'application/x-troff-ms',
'ustar' => 'application/x-ustar',
'src' => 'application/x-wais-source',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => 'application/zip',
'au' => 'audio/basic',
'snd' => 'audio/basic',
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'kar' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => 'audio/mpeg',
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'm3u' => 'audio/x-mpegurl',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'wav' => 'audio/x-wav',
'pdb' => 'chemical/x-pdb',
'xyz' => 'chemical/x-xyz',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'ief' => 'image/ief',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'png' => 'image/png',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'djvu' => 'image/vnd.djvu',
'djv' => 'image/vnd.djvu',
'wbmp' => 'image/vnd.wap.wbmp',
'ras' => 'image/x-cmu-raster',
'pnm' => 'image/x-portable-anymap',
'pbm' => 'image/x-portable-bitmap',
'pgm' => 'image/x-portable-graymap',
'ppm' => 'image/x-portable-pixmap',
'rgb' => 'image/x-rgb',
'xbm' => 'image/x-xbitmap',
'xpm' => 'image/x-xpixmap',
'xwd' => 'image/x-xwindowdump',
'igs' => 'model/iges',
'iges' => 'model/iges',
'msh' => 'model/mesh',
'mesh' => 'model/mesh',
'silo' => 'model/mesh',
'wrl' => 'model/vrml',
'vrml' => 'model/vrml',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'asc' => 'text/plain',
'txt' => 'text/plain',
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'sgml' => 'text/sgml',
'sgm' => 'text/sgml',
'tsv' => 'text/tab-separated-values',
'wml' => 'text/vnd.wap.wml',
'wmls' => 'text/vnd.wap.wmlscript',
'etx' => 'text/x-setext',
'xsl' => 'text/xml',
'xml' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'mxu' => 'video/vnd.mpegurl',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'ice' => 'x-conference/x-cooltalk',
);
posted on 2006-07-03 13:40
小言身寸 阅读(988)
评论(0) 编辑 收藏 所属分类:
JAVA相关技术