服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

servlet基础例程-helloservlet(linux版本)


  servlet基础例程-helloservlet(linux版本)
http://www.netqu.com 中华技术网会员 wuxuehui 发布

/*
作者:何志强[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");

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 = "无名氏";

//在使用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,httpservletresponse 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();
}
}

扫描关注微信公众号