【狂神说Java】JavaWeb入门到实战--Servlet详解

“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”

“http://java.sun.com/dtd/web-app_2_3.dtd” >

<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”>

hello

cn.bloghut.servlet.HelloServlet

hello

/hello

配置tomcat

测试

在这里插入图片描述

4.Servlet 原理


Servlet 是由web服务器调用,web服务器在收到浏览器请求之后,会调用相关API对请求处理。

在这里插入图片描述

5.Mapping 问题:


一个请求(Servlet)可以指定一个映射路径

hello

cn.bloghut.servlet.HelloServlet

hello

/hello

一个请求(Servlet)可以指定多个映射路径

hello

cn.bloghut.servlet.HelloServlet

hello

/hello1

hello

/hello2

hello

/hello3

一个请求(Servlet)可以指定通用映射路径

hello

/*

指定一些后缀或者前缀等

hello

*.do

优先级问题

指定了固有的映射路径优先级最高,如果找不到就会走默认的处理请求;

6.ServletContext


web容器在启动的时候,它会为每个web程序都创建一个ServletContext对象,它代表了当前的web应用。

6.1.共享数据

我在这个Servlet中保存的数据

在这里插入图片描述

放置数据的Servlet

/**

  • @author by 闲言

  • @classname HelloServlet

  • @description 放置数据

  • @date 2021/9/11 14:23

*/

public class setData extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

System.out.println(“hello”);

ServletContext servletContext = this.getServletContext();

String username = “闲言”;//数据

servletContext.setAttribute(“username”,username);//将一个数据保存在ServletContext中 名为:username 值为 闲言

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req,resp);

}

}

读取数据的Servlet

/**

  • @author by 闲言

  • @classname GetServlet

  • @description 获取数据

  • @date 2021/9/16 21:32

*/

public class GetData extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

ServletContext context = this.getServletContext();

String username = (String) context.getAttribute(“username”);

resp.setCharacterEncoding(“utf-8”);

resp.setContentType(“text/html”);

PrintWriter writer = resp.getWriter();

writer.write(username);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req,resp);

}

}

6.2获取初始化参数

url

jdbc:mysql://localhost:3306/school

public class ServletDemo03 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//获取上下文对象

ServletContext context = this.getServletContext();

//获取初始参数

String url = context.getInitParameter(“url”);

System.out.println("url: "+url);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req,resp);

}

}

6.3 请求转发

路径不会发生变化

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

ServletContext context = this.getServletContext();

//请求转发 /get 转发的路径

RequestDispatcher dispatcher = context.getRequestDispatcher(“/get”);

//调用forward 实现请求转发

dispatcher.forward(req,resp);

}

在这里插入图片描述

6.4 读取配置文件

Properties

在resources 目录下新建properties

在java 目录下新建properties

发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath;

思路:需要一个文件流

properties文件

username=xy123

password=123

代码

public class ServletDemo05 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

InputStream stream = this.getServletContext().getResourceAsStream(“/WEB-INF/classes/cn/bloghut/servlet/aa.properties”);

// InputStream stream = this.getServletContext().getResourceAsStream(“/WEB-INF/classes/db.properties”);

Properties pt = new Properties();

pt.load(stream);

String username = pt.getProperty(“username”);

String password = pt.getProperty(“password”);

resp.getWriter().println(username+“:”+password);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req,resp);

}

}

效果

在这里插入图片描述

maven资源导出失败问题

src/main/resources

**/*.xml

**/*.properties

src/main/java

**/*.properties

**/*.xml

6.5HttpServletResponse

web服务器接收到客户端的http请求,会针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletRespoonse;

  • 如果要获取客户端请求过来的参数:找HttpServletRequest

  • 如果要给客户端响应一些信息:找HttpServletResponse

简单分类

负责向浏览器发送数据的方法

public ServletOutputStream getOutputStream() throws IOException;

public PrintWriter getWriter() throws IOException;

负责向浏览器写一些响应头的方法

public void setCharacterEncoding(String charset);

public void setContentLength(int len);

public void setContentLengthLong(long len);

public void setContentType(String type);

public void setDateHeader(String name, long date);

public void addDateHeader(String name, long date);

public void setHeader(String name, String value);

public void addHeader(String name, String value);

public void setIntHeader(String name, int value);

public void addIntHeader(String name, int value);

响应的状态码常量

public static final int SC_CONTINUE = 100;

public static final int SC_SWITCHING_PROTOCOLS = 101;

public static final int SC_OK = 200;

public static final int SC_CREATED = 201;

public static final int SC_ACCEPTED = 202;

public static final int SC_NON_AUTHORITATIVE_INFORMATION = 203;

public static final int SC_NO_CONTENT = 204;

public static final int SC_RESET_CONTENT = 205;

public static final int SC_PARTIAL_CONTENT = 206;

public static final int SC_MULTIPLE_CHOICES = 300;

public static final int SC_MOVED_PERMANENTLY = 301;

public static final int SC_MOVED_TEMPORARILY = 302;

public static final int SC_FOUND = 302;

public static final int SC_SEE_OTHER = 303;

public static final int SC_NOT_MODIFIED = 304;

public static final int SC_USE_PROXY = 305;

public static final int SC_TEMPORARY_REDIRECT = 307;

public static final int SC_BAD_REQUEST = 400;

public static final int SC_UNAUTHORIZED = 401;

public static final int SC_PAYMENT_REQUIRED = 402;

public static final int SC_FORBIDDEN = 403;

public static final int SC_NOT_FOUND = 404;

public static final int SC_METHOD_NOT_ALLOWED = 405;

public static final int SC_NOT_ACCEPTABLE = 406;

public static final int SC_PROXY_AUTHENTICATION_REQUIRED = 407;

public static final int SC_REQUEST_TIMEOUT = 408;

public static final int SC_CONFLICT = 409;

public static final int SC_GONE = 410;

public static final int SC_LENGTH_REQUIRED = 411;

public static final int SC_PRECONDITION_FAILED = 412;

public static final int SC_REQUEST_ENTITY_TOO_LARGE = 413;

public static final int SC_REQUEST_URI_TOO_LONG = 414;

public static final int SC_UNSUPPORTED_MEDIA_TYPE = 415;

public static final int SC_REQUESTED_RANGE_NOT_SATISFIABLE = 416;

public static final int SC_EXPECTATION_FAILED = 417;

public static final int SC_INTERNAL_SERVER_ERROR = 500;

public static final int SC_NOT_IMPLEMENTED = 501;

public static final int SC_BAD_GATEWAY = 502;

public static final int SC_SERVICE_UNAVAILABLE = 503;

public static final int SC_GATEWAY_TIMEOUT = 504;

public static final int SC_HTTP_VERSION_NOT_SUPPORTED = 505;

下载文件

1.向浏览器输出消息

2.下载文件

2.1.要获取文件的路径

2.2下载的文件名是啥

2.3设置想办法让浏览器能够支持下载我们需要的东西

2.4获取下载文件的输入流

2.创建缓冲区

2.5获取OutputStream对象

2.6将FileOutputStream流写入到buff缓冲区

2.7使用OutputStream将缓冲区中的数据输出到客户端!

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//要获取文件的路径

// String realPath = this.getServletContext().getRealPath(“/1.png”);

String realPath = “G:\JavaWeb\狂\servlet\response-2\target\classes\1.png”;

System.out.println(“下载路径:” + realPath);

//下载的文件名是啥

String fileName = realPath.substring(realPath.lastIndexOf(“//”) + 1);

//设置想办法让浏览器能够支持下载我们需要的东西

//中文文件名URLEncoder.encode 编码,否则可能乱码

resp.setHeader(“Content-Disposition”, “attachment;fileName=” + URLEncoder.encode(fileName,“UTF-8”));

//获取下载文件的输入流

FileInputStream in = new FileInputStream(realPath);

//创建缓冲区

int len = 0;

byte[] buffer = new byte[1024];

//获取OutputStream对象

ServletOutputStream out = resp.getOutputStream();

//将FileOutputStream流写入到buff缓冲区

//使用OutputStream将缓冲区中的数据输出到客户端!

while ((len = in.read(buffer)) > 0) {

out.write(buffer,0,len);

}

out.close();

in.close();

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req, resp);

}

验证码功能

验证码怎么来的?

1.前端实现

2.后端实现,需要用到Java图片类,生成一个图片

分享

首先分享一份学习大纲,内容较多,涵盖了互联网行业所有的流行以及核心技术,以截图形式分享:

(亿级流量性能调优实战+一线大厂分布式实战+架构师筑基必备技能+设计思想开源框架解读+性能直线提升架构技术+高效存储让项目性能起飞+分布式扩展到微服务架构…实在是太多了)

其次分享一些技术知识,以截图形式分享一部分:

Tomcat架构解析:

算法训练+高分宝典:

Spring Cloud+Docker微服务实战:

最后分享一波面试资料:

切莫死记硬背,小心面试官直接让你出门右拐

1000道互联网Java面试题:

Java高级架构面试知识整理:

uffer,0,len);

}

out.close();

in.close();

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

doGet(req, resp);

}

验证码功能

验证码怎么来的?

1.前端实现

2.后端实现,需要用到Java图片类,生成一个图片

分享

首先分享一份学习大纲,内容较多,涵盖了互联网行业所有的流行以及核心技术,以截图形式分享:

(亿级流量性能调优实战+一线大厂分布式实战+架构师筑基必备技能+设计思想开源框架解读+性能直线提升架构技术+高效存储让项目性能起飞+分布式扩展到微服务架构…实在是太多了)

其次分享一些技术知识,以截图形式分享一部分:

Tomcat架构解析:

[外链图片转存中…(img-d8QVctNJ-1714532240400)]

算法训练+高分宝典:

[外链图片转存中…(img-AuxK8KeI-1714532240401)]

Spring Cloud+Docker微服务实战:

[外链图片转存中…(img-7vTTACAY-1714532240401)]

最后分享一波面试资料:

切莫死记硬背,小心面试官直接让你出门右拐

1000道互联网Java面试题:

[外链图片转存中…(img-WM67PiJR-1714532240402)]

Java高级架构面试知识整理:

[外链图片转存中…(img-T8IOXFZw-1714532240402)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 23
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值