?? servlet基礎例程 - helloservlet - nt版本.txt
字號:
作者:何志強
email: hhzqq@21cn.com
日期:2000-8-10 17:06:20
/*
作者:何志強[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();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -