欢迎光临
我们一直在努力

Servlet基础例程 – HelloServlet – Linux版本-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

/*
作者:何志强[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");

      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();
   }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Servlet基础例程 – HelloServlet – Linux版本-JSP教程,Jsp/Servlet
分享到: 更多 (0)

相关推荐

  • 暂无文章