Servlet——开发Servlet三种方法详解

该博客主要关于java servlet编程,开发servlet三种方法

该篇博客目录

1、实现servlet接口
2、继承GenericServlet
3、继承HttpServlet
4、用户登录(利用HttpServlet)

关于目录前三条,都是开发servlet的方法,发展过程按目录先后次序,目前HttpServlet最广泛

有一点要注意,每写一个servlet都要部署一次


一、实现servlet接口——开发servlet

开发servlet要引入servlet-api.jar

该接口中有三个生命周期

  • init():该init函数用于初始化该servlet,该函数只会被调用一次(当用户第一次访问该servlet时被调用)
  • destroy():销毁servlet实例(释放内存),关闭tomcat是调用
  • service:service函数用于处理业务逻辑,业务逻辑代码都写在这,当用户访问servlet时,都会调用该函数

还有两个普通方法

  • getServletInfo():获取servlet信息,可选项
  • getServletConfig:获取servlet配置信息,可选项

hello.java

package cn.linjie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class hello implements Servlet{

    //销毁servlet实例(释放内存),关闭tomcat是调用
    @Override
    public void destroy() {
        System.out.println("destroy");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    //该init函数用于初始化该servlet,该函数只会被调用一次(当用户第一次访问该servlet时被调用)
    @Override
    public void init(ServletConfig arg0) throws ServletException {
        System.out.println("init it");
    }

    //service函数用于处理业务逻辑,业务逻辑代码都写在这
    //当用户访问servlet时,都会调用该函数
    //res:用于获得客户端(浏览器)的信息
    //ron:用于向客户端(浏览器)返回信息
    @Override
    public void service(ServletRequest res, ServletResponse ron) throws ServletException, IOException {
        System.out.println("service");

        //打印在浏览器上
        PrintWriter pw=ron.getWriter();
        pw.println("hello,this is servlet");
    }

}

web.xml(与lib同级)




  hello
  
  
    
    name
    
    cn.linjie.hello
  
  
    
    name
    
    /sp
  

servlet生命周期有四个阶段

  • 加载和实例化:由servlet容器负责
  • 初始化:init
  • 请求处理:service
  • 服务终止:destroy

Servlet——开发Servlet三种方法详解_第1张图片

结果

Servlet——开发Servlet三种方法详解_第2张图片


二、继承GenericServlet——开发servlet

只需要重写service方法

helloGen.java

package cn.linjie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;


public class helloGen extends GenericServlet  {

    @Override
    public void service(ServletRequest res, ServletResponse ron) throws ServletException, IOException {
        System.out.println("service..");
        PrintWriter pw=ron.getWriter();
        pw.println("hello,this is a GenericServlet");
    }
}

web.xml




  hello
  
  
    
    name
    
    cn.linjie.hello
  
  
    
    name
    
    /sp
  

  
    name2
    cn.linjie.helloGen
  
  
    name2
    /sp2
  

Servlet——开发Servlet三种方法详解_第3张图片


继承HttpServlet——开发servlet

需要重写doGet和doPost方法

表单提交数据get请求与post请求的区别

  • 安全性:get

helloHttp.java

package cn.linjie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/helloHttp")
public class helloHttp extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public helloHttp() {
        super();
    }

    /*
     *doGet写业务逻辑 
     *doPost调用doGet 
     */

    //处理get请求
    //res获取浏览器信息
    //ron向浏览器返回信息
    protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
        PrintWriter pw=ron.getWriter();
        pw.println("hello,this is a HttpServlet");
    }

    //处理post请求
    //为什么doGet合可以写在doPost中,因为两个方法代码一样,这样调用可以避免代码重复
    protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
        doGet(res, ron);
    }

}

web.xml




  hello
  
  
    
    name
    
    cn.linjie.hello
  
  
    
    name
    
    /sp
  

  
    name2
    cn.linjie.helloGen
  
  
    name2
    /sp2
  

    
    name3
    cn.linjie.helloHttp
  
  
    name3
    /sp3
  

Servlet——开发Servlet三种方法详解_第4张图片

四、用户登录开发(HttpServlet)

用户登录框架

Servlet——开发Servlet三种方法详解_第5张图片

logindemo.java

package cn.linjie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录界面
 */
@WebServlet("/helloHttp")
public class logindemo extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public logindemo() {
        super();
        // TODO Auto-generated constructor stub
    }

    /*
     *doGet写业务逻辑 
     *doPost调用doGet 
     */

    //处理get请求
    //res获取浏览器信息
    //ron向浏览器返回信息
    protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {

        //处理中文乱码
        ron.setContentType("text/html;charset=utf-8");

        PrintWriter pw=ron.getWriter();

        pw.println("");
        pw.println("");
        pw.println("登录");
        pw.println("

登录

"); pw.println("
"); pw.println("用户名
"); pw.println("密码
"); pw.println("
"); pw.println("
"); pw.println(""); pw.println(""); } //处理post请求 //为什么doGet合可以写在doPost中,因为两个方法代码一样,这样调用可以避免代码重复 protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException { doGet(res, ron); } }

loginC.java

package cn.linjie;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录验证
 */
@WebServlet("/loginC")
public class loginC extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginC() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
        //接受用户名和密码(res是获取浏览器信息)
        String u=res.getParameter("username");
        String p=res.getParameter("passwd");

        //验证
        if(u.equals("xlj")&&p.equals("123"))
        {
            //合法
            //跳转
            ron.sendRedirect("welcome");
        }else {
            //不合法
            //跳转(参数是跳转至的url)
            ron.sendRedirect("login");
        }
    }


    protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
        doGet(res, ron);
    }

}

Wlcome.java

package cn.linjie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Welcome
 */
@WebServlet("/Welcome")
public class Welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Welcome() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
        // TODO Auto-generated method stub
        PrintWriter pw=ron.getWriter();
        pw.println("welcome");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest res, HttpServletResponse ron) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(res, ron);
    }

}

web.xml



  
    logindemo
    cn.linjie.logindemo
  
  
    logindemo
    /login
  
    
    loginC
    cn.linjie.loginC
  
  
    loginC
    /loginc
  
    
    Welcome
    cn.linjie.Welcome
  
  
    Welcome
    /welcome
  

Servlet——开发Servlet三种方法详解_第6张图片

Servlet——开发Servlet三种方法详解_第7张图片

你可能感兴趣的:(●,Java基础)