📄 0090.htm
字号:
<html>
<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1 {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>Servlet基础例程 - HelloServlet - Linux版本</strong></big></p>
<div align="right">(文/何志强)</div>
<p><span class=smallFont>
/*<br>
作者:何志强[hhzqq@21cn.com]<br>
日期:2000-08-10<br>
版本:1.0<br>
功能:Servlet基础例程 - HelloServlet<br>
*/<br>
<br>
import java.io.*;<br>
import java.text.*; //MessageFormat<br>
import javax.servlet.*;<br>
import javax.servlet.http.*;<br>
<br>
public class HelloServlet extends HttpServlet{<br>
//页面标题<br>
protected static final String strTitle = "Servlet基础例程 - HelloServlet";<br>
<br>
//页眉<br>
protected static final String strHeader =<br>
"<html>"+<br>
"<head>"+<br>
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"+<br>
"<title>{0}</title>"+<br>
"</head>"+<br>
"<body>";<br>
<br>
//页脚<br>
protected static final String strFooter =<br>
"</body>"+<br>
"</html>";<br>
<br>
//表单<br>
protected static final String strForm =<br>
"<form action=\"{0}\" method=\"post\">"+<br>
"您尊姓大名:<input type=\"text\" name=\"name\">"+<br>
"<input type=\"submit\" name=\"submit\" value=\"提交\">"+<br>
"</form>";<br>
<br>
protected static final String strHello =<br>
"您好,{0},欢迎来到Servlet/JSP世界!";<br>
<br>
//出错信息<br>
protected static final String strError =<br>
"<h2><font color=\"#ff0000\">{0}</font></h2>";<br>
<br>
protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{<br>
process(req,resp);<br>
}<br>
<br>
protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{<br>
process(req,resp);<br>
}<br>
<br>
protected void process(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{<br>
try{<br>
String submit = req.getParameter("submit");<br>
if(submit==null)<br>
printForm(req,resp);<br>
else<br>
printHello(req,resp);<br>
}<br>
catch(Exception e){<br>
printError(e.toString(),req,resp);<br>
}<br>
}<br>
<br>
protected void printForm(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{<br>
//在使用PrintWriter前得先设置Content Type<br>
resp.setContentType("text/html");<br>
<br>
PrintWriter out = resp.getWriter();<br>
<br>
//输出页眉<br>
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 请输入尊姓大名"}));<br>
<br>
//输出表单<br>
out.print(MessageFormat.format(strForm,new Object[]{req.getContextPath()+req.getServletPath()}));<br>
<br>
//输出页脚<br>
out.print(strFooter);<br>
<br>
out.flush();<br>
}<br>
<br>
protected void printHello(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{<br>
//获取用户输入的数据<br>
String name = req.getParameter("name");<br>
<br>
if(name==null)<br>
name = "无名氏";<br>
<br>
//在使用PrintWriter前得先设置Content Type<br>
resp.setContentType("text/html");<br>
<br>
PrintWriter out = resp.getWriter();<br>
<br>
//输出页眉<br>
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 欢迎您"}));<br>
<br>
//输出欢迎信息<br>
out.print(MessageFormat.format(strHello,new Object[]{name}));<br>
<br>
//输出页脚<br>
out.print(strFooter);<br>
<br>
out.flush();<br>
}<br>
<br>
protected void printError(String error, HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{<br>
//在使用PrintWriter前得先设置Content Type<br>
resp.setContentType("text/html");<br>
<br>
PrintWriter out = resp.getWriter();<br>
<br>
//输出页眉<br>
out.print(MessageFormat.format(strHeader,new Object[]{strTitle+" - 出错信息"}));<br>
<br>
//输出出错信息<br>
out.print(MessageFormat.format(strError,new Object[]{error}));<br>
<br>
//输出页脚<br>
out.print(strFooter);<br>
<br>
out.flush();<br>
}<br>
}
</table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -