Java Web JSP内置对象作用域
page:对应的内置对象是pageContext、只在当前页面有效 request:对应的内置对象是request、在一次请求内有效 session:对应的内置对象是Session、在一次会话内有效 appliaction:对应的内置对象是Appliaction、对应整个Web应用
page < request < session < appliaction
EL表达式
替代JSP页面中数据访问时的复杂编码、方便取出域中对象保存的数据
pageContext ——> request ——> session ——>appliaction
pageContext:${pageScope.name}
request:${requestScope.name}
session:${sessionScope.name}
appliaction:${appliactionScope.name}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 public class User { private Integer id; private String name; private Double score; private Student student; public User (Integer id, String name, Double score, Student student) { this .id = id; this .name = name; this .score = score; this .student = student; } public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public Double getScore () { return score; } public void setScore (Double score) { this .score = score; } public Student getStudent () { return student; } public void setStudent (Student student) { this .student = student; } @Override public String toString () { return "User{" + "id=" + id + ", name='" + name + '\'' + ", score=" + score + ", student=" + student + '}' ; } } public class Student {}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <%@ page import ="com.dream.xiaobo.EL.User" %> <%@ page import ="com.dream.xiaobo.EL.Student" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>EL</title> </head> <body> <% Student student = new Student(); User user = new User(1 ,"小博" ,99.9 , student); pageContext.setAttribute("user" ,user); %> <table> <tr> <th>编号</th> <th>姓名</th> <th>分数</th> <th>地址</th> </tr> <tr> <td>${pageContext.user.id}</td> <td>${pageContext.user.name}</td> <td>${pageContext.user.score}</td> <td>${pageContext.user.student}</td> </tr> </table> </body> </html>
&& –> and || –> or ! –> not == ->> eq < –> lt –> gt <= –> le = –> ge empty(判断是否为空) –> 空是变量为null、长度为0的String、size为0的集合
JSTL
使用标签完成一些逻辑处理、不在出现JSP脚本穿插情况
1、导入两个jar包(jstl.jar standard.jar)、放在web/WEB-INF
2、在JSP页面引入
1 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3、直接使用
提供统一标签 用于编写各种动态功能
set:向域中对象添加数据
1 2 <c:set var ="name" value="set值" scope="request" ></c:set> ${requestScope.name}
out:输出域对象中的数据
1 <c:out value="${name}" default ="未定义" ></c:out>
remove:删除域对象中的数据
1 <c:remove var ="name" scope="page" ></c:remove>
catch:异常捕获
1 2 3 4 5 6 <c:catch var ="exception" > <% int a = 10 /0 ; %> </c:catch > ${exception}
forEach:迭代标签
1 2 3 4 5 6 7 <c:forEach items="${map}" var ="user" > <tr> <td>${user.id}</td> <td>${user.name}</td> <td>${user.score}</td> </tr> </c:forEach>
Filter
拦截传入的请求和传出的响应 修改或以某种方式处理正在客户端和服务器之间交换的数据流
1 2 3 4 5 6 7 public class MyFilter implements Filter {@Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { } }
web.xml
1 2 3 4 5 6 7 8 <filter > <filter-name > myFilter</filter-name > <filter-class > com.dream.xiaobo.filter.MyFilter</filter-class > </filter > <filter-mapping > <filter-name > myFilter</filter-name > <url-pattern > /*</url-pattern > </filter-mapping >
注解
注解可以简化配置、但是web.xml配置可以控制Filter的执行先后顺序、而注解无法控制Filter执行先后顺序
统一处理资源乱码
1 2 3 request.setCharacterEncoding("UTF-8" ); response.setCharacterEncoding("UTF-8" ); response.setContentType("text/html;charset=UTF-8" );
屏蔽敏感词汇
1 2 3 4 String name = request.getParameter("name" ); name.replaceAll("敏感词" ,"***" ); request.setAttribute("name" ,name); filterChain.doFilter(request, response);
控制资源访问权限
文件上传
1、input设置为file
2、form表单method设置为post、get请求发送的是文件名给服务端、而不是文件本身、所以必须用post
3、form表单enctype设置为multipart/form-data,以二进制的形式传输数据
1 2 3 4 5 6 7 8 9 10 11 12 13 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <form action="uploadServlet" enctype="multipart/form-data" method="post" > <input type="text" name="desc" /></br> <input type="file" name="img" /></br> <input type="submit" value="上传" > </form> </body> </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 @WebServlet("/uploadServlet") public class UploadServlet extends HttpServlet { @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8" ); ServletInputStream inputStream = req.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String path = req.getServletContext().getRealPath("file/xiaobo.txt" ); OutputStream outputStream = new FileOutputStream(path); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); String temp = "" ; while ((temp = bufferedReader.readLine()) != null ){ System.out.println(temp); bufferedWriter.write(temp); } bufferedWriter.close(); outputStreamWriter.close(); outputStream.close(); bufferedReader.close(); inputStreamReader.close(); inputStream.close(); } }
web/WEB-INF下导入jar commons-fileupload-1.3.1 commons-io-2.5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <form action="upload" enctype="multipart/form-data" method="post" > <input type="text" name="desc" /></br> <input type="file" name="img" /></br> <input type="submit" value="上传" > </form> </body> </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 @WebServlet("/upload") public class MyUpload extends HttpServlet { @Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8" ); try { DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory(); ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory); List<FileItem> fileItems = fileUpload.parseRequest(req); for (FileItem fileItem: fileItems){ if (fileItem.isFormField()){ String name = fileItem.getName(); String fieldName = fileItem.getFieldName(); System.out.println(name + ":" + fieldName); }else { String name = fileItem.getName(); InputStream inputStream = fileItem.getInputStream(); String path = req.getServletContext().getRealPath("file/" + name); System.out.println(path); OutputStream outputStream = new FileOutputStream(path); int temp = 0 ; while ((temp = inputStream.read()) != -1 ) { System.out.println(temp); outputStream.write(temp); } outputStream.close(); inputStream.close(); System.out.println("上传成功" ); } } } catch (FileUploadException e) { e.printStackTrace(); } } }
文件下载 1 2 3 4 5 6 7 8 9 10 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>下载</title> </head> <body> <a href="/download?type=png" >1. png</a> <a href="/download?type=txt" >xiaobo.txt</a> </body> </html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 @WebServlet("/download") public class MyDownload extends HttpServlet { @Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8" ); String type = req.getParameter("type" ); String fileName = "" ; switch (type){ case "png" : fileName = "微信截图_20210618143454.png" ; break ; case "txt" : fileName = "xiaobo.txt" ; break ; } resp.setContentType("application/x-msdownload" ); resp.setHeader("Content-Disposition" ,"attachment;filename=" +fileName); OutputStream outputStream = resp.getOutputStream(); String path = req.getServletContext().getRealPath("file/" +fileName); InputStream inputStream = new FileInputStream(path); int temp = 0 ; while ((temp=inputStream.read())!=-1 ){ outputStream.write(temp); } inputStream.close(); outputStream.close(); } }
正确的开始、微小的长进、然后持续、嘿、我是小博、带你一起看我目之所及的世界……