马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大.servlet/*
何志强[hhzqq@21cn.com]
日期:2000-08-10
版本:1.0
功效:Servlet基本例程-HelloServlet
*/
importjava.io.*;
importjava.text.*;//MessageFormat
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassHelloServletextendsHttpServlet{
//页面题目
protectedstaticfinalStringstrTitle="Servlet基本例程-HelloServlet";
//页眉
protectedstaticfinalStringstrHeader=
"<html>"+
"<head>"+
"<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">"+
"<title>{0}</title>"+
"</head>"+
"<body>";
//页脚
protectedstaticfinalStringstrFooter=
"</body>"+
"</html>";
//表单
protectedstaticfinalStringstrForm=
"<formaction="{0}"method="post">"+
"您贵姓台甫:<inputtype="text"name="name">"+
"<inputtype="submit"name="submit"value="提交">"+
"</form>";
protectedstaticfinalStringstrHello=
"您好,{0},接待离开Servlet/JSP天下!";
//堕落信息
protectedstaticfinalStringstrError=
"<h2><fontcolor="#ff0000">{0}</font></h2>";
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
process(req,resp);
}
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
process(req,resp);
}
protectedvoidprocess(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
try{
Stringsubmit=req.getParameter("submit");
if(submit==null)
printForm(req,resp);
else
printHello(req,resp);
}
catch(Exceptione){
printError(e.toString(),req,resp);
}
}
protectedvoidprintForm(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//在利用PrintWriter前得先设置ContentType
resp.setContentType("text/html;charset=gb2312");
PrintWriterout=resp.getWriter();
//输入页眉
out.print(MessageFormat.format(strHeader,newObject[]{strTitle+"-请输出贵姓台甫"}));
//输入表单
out.print(MessageFormat.format(strForm,newObject[]{req.getContextPath()+req.getServletPath()}));
//输入页脚
out.print(strFooter);
out.flush();
}
protectedvoidprintHello(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//猎取用户输出的数据
Stringname=req.getParameter("name");
if(name==null)
name="知名氏";
else
//对用户输出的数据作需要的字符编码转换
name=newString(name.getBytes("iso-8859-1"));
//在利用PrintWriter前得先设置ContentType
resp.setContentType("text/html;charset=gb2312");
PrintWriterout=resp.getWriter();
//输入页眉
out.print(MessageFormat.format(strHeader,newObject[]{strTitle+"-接待您"}));
//输入接待信息
out.print(MessageFormat.format(strHello,newObject[]{name}));
//输入页脚
out.print(strFooter);
out.flush();
}
protectedvoidprintError(Stringerror,HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{
//在利用PrintWriter前得先设置ContentType
resp.setContentType("text/html;charset=gb2312");
PrintWriterout=resp.getWriter();
//输入页眉
out.print(MessageFormat.format(strHeader,newObject[]{strTitle+"-堕落信息"}));
//输入堕落信息
out.print(MessageFormat.format(strError,newObject[]{error}));
//输入页脚
out.print(strFooter);
out.flush();
}
}
大型的应用一般不会用这些框架(因为性能考虑);开发人员根据需要选择用一些框架,也可以不选用框架;不用框架并不代表要自己写框架;修改框架的可能性更小。 |