package com.demo.servlet;
import javax.servlet.*;
import java.io.IOException;
public class LifeServlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("Servlet init");
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse)
throws ServletException, IOException {
System.out.println("Servlet service");
}
@Override
public void destroy() {
System.out.println("Servlet destroy");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
}
某种原因,导致第一次请求时初始化很慢,这时就可以修改Servlet的初始化时间
使用其值为大于等于0的整数时,该Servlet就会在服务器启动时初始化,初始化在整个生命中只有一次
同一个Servlet,要么使用web.xml文件配置,要么使用注解配置,但是不能使用两种方式同时配置
注解中的参数有name、serviceName;若是只写name参数,可以省略key值,只写value;除此之外要写key-value的形式
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println(req.getMethod());
System.out.println(req.getRequestURL());
System.out.println(req.getRequestURI());
System.out.println(req.getServerName());
System.out.println(req.getServerPort());
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String value1 = req.getHeader("Host");
System.out.println(value1);
Enumeration names = req.getHeaderNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
String value = req.getHeader(name);
System.out.println(name+":"+value);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String gender = req.getParameter("gender");
String hobbies[] = req.getParameterValues("hobbies");
System.out.println(username+","+password+","+gender+","+ Arrays.toString(hobbies));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求体编码集
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
String gender = req.getParameter("gender");
String hobbies[] = req.getParameterValues("hobbies");
System.out.println(username+","+password+","+gender+","+ Arrays.toString(hobbies));
//文件相关处理
//获取用户提交的文件对象
Part part = req.getPart("photo");
//指定上传路径
String path = "d://upload";
//判断该路径是否存在
File file = new File(path);
if (!file.exists()) {
//不存在则创建
file.mkdirs();
}
//获取上传的文件的名字
String fileName = part.getSubmittedFileName();
//在指定位置创建指定文件
part.write(path+"/"+fileName);
}
状态码 | 说明 |
---|---|
1** | 请求已到达服务器,但未完成,需要再次请求 |
2** | 请求已到达服务器,并且已完成 |
3** | 服务器内部资源重定向 |
4** | 资源找不到 |
5** | 服务器内部异常,代码异常 |
200 | 成功 |
404 | 找不到,原因:路径有问题 |
500 | 代码有问题,90%以上都是空指针 |
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setStatus(404);
}
响应头key | value |
---|---|
Location | 这个头通常配合302状态码使用,它用于告诉浏览器你去找谁。 |
Server | 告诉浏览器,服务器的类型 |
Content-Encoding | 服务器通过这个头,告诉浏览器,回送的数据采用的压缩格式。 |
Content-Length | 0 |
Content-Language | zh-cn |
Content-Type | 这个头用于告诉浏览器,回送数据的类型 |
Last-Modified | 这个头用于告诉浏览器,数据的最后修改时间 |
Transfer-Encoding | 用于通知浏览器,数据是以分块形式回送的 |
Cache-Control | no-cache 通知浏览器不要缓存 |
Refresh | 1;url=http://www.it315.org 隔多少秒以后,让当前页面去访问哪个地址(例如网页登陆成功后,跳回原来的界面,就是用的这个头) |
Content-Disposition | attachment;filename=aaa.zip 和下载相关,通知浏览器以附件的形式下载服务器发送过去的数据 |
Set-Cookie | SS=Q0=5Lb_nQ; path=/search 和cookie相关的头 |
ETag | W/“83794-1208174400000” 和cookie相关的头 |
Expires | -1 通知浏览器是否缓存当前资源:如果这个头的值是一个以毫秒为单位值,则通知浏览器缓存资源到指定的时间点;如果值是0或-1,则通知浏览器禁止缓存 |
Pragma | no-cache 通知浏览器是否缓存资源 |
Connection | close/Keep-Alive 是否继续保持连接 |
Date | Tue, 11 Jul 2000 18:23:51 GMT 当前响应的时间 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setStatus(302);
resp.setHeader("location","http://www.baidu.com");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setHeader("refresh","2;url=http://www.qidian.com");
}
ServletOutputStream out = response.getOutputStream();//字节流
PrintWriter out = response.getWriter();//字符流
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
out.print("今天天气不错
");
out.close();
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<a href="responseServlet">下载a>
body>
html>
去除原来的 exploded
重新添加exploded
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//告诉浏览器,以附件的形式打开数据
resp.setHeader("Content-Disposition","attachment;filename=erha.png");
//输入流读取文件
//this.getClass().getClassLoader().getResourceAsStream("gouza.png")用于快速的获取src当中问文件对象流
InputStream in = this.getClass().getClassLoader().getResourceAsStream("gouza.png");
//输出流写文件
ServletOutputStream out = resp.getOutputStream();
//边读边写
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b,0,len);
}
out.close();
in.close();
}
跳转方式 | 方法 |
---|---|
请求转发 | request.getRequestDispatcher(“跳转的文件路径”).forward(request,response); |
重定向 | response.sendRedirect(“跳转的文件路径”); |
package com.demo.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("进入FirstServlet");
req.setAttribute("color","red");
System.out.println(req.getAttribute("color"));
req.getRequestDispatcher("secondServlet").forward(req,resp);
}
}
package com.demo.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/secondServlet")
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("进入SecondServlet");
System.out.println(req.getAttribute("color"));
}
}
Title
测试请求转发
package com.demo.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/firstServlet")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("进入FirstServlet");
req.setAttribute("color","red");
System.out.println(req.getAttribute("color"));
resp.sendRedirect("secondServlet");
}
}
package com.demo.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/secondServlet")
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("进入SecondServlet");
System.out.println(req.getAttribute("color"));
}
}
请求转发 | 重定向 | |
---|---|---|
发出位置 | 服务器行为 | 客户端行为 |
地址栏内容 | 地址栏不会发生改变 | 地址栏会发生改变 |
请求次数 | 只有一次请求 | 至少两次请求 |
request共享 | equest是共享的 | request不共享的 |
转发效率 | 高 | 较低 |
发送范围 | 只能在当前模块中进行 | 可以跳转到其他模块的作用域容器对象:作用域里面的作为容器的对象 |
--------------------------------------------------------: |
| 发出位置 | 服务器行为 | 客户端行为 |
| 地址栏内容 | 地址栏不会发生改变 | 地址栏会发生改变 |
| 请求次数 | 只有一次请求 | 至少两次请求 |
| request共享 | equest是共享的 | request不共享的 |
| 转发效率 | 高 | 较低 |
| 发送范围 | 只能在当前模块中进行 | 可以跳转到其他模块的作用域容器对象:作用域里面的作为容器的对象 |