网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.Java技巧:列表排序.
.使用.NET框架类替代API调用的问题.
.21种代码的“坏味道”.
.新版JUnit 4.0 抢先体验.
.类的设计方法.
.java中有关日期的显示问题.
.String类使用的例子(1).
.除法运算符 (/).
.Test of the Java Skill(2).
.用Java生成文本文件.
.java api之实现(上).
.Resin服务器的使用.
.用TikeSwing框架开发Java应用的表.
.J2EE1.4新特性之Servlet2.4新特性.
.[JAVA100例]069、使用SAX解析XML.
.XDoclet:EJB开发好帮手.
.Java Servlet及Cookie的使用.
.CORBA对象生命周期之早期和后期绑.
.Spring i18n的better .
.怎样锁右键,和解开右键?.

Servlet基础例程-HelloServlet(Linux版本)

发表日期:2008-1-5



  Servlet基础例程-HelloServlet(Linux版本)
http://www.netqu.com 中华技术网会员 wuxuehui 发布

/*
作者:何志强[hhzQQ@21cn.com]
功能:Servlet基础例程 - HelloServlet
*/

import Java.io.*;
import java.text.*; //MessageFormat
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloServlet extends HttpServlet{
//页面标题
protected static final String strTitle = "Servlet基础例程 - HelloServlet";

//页眉
protected static final String strHeader =
"<Html>"+
"<head>"+
"<meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312"">"+
"<title>{0}</title>"+
"</head>"+
"<body>";

//页脚
protected static final String strFooter =
"</body>"+
"</html>";

//表单
protected static final String strForm =
"<form action=""{0}"" method=""post"">"+
"您尊姓大名:<input type=""text"" name=""name"">"+
"<input type=""submit"" name=""submit"" value=""提交"">"+
"</form>";

protected static final String strHello =
"您好,{0},欢迎来到Servlet/jsp世界!";

//出错信息
protected static final String strError =
"<h2><font color=""#ff0000"">{0}</font></h2>";

protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
process(req,resp);
}

protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
process(req,resp);
}

protected void process(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
try{
String submit = req.getParameter("submit");
if(submit==null)
printForm(req,resp);
else
printHello(req,resp);
}
catch(Exception e){
printError(e.toString(),req,resp);
}
}

protected void printForm(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//在使用PrintWriter前得先设置Content Type
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

//输出页眉
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 请输入尊姓大名"}));

//输出表单
out.print(MessageFormat.format(strForm,new Object[]{req.getContextPath()+req.getServletPath()}));

//输出页脚
out.print(strFooter);

out.flush();
}

protected void printHello(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//获取用户输入的数据
String name = req.getParameter("name");

if(name==null)
name = "无名氏";

//在使用PrintWriter前得先设置Content Type
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

//输出页眉
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 欢迎您"}));

//输出欢迎信息
out.print(MessageFormat.format(strHello,new Object[]{name}));

//输出页脚
out.print(strFooter);

out.flush();
}

protected void printError(String error, HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
//在使用PrintWriter前得先设置Content Type
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

//输出页眉
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 出错信息"}));

//输出出错信息
out.print(MessageFormat.format(strError,new Object[]{error}));

//输出页脚
out.print(strFooter);

out.flush();
}
}
上一篇:Servlet基础例程-HelloServlet(NT版本) 人气:554
下一篇:Servlet的优越性 人气:491
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐