Java WEB 入门到实战 (3)

package com.liu.servlet; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; import java.net.URLEncoder; public class FileServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // String path = "/Applications/MAMP/JavaDemo/javeweb-maven-01/response/src/main/resources/1.png"; String realPath ="/Applications/MAMP/JavaDemo/javeweb-maven-01/response/src/main/resources/1.png"; System.out.println("下载文件的路径" + realPath); String fileName = realPath.substring(realPath.lastIndexOf("//") + 1); resp.setHeader("Content-disposition", "attachment;fileName=" + URLEncoder.encode(fileName,"utf-8")); FileInputStream in = new FileInputStream(realPath); int len = 0; byte[] buffer = new byte[1024]; ServletOutputStream outputStream = resp.getOutputStream(); while ((len = in.read(buffer)) > 0) { outputStream.write(buffer, 0, len); } outputStream.close(); in.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

Response 验证码实现 package com.liu.servlet; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.Random; public class ImageServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("refresh", "3"); BufferedImage image = new BufferedImage(80, 20, BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) image.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, 80, 20); g.setColor(Color.BLUE); g.setFont(new Font(null, Font.BOLD, 20)); g.drawString(makeNum(), 0, 20); resp.setContentType("image/jpg"); resp.setDateHeader("expires", -1); resp.setHeader("Cache-Control", "no-cache"); resp.setHeader("Pragma", "no-cache"); ImageIO.write(image, "jpg", resp.getOutputStream()); } private String makeNum() { Random random = new Random(); String num = random.nextInt(9999999) + ""; StringBuffer sb = new StringBuffer(); for (int i = 0; i < 7 - num.length(); i++) { sb.append("0"); } num = sb.toString() + num; return num; } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } } Response 重定向

一个web资源收到客户端请求后,他会通知其他客户端请求另外一个资源,这个过程叫重定向

package com.liu.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RedirectServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.sendRedirect("/r2/img"); } } Request

HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,HTTP请求中的所有信息会封装到HttpServletRequest,通过HttpServletRequest的方法,获得了客户端所有的信息

获取传递的参数 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); String[] hobbys = req.getParameterValues("hobbys"); req.getRequestDispatcher("/success.jsp").forward(req,resp); //请求转发 } Cookie/Session 会话

会话:用户打开一个浏览器,点击了很多链接,访问了多个web资源,关闭浏览器,这个过程,可以称之为会话

保存会话的两种技术

cookie

客户端技术(响应,请求)

从请求中拿到了cookie信息

服务器响应给客户端cookie

Cookie[] cookies = req.getCookies();//返回数组 cookie.getName();//获得cookie中的key cookie.getValue();//获取的cookie的val new Cookie("lastLoginTime", System.currentTimeMillis() + ""); //new cookie cookie.setMaxAge(24 * 60 * 60); //cookie 过期时间 resp.addCookie(cookie);//响应cookie

一般会报错在本地的,用户目录下,appdata

浏览器cookie上线300个,每个站点最多200cookie,cookie 大小限制 4KB

session

服务器技术,利用这个技术,可保存用户的会话信息,可以把信息或数据放到Session中

服务器会给每个用户(浏览器)来创建一个session对象

一个session独占一个浏览器,只要浏览器没有关闭,这个session就存在

JSP原理剖析 什么是JSP

java server pages: java服务端页面,也和Servlet一样,用于动态web技术

最大特点:

写JSP就想写HTML 一样

HTML只给用户提供静态数据

JSP页面中可以嵌入JAVA代码,为用户提供动态数据

JSP 原理

tomcat 中有个work目录;

IDEA中使用Tomcat,会在Tomcat中生产一个work目录

浏览器向服务器发送请求,不管访问什么资源,其实就是在访问servlet

JSP最终也会被转成一个java类

JSP基础语法 和 指令

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzfpyy.html