栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

HttpServlet

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HttpServlet

HttpServlet 什么是HttpServlet

HttpServlet是Servlet接口的一个实现类

Servlet框架是由两个Java包组成:javax.servlet和javax.servlet.http。 在javax.servlet包中定义了所有的Servlet类都必须实现或扩展的的通用接口和类,在javax.servlet.http包中定义了采用HTTP通信协议的HttpServlet类

HttpServlet的请求响应流程

Web客户向Servlet容器发出Http请求Servlet容器解析Web客户的Http请求Servlet容器创建一个HttpRequest对象,在这个对象中封装Http请求信息Servlet容器创建一个HttpResponse对象Servlet容器调用HttpServlet的service方法,把HttpRequest和HttpResponse对象作为service方法的参数传给HttpServlet对象HttpServlet调用HttpRequest的有关方法,获取HTTP请求信息HttpServlet调用HttpResponse的有关方法,生成响应数据Servlet容器把HttpServlet的响应结果传给Web客户 如何创建HttpServlet,实现步骤

继承HttpServlet抽象类Override HttpServlet的部分方法,常见的doGet(), doPost()方法获取HTTP请求信息。通过HttpServletRequest对象来检索HTML表单所提交的数据或URL上的查询字符串生成HTTP响应结果。通过HttpServletResponse对象生成响应结果 创建测试项目HttpServletDemo,实现HttpServlet

    在src/main目录下,新建一个package名字随便取,然后在package下新建一个java(Source Root)和resources(Resources Root)两个文件夹

    打开HttpServletDemo/pom.xml配置加载javax.servlet、javax.servlet.jsp资源包

    打开webapp/WEB-INF/web.xml,复制以下内容替换web.xml中的代码




ServletContext

** 什么是ServletContext ?**

ServletContext是一个web应用的上下文对象

ServletContext对象的生命周期 ?

ServletContext中属性的生命周期从创建开始,到服务器关闭结束

如何获得ServletContext ?

# 继承HttpServlet
ServletContext servletContext = this.getServletContext();

1.1 域对象的通用的方法:

setAtrribute(String name,Object obj) # 键值对

getAttribute(String name) # 根据健取值,取到的值为Object类型,需要向下转型

removeAttribute(String name)
创建com.test.servlet/TestHttpServletDemo.java

package com.test.servlet;

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

public class TestHttpServletDemo extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();

        String name = "HttpServlet -> getServletContext -> setAttribute && getAttribute";
        servletContext.setAttribute("name", name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

创建com.test.servlet/TestHttpServletDemo2.java

package com.test.servlet;

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

public class TestHttpServletDemo2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String name = (String) servletContext.getAttribute("name");

        resp.setContentType("text/html; charset=UTF-8");
        resp.getWriter().write(name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

webapp/WEB-INF/web.xml


    demo
    com.test.servlet.TestHttpServletDemo
  
  
    demo
    /demo
  
  
  
    demo2
    com.test.servlet.TestHttpServletDemo2
  
  
    demo2
    /demo2
  

先访问http://localhost:8080/demo/demo
在访问http://localhost:8080/demo/demo2

1.2 获取web初始化参数
创建com.test.servlet/TestHttpServletDemo3.java

package com.test.servlet;

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

public class TestHttpServletDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();

        // 获取web初始化参数
        String db = servletContext.getInitParameter("db-mysql");
        resp.getWriter().println(db);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

webapp/WEB-INF/web.xml

  
  
    db-mysql
    jdbc:mysql://localhost:3306/DatabaseName
  
  
  ......
  
    
    demo3
    com.test.servlet.TestHttpServletDemo3
  
  
    demo3
    /demo3
  

1.3 请求转发 (与重定向 1.5 )
请求转发:客户端发起一次请求,一次响应。客户端向服务端A发起请求,A接到请求后,调用getRequestDispacther 把请求转发给指定的服务端B。整个过程都在服务端完成,而且是在同一个请求里面。因此A和B共享一个Request.

重定向: 客户端发起两次请求,两次响应。客户发送一个请求到服务端A,A接到请求后,向客户端返回个响应,告诉客户端,你必须再发送一个请求到服务端B。紧接着客服端再次发送一个请求到服务端B。两次请求,相互独立,不共享Request

getRequestDispatcher(String s) # s 转发路径
创建com.test.servlet/TestHttpServletDemo4.java

package com.test.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestHttpServletDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();

        RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/demo3"); //转发的请求路径
        requestDispatcher.forward(req, resp); // 调用forward实现请求转发
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

webapp/WEB-INF/web.xml

  
  
    demo4
    com.test.servlet.TestHttpServletDemo4
  
  
    demo4
    /demo4
  

1.4 Properties
load() 从字节输入流中读取键值对
在main目录下创建resources文件夹,并设置为resources root。然后在resources文件夹下创建test.properties

name=test

创建com.test.servlet/TestHttpServletDemo5.java

package com.test.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class TestHttpServletDemo5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/test.properties");

        Properties prop = new Properties();
        prop.load(resourceAsStream);
        String name = prop.getProperty("name");

        resp.getWriter().println(name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

webapp/WEB-INF/web.xml

  
    demo5
    com.test.servlet.TestHttpServletDemo5
  
  
    demo5
    /demo5
  

1.5 重定向
sendRedirect(String s)
创建com.test.servlet/TestHttpServletDemo6.java

package com.test.servlet;

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

public class TestHttpServletDemo6 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("/demo/demo5");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

webapp/WEB-INF/web.xml


    demo6
    com.test.servlet.TestHttpServletDemo6
  
  
    demo6
    /demo6
  

访问:http://localhost:8080/demo/demo6, 会重定向到 http://localhost:8080/demo/demo5

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/764384.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号