添加模板
package ${enclosing_package};
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ${primary_type_name} extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
${cursor}
}
设置全局配置信息
public class Demo01 extends HttpServlet {
// public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// //先获取config对象
// ServletConfig servletConfig = this.getServletConfig();
// //取出servlet的配置信息
// String value = servletConfig.getInitParameter("username");
// System.out.println(value);
// }
//
// public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// doGet(request, response);
// }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取context域对象
ServletContext application = this.getServletContext();
//获取全局配置信息
String value = application.getInitParameter("key");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
获取服务器真实文件上的路径 并读取
使用Context域对象获取,可以获取到服务器上任意资源路径
public class Demo02 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取a文件
ServletContext context1 = this.getServletContext();
// 获取服务器上的真实路径(绝对路径 磁盘路径)
String path1 = context1.getRealPath("/WEB-INF/classes/a.properties");
System.out.println(path1);
// 读取文件
Properties properties1 = new Properties();
properties1.load(new FileInputStream(path1));
System.out.println(properties1.getProperty("key"));
//读取b文件 /WEB-INF/classes/com/lanou/demo/b.properties
ServletContext context2 = this.getServletContext();
String path2 = context2.getRealPath("/WEB-INF/classes/com/lanou/demo/b.properties");
System.out.println(path2);
Properties properties2 = new Properties();
properties2.load(new FileInputStream(path2));
System.out.println(properties2.getProperty("key"));
//读取c文件 /WEB-INF/c.properties
ServletContext context3 = this.getServletContext();
String path3 = context3.getRealPath("/WEB-INF/classes/com/lanou/demo/b.properties");
System.out.println(path3);
Properties properties3 = new Properties();
properties3.load(new FileInputStream(path3));
System.out.println(properties3.getProperty("key"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
请求转发
浏览器只是发起了一次请求
servlet内部做了请求转发 浏览器并不知道
public class Demo03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Guess who I am?");
System.out.println("I don't know,你让demo04猜猜");
//获取context域对象
ServletContext context = this.getServletContext();
//从context域中 获取请求转发器
RequestDispatcher dispatcher = context.getRequestDispatcher("/demo04");
//进行请求转发
dispatcher.forward(request, response);
System.out.println("I do,哈哈");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class Demo04 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("I tell you,faggot");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
HttpServletResponse 服务器的响应对象
响应对象中都有什么?
1.响应行 http/1.1 状态码200
2.响应头 告诉浏览器我要做什么 例如响应给你的文件需要下载 以什么编码格式解析数据
3.响应体 响应回浏览器的数据
public class Demo05 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.设置编码格式服务器的编码格式,默认tomcat iso-8859-1
//response.setCharacterEncoding("UTF-8");
//告诉浏览器要使用什么编码格式来查看
//2.添加响应头
//response.setHeader("Content-type", "text/html;charset=UTF-8");
//这句代表以上两句的方法
response.setContentType("text/html;charset=UTF-8");
//响应给浏览器一句话
//从响应对象HttpServletResponse中获取流对象
//注意:这个流对象不是你自己创建,要从响应中获取
PrintWriter out = response.getWriter();
out.write("啦啦啦啦");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
下载文件
用户发送请求,请求到访问servlet
Servlet处理请求(把服务器上的图片,以流的形式,使用response响应给用户浏览器)
public class Demo06 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取服务器上的图片路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/1.jpeg");
//字符串切割,获取图片的名字
int index = path.lastIndexOf("/");
String filename = path.substring(index+1);
System.out.println(filename);
//修改文件名字的字符集
filename = new String(filename.getBytes(), "iso-8859-1");
//添加响应头(需要拼接文件的名字)
response.setHeader("content-disposition", "attachment;filename="+filename);
//告诉浏览器文件下载的格式添加响应头
response.setHeader("content-type", "image/jpeg");
// 从服务器中读取图片
FileInputStream fis = new FileInputStream(path);
//注意:需要获取response当中的字节流进行数据的响应
ServletOutputStream sos = response.getOutputStream();
// 边读边写
int len = 0;
byte[] b = new byte[1024];
while((len = fis.read(b)) != -1) {
//响应回浏览器
//如果只是单纯的吧图片响应回去,浏览器并不知道你要干啥(下载或浏览)
//需要通过响应头 通知浏览器 我给你这个文件,是下载用的
sos.write(b,0,len);
}
//注意:自己创建的流自己关
fis.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
请求重定向
请求重定向和请求转发的区别
请求重定向是发起的两次请求(请求地址发生了变化)
响应时要注意细节
从response获取的字符流和字节流,在一个servlet中不能同时使用
public class Demo07 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//通过请求头的方式,请求重定向
//System.out.println("我要借钱");
//System.out.println("我没有,去找demo08");
//添加重定向响应头
//注意:添加头信息网址的时候,需要写明工程名
//response.setHeader("location","/java-web-servlet-3.27/demo08");
//添加重定向状态码
//response.setStatus(302);
//System.out.println("我去了");
//添加刷新头(每秒刷新一次)
//response.setIntHeader("refresh",1);
//添加随机数
//response.getWriter().write(Math.random() + "");
//设置响应的字符集
response.setContentType("text/html;charset=UTF-8");
//三秒后跳转一个网址
response.setHeader("refresh", "3;url=/java-web-servlet-3.27/demo08");
response.getWriter().write("三秒后跳转");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
HttpServletRequest 用户的请求对象
包含了请求行,请求头,请求体
public class Demo08 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request);
//System.out.println("我借了");
///java-web-servlet-3.27/demo08
System.out.println(request.getRequestURI());
//http://localhost:8080/java-web-servlet-3.27/demo08
System.out.println(request.getRequestURL());
//获取请求的类型(用浏览器直接请求的都是Get请求)
System.out.println(request.getMethod());
//获取请求路径(相对路径)
System.out.println(request.getContextPath());
//获取请求中携带的参数
//参数是你提交表单的时候,表单的name属性
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username + " " + password);
//判断浏览器
//可以通过请求中的信息获取用户使用浏览器
String header = request.getHeader("User-Agent");
System.out.println(header);
if(header.toLowerCase().contains("firefox")) {
System.out.println("用的是火狐");
}else if(header.toLowerCase().contains("chrome")){
System.out.println("用的是谷歌");
}else {
System.out.println("其他浏览器");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}