快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

SERVLET基础例程-HELLOSERVLET(LINUX版本)

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();
}
}
返回类别: 教程
上一教程: 使用JDBC创建数据库对象
下一教程: JAVA中使用JCOM操作OFFICE对象

您可以阅读与"SERVLET基础例程-HELLOSERVLET(LINUX版本)"相关的教程:
· Dukes Bookstore(Servlet 版本)笔记
· SERVLET开发中JDBC的高级应用
· J2EE基础概念
· Java Servlet的使用
· JAVA程序员必读:基础篇(1)
    微笑服务 优质保证 索取样品