Article View: cn.bbs.comp.lang.java
Article #23184������̱����еĴ���
From: �ٷ���ǿ�
Date: Mon, 11 Aug 2003 02:32
Date: Mon, 11 Aug 2003 02:32
105 lines
4336 bytes
4336 bytes
�������������14��26��36��118�й���6���������Ϻ��ָ�㣬лл
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) throw
s ServletException,IOException{
process(req,resp);
}
protected void process(HttpServletRequest req,HttpServletResponse resp) thro
ws 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) th
rows 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) t
hrows 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,HttpServletRe
sponse 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();
}
}��
--
[m[1;35m�� ��Դ:������δ��վ bbs.pku.edu.cn��[FROM: 218.5.0.233][m
Technical Details
Message-ID:
<47biLl$A0Q@bbs.pku.edu.cn>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!news-out.visi.com!petbe.visi.com!newsfeed.media.kyoto-u.ac.jp!newsgate.cuhk.edu.hk!news.zixia.net!news.happynet.org!PKUShow Full Headers
Complete Article Headers
X-Google-Language: INDONESIAN,ASCII X-Google-Thread: f6cb0,5d95a95e5d1d85b8,start X-Google-Attributes: gidf6cb0,public X-Google-ArrivalTime: 2003-08-10 19:33:05 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!news-out.visi.com!petbe.visi.com!newsfeed.media.kyoto-u.ac.jp!newsgate.cuhk.edu.hk!news.zixia.net!news.happynet.org!PKU From: xingkongsky@bbs.pku.edu.cn (�ٷ���ǿ�) Newsgroups: cn.bbs.comp.lang.java Subject: ������̱����еĴ��� Date: 11 Aug 2003 02:32:48 GMT Organization: ����δ��վ Message-ID: <47biLl$A0Q@bbs.pku.edu.cn> Mime-Version: 1.0 Content-Type: text/plain; charset="gb2312" Content-Transfer-Encoding: 8bit X-Filename: Java/M.1060569119.A Lines: 105