创建HttpServlet实例
( 1)创建自己的类并继承HttpServlet
(2)重载HttpServlet的doGet()或doPost()等方法
(3)获取HTTP请求信息,进行业务逻辑处理
(4)生成HTTP响应结果,以流形式输出到客户端浏览输出到磁盘文件
(5)在web.xml配置文件中注册HttpServlet
(6)启动Servlet容器进行测试
Servlet生命周期分为三个阶段:
1,初始化阶段 调用init()方法
2,响应客户请求阶段 调用service()方法
3,终止阶段 调用destroy()方法
*************************
MyServlet
*************************
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
public MyServlet() {//构造函数
super();
}
public void destroy() {//销毁时将调用的方法
super.destroy();
}
//处理以GET方式提交过来的表单数据
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);//直接调用doPost方法
}
//处理以Post方式提交上来的表单数据
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");//设置响应采用的编码方式
String name = request.getParameter("name");//从提交上来的表单中取name文本框的值
PrintWriter out = response.getWriter();
ServletContext context = getServletContext();//得到整个Web应用的ServletContext对象
int count = 1;//从1开始起计
if (context.getAttribute("count")==null){//如果是第一位访客
context.setAttribute("count",new Integer(count));//设置计数器初始值
}else{//如果不是第一位访客
count = Integer.parseInt(context.getAttribute("count").toString());//取出现有的计数值
count++;//计数加1
context.setAttribute("count",new Integer(count));//更新计数器内容
}
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(name+", 你好!你是第"+count+"位访客!");//实现简单问好
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
//本Servlet装载到容器后将自动执行的初始化方法
public void init() throws ServletException {
// Put your code here
}
}
*************************************
web.xml
************************************
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
*********************************
index.jsp
********************************
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<title>测试Servlet页面</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<FORM name="form1" action="MyServlet" method="post">
请输入姓名:
<INPUT name="name" type="text">
<INPUT name="submit" value="提交" type="submit">
</FORM>
</body>
</body>
</html>