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

首頁 > 學院 > 開發設計 > 正文

Servlet基礎例程-HelloServlet(NT版本)

2019-11-18 15:08:29
字體:
來源:轉載
供稿:網友

  /*
作者:何志強[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;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();
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宽城| 灵川县| 江达县| 巧家县| 洪洞县| 休宁县| 伊川县| 广丰县| 泸溪县| 邢台市| 宁明县| 安泽县| 大余县| 酒泉市| 广宗县| 东港市| 三门峡市| 宣武区| 南阳市| 宾川县| 科尔| 清新县| 会同县| 桂平市| 新和县| 九龙县| 金沙县| 兴海县| 龙州县| 南昌市| 买车| 大埔县| 乡宁县| 黄冈市| 清水县| 阿鲁科尔沁旗| 宝应县| 南京市| 陆丰市| 增城市| 壶关县|