硬件学院 | 网络学院 | 游戏秘籍 | 求职技巧 | 企业管理 | 软件资讯 | IT导购 | 软件下载 | 源码下载
软件学院 | 安全资讯 | 图形图象 | 网络营销 | 电子商务 | 硬件资讯 | IT生活 | 教程下载 | 电影娱乐
网站首页 个人求职 单位招聘 高校联盟 猎头服务 培训服务 资讯中心 IT论坛
让每一个热爱IT的人都找到一份满意的工作!
文章搜索:
您的位置首页->-> 软件学院-> JSP技术-> Servlet基础例程 - HelloServlet - NT版本
Servlet基础例程 - HelloServlet - NT版本
作者:中国资讯网 来源:zixuen.com 加入时间:2005-5-12 www.cnitrc.com
/*
作者:何志强[hhzqq@21cn.com]
日期:2000-08-10
版本:1.0
功能: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;charset=gb2312");

      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 = "无名氏";
      else
         //对用户输入的数据作必要的字符编码转换
         name = new String(name.getBytes("iso-8859-1"));

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

      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;charset=gb2312");

      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();
   }
}
相关文章:
JSP技术
ASP技术
PHP技术
JSP技术
.NET技术
服务器技术
数据库技术
其它类
工具软件
办公软件
本类阅读TOP10
关于我们 | 服务声明 | 使用帮助 | 广告合作 | 网站地图 | 友情链接 | 加盟合作 | 联系我们
Copyright ? 2006 cnitrc.com Inc. All Rights Reserved. 浙ICP备05074295号
中国IT人才网 版权所有 网络实名:中国IT人才
未经书面授权严禁转载和复制本站的任何招聘信息和文章