国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Servlet基礎(chǔ)例程-HelloServlet(Linux版本)

2019-11-18 15:08:29
字體:
供稿:網(wǎng)友

  Servlet基礎(chǔ)例程-HelloServlet(linux版本)
http://www.netqu.com 中華技術(shù)網(wǎng)會員 wuxuehui 發(fā)布

/*
作者:何志強(qiáng)[hhzQQ@21cn.com]
功能:Servlet基礎(chǔ)例程 - HelloServlet
*/

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

public class HelloServlet extends HttpServlet{
//頁面標(biāo)題
PRotected static final String strTitle = "Servlet基礎(chǔ)例程 - 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世界!";

//出錯(cuò)信息
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前得先設(shè)置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{
//獲取用戶輸入的數(shù)據(jù)
String name = req.getParameter("name");

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

//在使用PrintWriter前得先設(shè)置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前得先設(shè)置Content Type
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

//輸出頁眉
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 出錯(cuò)信息"}));

//輸出出錯(cuò)信息
out.print(MessageFormat.format(strError,new Object[]{error}));

//輸出頁腳
out.print(strFooter);

out.flush();
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 盐城市| 昌宁县| 临猗县| 临潭县| 保德县| 平南县| 额尔古纳市| 远安县| 铜鼓县| 吐鲁番市| 潢川县| 延长县| 正宁县| 南宁市| 呼图壁县| 舒兰市| 和平区| 五寨县| 民丰县| 庄浪县| 增城市| 林西县| 革吉县| 宁波市| 新蔡县| 渭源县| 贡嘎县| 新源县| 延长县| 北流市| 顺昌县| 平阳县| 当阳市| 高邮市| 清涧县| 宿州市| 彝良县| 青岛市| 曲松县| 隆安县| 东阿县|