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();
}
}保留:: http://www.qqread.com/java/2006/10/f245948.html
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- Linux集群技术 (8416篇文章)
- 体验Linux的音影世界 (8088篇文章)
- Linux驱动大全 (8891篇文章)
- Linux下的路由的配置与应用 (11888篇文章)
- Linux命令简介 (9952篇文章)
- Linux防火墙 (9769篇文章)
- Linux日志专题 (8542篇文章)
- Linux服务器的安全性能 (20541篇文章)
- 揭秘Linux内存管理 (8152篇文章)
- 解析Linux文件系统 (8354篇文章)
- 精通Hibernate之映射继承关系(四) (0次浏览)
- 何不将Java与.NET合二为一? (0次浏览)
- Hibernate的JNDI名称绑定分析 (0次浏览)
- 精通Hibernate之映射继承关系(一) (0次浏览)
- 精通Hibernate之映射继承关系(二) (0次浏览)
- 美国计算机教授语出惊人:Java对学生有害 (0次浏览)
- JDK 6 JRE 6 Update 4 (0次浏览)
- 三步教你改善Java代码质量 (0次浏览)
- Java语言入门 简述Java语言回收机制 (0次浏览)
- 2008年Java开发者最迫切的五个期望 (0次浏览)



