Structure介绍操作
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("我妹是小可爱");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
web.xml
<?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 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.yl520.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
node.md(提取web.xml中公共代码方便复制)
web.xml
```xml
<?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 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
</web-app>
```
context
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
context共享数据
共享数据(在AServlet保存的数据可以在BServlet中拿到)
需要创建一个放置数据的类HelloServlet
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// this.getInitParameter();初始化参数
// this.getServletConfig();Servlet配置
// this.getServletContext();上下文
ServletContext context = this.getServletContext();
String username="我妹最可爱";
context.setAttribute("username",username);//将一个数据保存在servletcontext,名字为:username,值为:username
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
需要创建一个读取数据的类GetServlet
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");
resp.getWriter().print("谁最可爱"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
配置web.xml
<?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 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- <context-param> this.getInitParameter();初始化参数配置的-->
<!-- <param-name></param-name>-->
<!-- <param-value></param-value>-->
<!-- </context-param>-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.yl520.servlet.HelloServlet</servlet-class>
<!-- <init-param> this.getInitParameter();初始化参数配置的-->
<!-- <param-name></param-name>-->
<!-- <param-value></param-value>-->
<!-- </init-param>-->
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>getcontext</servlet-name>
<servlet-class>com.yl520.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getcontext</servlet-name>
<url-pattern>/getcontext</url-pattern>
</servlet-mapping>
</web-app>
测试结果:
先访问/hello,让HelloServlet放入数据,再访问/getcontext获取数据
ServletContext应用
获取初始化参数
创建类ServletDemo继承HttpServlet
public class ServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
配置web.xml
<!-- 配置web应用的初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
<servlet>
<servlet-name>pa</servlet-name>
<servlet-class>com.yl520.servlet.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>pa</servlet-name>
<url-pattern>/param</url-pattern>
</servlet-mapping>
启动tomcat,弹出后访问/param,得到:
jdbc:mysql://localhost:3306/mybatis
请求转发
创建类继承HttpServlet
public class ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("我妹最可爱");
ServletContext context = this.getServletContext();
RequestDispatcher requestDispatcher = context.getRequestDispatcher("/param");//转发的请求路径
requestDispatcher.forward(req, resp);//调用forward实现请求转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
设置web.xml
<!-- 请求转发-->
<servlet>
<servlet-name>s4</servlet-name>
<servlet-class>com.yl520.servlet.ServletDemo04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s4</servlet-name>
<url-pattern>/s4</url-pattern>
</servlet-mapping>
读取资源文件(项目中大量用到)
- 在java目录下新建properties
- 在resource目录下新建properties
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath
resource下创建db.properties
username=root
password=root
创建类
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream resource = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(resource);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
resp.getWriter().print(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
/WEB-INF/classes/db.properties(第一个/表示本项目的相对路径)
HttpServletResponse下载文件
web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的HttpServletResponse对象;
- 获取客户端请求过来的参数,找HttpServletRequest
- 给客户端响应一些信息,找HttpServletResponse
下载文件:
创建类
public class FileServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取下载文件路径
String realPath = "D:\\CGBIV4\\CODES\\HelloServlet\\response\\target\\classes\\我妹是小可爱.jpg";
//下载文件名
String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
//设置想办法让浏览器能够支持(Content-Disponsition)下载我们需要的东西
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流写入buffer缓冲区,使用outputstream将缓冲区的数据输出到客户端
while ((len=in.read(buffer))!=-1){
out.write(buffer,0,len);
}
in.close();
out.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
配置web.xml文件
<?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 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>sl</servlet-name>
<servlet-class>com.yl520.servlet.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sl</servlet-name>
<url-pattern>/down</url-pattern>
</servlet-mapping>
</web-app>
插入要下载的文件
访问/down