大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> JSP专区 -> Servlet基础例程 - HelloServlet - NT版本

Servlet基础例程 - HelloServlet - NT版本

时间: 2021-07-31 作者:daque

/*作家:何志强[hhzqq@21cn.com]日子:2000-08-10本子:1.0功效:servlet普通例程 - helloservlet*/import java.io.*;import java.text.*; //messageformatimport 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=utf8\">"+"<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 typeresp.setcontenttype("text/html;charset=utf8");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 typeresp.setcontenttype("text/html;charset=utf8");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 typeresp.setcontenttype("text/html;charset=utf8");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(); }}

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.