一个Servlet可以指定一个映射路径
<servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping>
一个Servlet可以指定多个映射路径
一个Servlet可以指定通用映射路径
Servlet 可以自定义映射路径
//注册Servlet <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.liu.servlet.HelloServlet</servlet-class> </servlet>优先级问题
指定固有的映射路径优先级最高,如果找不到就找默认请求
ServletContext
web容器在启动的时候,他会为每个web程序创建一个对应的ServletContext对象,它代表了当前的web应用
共享数据
package com.liu.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; import java.io.PrintWriter; public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String userName =(String) context.getAttribute("userName"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter writer = resp.getWriter(); writer.print(userName); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
package com.liu.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; import java.io.PrintWriter; public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String userName =(String) context.getAttribute("userName"); resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter writer = resp.getWriter(); writer.print(userName); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee " version="4.0" metadata-complete="true"> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.liu.servlet.HelloServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet> <servlet-name>getc</servlet-name> <servlet-class>com.liu.servlet.GetServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>getc</servlet-name> <url-pattern>/getc</url-pattern> </servlet-mapping> </web-app>
Responseweb服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应一个HttpServletResponse;
如果客户端请求过来的参数:找HttpServletRequest
如果要给客户端响应一些信息:找HttpServletResponse
简单分类负责想浏览器发送数据的方法
ServletOutputStream getOutputStream() throws IOException; PrintWriter getWriter() throws IOException;
负责向浏览器设置响应的编码
void setCharacterEncoding(String var1); //编码 void setContentLength(int var1);//长度 void setContentLengthLong(long var1); void setContentType(String var1);//响应类型 常见应用
向浏览器输出消息
下载文件
获取文件的路径
下载文件名是啥
设置让浏览器支持下载需要的东西
获取下载文件的输入流
创建缓存区,获取OutputStream对象
将FileOutputStream流写入到buffer缓冲区
使用OutputStream将缓存区输出到客户端