转发
调用HttpServletRequest
对象的方法
request.getRequestDispatcher("test.jsp").forward(req, resp);
重定向
调用HttpServletResponse
对象的方法
response.sendRedirect("loginsuccess.jsp");
转发:URL没有变化 重定向:URL会发生变化
转发:请求1次 重定向:请求2次
转发:服务器行为 重定向:客户端行为
转发: 直接传递请求数据 重定向: 必须经过session/application全局中间数据缓存
JSP
:只有当客户端第一次请求JSP时,才须要将其转换、编译以及实例化
Servlet
: 只有当服务器启动时(web.xml中配置load-on-startup=1,默认为0)或者第一次请求该servlet时,才会加载和实例化
ServletConfig、 ServletContext、 HttpServletRequest、 HttpServletResponse
ServletConfig:表明当前Servlet的配置信息(web.xml) 获取ServletConfig方法: ServletConfig sc = this.getServletConfig(); ServletContext:表明当前Application 获取ServletContext方法: ServletContext sc = this.getServletContext(); HttpServletRequest:表明请求信息 HttpServletResponse:表明响应信息
Tomcat 启动时,
为每一个web项目建立对应的ServletContext对象
ServletContext对象什么时候销毁?
第一种:把web应用移除;第二种:把Tomcat服务器中止
做用:是管理WEB资源,读取资源文件等 (请不要使用java文件方式去读取
)
2种方法: 一、使用ServletRequest对象 request.getRequestDispatcher(); 二、使用ServletContext对象 context.getRequestDispatcher(); 区别: ServletContext.getRequestDispatcher(String path)方法的参数必须以斜杠(/)开始, 被解释为相对于当前上下文根(context root)的路径。 例如:/myservlet是合法的路径,而../myservlet是不合法的路径。 ServletRequest.getRequestDispatcher(String path)方法的参数不但能够使相对于上下文根的路径, 并且能够是相对于当前Servlet的路径。如/myservlet和myservlet都是合法的路径。 若是路径以斜杠(/)开始,则被解释为相对于当前上下文根的路径; 若是没有以斜杠(/)开始,则被解释为相对于当前Servlet的路径。
一、context(表明整个应用均可以使用,servletContext) 二、request 三、session 四、page
方式1: InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/config/db.properties"); Properties props = new Properties(); props.load(in); 方式2: String path= this.getServletContext().getRealPath("/WEB-INF/config/db.properties"); FileInputStream fis = new FileInputStream(path); Properties props = new Properties(); props.load(fis);
方式1(类加载器 加载与读) InputStream in = UserServlet.class.getClassLoader().getResourceAsStream("db.properties"); Properties props = new Properties(); props.load(in); 方式2 (类加载器 只加载 用传统的方式读) String path = UserServlet.class.getClassLoader().getResource("db.properties").getPath(); FileInputStream fis = new FileInputStream(path); Properties props = new Properties(); props.load(fis);
response.setHeader("Content-Type", "text/html;charset=utf-8");
response.setHeader("Content-Type", "text/html"); String str = "中国"; OutputStream os = response.getOutputStream(); os.write("<meta charset=\"utf-8\" />".getBytes()); os.write(str.getBytes("utf-8"));
字符流
与字节流
的区别(字节流是字符流的基础)
字节流应用更普遍:二进制数据(视频、音频、图片、文本)
字符流应用更专一:文本(字符串)
获取字节流: OutputStream os = response.getOutputStream(); 获取字符流: PrintWriter pw = response.getWriter(); 设置服务器编码方式: 字节: str.getBtyes("UTF-8") //告诉服务器使用UTF-8编码字符 字符: response.setCharacterEncoding("utf-8"); //告诉服务器使用UTF-8编码字符 设置客户端编码方式: response.setContentType("text/html;charset=utf-8");
response.setHeader("content-disposition", "attachment;filename=" + filename); 若是下载的是中文文件,上面这种写法会出行乱码而且下载不了,因此必须使用下面这种: response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename,"UTF-8"));
1、为何要用URLEncoder
客户端在进行网页请求的时候,网址中可能会包含非ASCII码形式的内容,好比中文。java
而直接把中文放到网址中请求是不容许的,因此须要用URLEncoder编码地址,web
将网址中的非ASCII码内容转换成能够传输的字符缓存
不会被编码的内容服务器
1.大写字母A-Zsession
2.小写字母a-zapp
3.数字 0-9jsp
4.标点符 - _ . ! ~ * ' (和 ,)this
(只能获取文件真实路径)
String filePath = this.getServletContext().getRealPath("/WEB-INF/upload/xxx.jpg"); InputStream is = new FileInputStream(filePath);
OutputStream out = response.getOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while((len =is.read(buffer) ) > 0){ out.write(buffer, 0 ,len); }
若是须要先访问Servlet再跳转到Jsp的话,须要把JSP页面放在WEB-INF中 若是JSP能够直接访问,那直接放置在WEB-INF目录外层