(1)方法一:
- //这是第一个实现servlet的方法,使用时限servlet接口的方法来实现,使用的时候需要引用servlet-api.jar
- package com.lc;
- import javax.servlet.*;
- import java.io.*;
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
- import java.io.IOException;
- public class Hello implements Servlet{
- //该函数用于初始化该servlet(类似于类的构造函数)
- //该函数只会被调用一次(当用户第一次访问该servlet是被调用)
- public void init(ServletConfig parm1) throws ServletException {
- System.out.println("init it!");
- }
- public ServletConfig getServletConfig() {
- return null;
- }
- //这个函数用于处理业务逻辑
- //程序员应当那个当业务逻辑逻辑代码写在这里
- //当用户访问servlet时都会被调用
- //req用于获得客户端的信息
- //res用户想客户端返回信息 (可以认为是游览器)
- public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
- //从res中得到Printwriter
- System.out.println("service it!"); //在控制台显示
- PrintWriter pw = res.getWriter();
- pw.println("hello world!");//在游览器中显示
- }
- public String getServletInfo() {
- return "";
- }
- //三种情况导致destroy的调用:
- //1.reload 该servlet(webApps)
- //2.关闭tomcat
- //3.关机
- public void destroy() {
- //销毁servt实例 实际就是释放内存
- System.out.println("destroy it!");
- }
- }
(2)方法二:
- //第二个实现servlet程序,继承GenericServlet 这种方法很简单
- package com.tsinghua;
- import javax.servlet.GenericServlet;
- import java.io.*;
- import javax.servlet.*;
- public class HelloGenericServlet extends GenericServlet{
- //重写service方法即可
- public void service(ServletRequest req,ServletResponse res){
- //返回hello world!generic
- try{
- PrintWriter pw =res.getWriter();
- pw.println("Hello world! generic!");
- }
- catch(Exception ex){
- ex.printStackTrace();
- }
- }
- }
(3)方法三:
- //第三个servlet实现程序,通过继承HttpServlet
- package com.tsinghua;
- import javax.servlet.http.*;
- import java.io.*;
- public class HelloHttp extends HttpServlet{
- //处理get请求
- //req:用于获得客户端(游览器)的信息
- //res:用于向客户端(游览器)返回信息
- public void doGet(HttpServletRequest req,HttpServletResponse res){
- //业务逻辑
- try{
- PrintWriter pw = res.getWriter();
- pw.println("hello http!");
- }
- catch(Exception ex){
- ex.printStackTrace();
- }
- }
- //处理post请求
- //req:用于获得客户端(游览器)的信息
- //res:用于向客户端(游览器)返回信息
- public void doPost(HttpServletRequest req,HttpServletResponse res){
- this.doGet(req,res);
- }
- }
在实现上述三种方法的时候 需要配置web.xml文件,代码如下:
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <web-app 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_3_0.xsd"
- version="3.0"
- metadata-complete="true">
- <display-name>Welcome to Tomcat</display-name>
- <description>
- Welcome to Tomcat
- </description>
- <!--JSP servlet mappings starts-->
- <servlet>
- <!--给自己的servlet起个名字 任意的名字-->
- <servlet-name>Hello</servlet-name>
- <!--指明servlet的路径(包名+类名)-->
- <servlet-class>com.tsinghua.Hello</servlet-class>
- </servlet>
- <servlet-mapping>
- <!--和上边的servlet的名字是一样的-->
- <servlet-name>Hello</servlet-name>
- <!--这是在游览器中输入的访问该servlet的url-->
- <url-pattern>/helloworld</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>HelloGenericServlet</servlet-name>
- <servlet-class>com.tsinghua.HelloGenericServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>HelloGenericServlet</servlet-name>
- <url-pattern>/helloGeneric</url-pattern>
- </servlet-mapping>
- <servlet>
- <servlet-name>HelloHttp</servlet-name>
- <servlet-class>com.tsinghua.HelloHttp</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>HelloHttp</servlet-name>
- <url-pattern>/helloHttp</url-pattern>
- </servlet-mapping>
- <!--JSP servlet mappings end-->
- </web-app>