JavaWeb 后端 <二> 之 Servlet 学习笔记
2023-09-14 08:58:20 时间
//转发:源 public class ServletContextDemo4 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher("/servlet/ServletContextDemo5");//转发的地址。ServletContext得到的,地址必须以"/"开头,该"/"就代表着当前应用的访问路径/day07_01_servlet rd.forward(request, response);//转发 }
//转发:目标 public class ServletContextDemo5 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().write("I am five"); }
实现中文文件的下载
//实现中文文件的下载 public class ServletContextDemo6 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //文件在哪儿?以不变应万变 ServletContext sc = getServletContext(); String realPath = sc.getRealPath("/WEB-INF/classes/霉女.jpg");// 文件存放的真实绝对路径 // System.out.println(realPath); //构建文件的输入流 InputStream in = new FileInputStream(realPath); //告知客户端以下载的方式打开:Content-Disposition=attachment;filename=27.jpg //获取要下载的文件名 String filename = realPath.substring(realPath.lastIndexOf(File.separator)+1); response.setHeader("Content-Type", "application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(filename,"UTF-8"));//中文属于不安全的字符,需要进行URL编码 //用response的字节流进行输出 OutputStream out = response.getOutputStream(); int len = -1; byte b[] = new byte[1024]; while((len=in.read(b))!=-1){ out.write(b, 0, len); } in.close(); out.close(); }
encode 编码
实例: 使用utf-8编码
import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import org.junit.Test; public class UrlEncodeDemo { @Test public void test1() throws UnsupportedEncodingException{ String s = "胡轩"; System.out.println(URLEncoder.encode(s, "UTF-8")); } @Test public void test2() throws UnsupportedEncodingException{ String s = "%E8%83%A1%E8%BD%A9"; String v = URLDecoder.decode(s, "UTF-8"); System.out.println(v); } }
读取配置文件的各种方式
//演示:读取配置文件的各种方式 public class ServletContextDemo7 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { test31(request, response); } //请不要把Tomcat等服务器装在有空格的目录中 //类加载器读取:只能读取classes或者类路径中的任意资源。但是不适合读取特别大的资源。b c private void test31(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ClassLoader cl = ServletContextDemo7.class.getClassLoader();//得到类加载器 URL url = cl.getResource("com/itheima/resources/c.properties"); String path = url.getPath(); InputStream in = new FileInputStream(path); Properties props = new Properties(); props.load(in); System.out.println(props.getProperty("hello")); } //类加载器读取:只能读取classes或者类路径中的任意资源。但是不适合读取特别大的资源。b c private void test30(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ClassLoader cl = ServletContextDemo7.class.getClassLoader();//得到类加载器 // InputStream in = cl.getResourceAsStream("b.properties"); InputStream in = cl.getResourceAsStream("com/itheima/resources/c.properties"); Properties props = new Properties(); props.load(in); System.out.println(props.getProperty("hello")); } //利用ResourceBundle读取:b c ,不能读a,只能读取properties的文件 private void test20(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // ResourceBundle rb = ResourceBundle.getBundle("b"); ResourceBundle rb = ResourceBundle.getBundle("com.itheima.resources.c"); System.out.println(rb.getString("hello")); } //利用ServletContext读取:a b c //可以读取应用中任何位置上的资源。使用限制:只能在web应用中用 private void test10(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // String path = getServletContext().getRealPath("/a.properties"); // String path = getServletContext().getRealPath("/WEB-INF/classes/b.properties"); String path = getServletContext().getRealPath("/WEB-INF/classes/com/itheima/resources/c.properties"); InputStream in = new FileInputStream(path); Properties props = new Properties(); props.load(in); System.out.println(props.getProperty("hello")); }
相关文章
- 阅读不知道如何做笔记?试试这款常用的手机笔记工具
- 《Head First Servlet JSP》学习笔记
- 《Head First Servlet JSP》学习笔记一
- mongodb学习笔记-tina
- 《从零开始学Swift》学习笔记(Day 35)——会使用下标吗?
- 【王凤鸣 太极缠丝功笔记】第二章
- 【Andorid X 项目笔记】嵌套Fragment的使用(5)
- 机器学习笔记 - win10安装Pytorch-GPU版本并训练第一个神经网络
- 机器学习笔记 - Kaggle竞赛 帮助保护大堡礁,棘冠海星目标检测参赛经历
- 机器学习笔记 - 确定性与随机机器学习
- 数学建模学习笔记(九)数据预处理
- 一些SAP UI5的学习笔记
- 重构老系统遗留代码的一些方法学习笔记
- Salesforce for marketers学习笔记
- Angular CLI builder 学习笔记
- JavaSE学习笔记--Item1 注解Annotation
- 802.11 学习笔记
- 《30天自制操作系统》笔记(04)——显示器256色
- Oracle Apex 有用笔记系列 2 - 文件上传管理
- python学习笔记(六)文件夹遍历,异常处理
- C++学习笔记9-运算符重载
- 笔记:投资学习 (2020-07-10)[68.99%]
- Java学习笔记(一)——HelloWorld
- Node.js学习笔记——nvm
- C/C++学习笔记九