JavaWeb(四)

JavaWeb

人类与计算机的图灵测试(验证码)
public class CodeServlert extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //fun(response);
        //使用jar生成验证码
        ValidateCode Vcode = new ValidateCode(100, 20, 4, 9);
        //获取生成的验证码字符串
        String code = Vcode.getCode();
        //传值方式 1.拼接网址字符串 2.域对象
        //使用session来储存验证码
        request.getSession().setAttribute("wcode", code);
        System.out.println(code);
        //写到网页上(通过 响应中的字节流 写回 网页)
        Vcode.write(response.getOutputStream());
    }
    private void fun(HttpServletResponse response) throws IOException {
        int width = 110;
        int height = 25;
        // 在内存中创建一个图像对象
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 创建一个画笔
        Graphics g = img.getGraphics();
        // 给图片添加背景色
        g.setColor(Color.PINK);// 设置一个颜色
        g.fillRect(1, 1, width - 2, height - 2);// 填充颜色
        // 给边框一个色
        g.setColor(Color.RED);
        g.drawRect(0, 0, width - 1, height - 1);// 设置边框的显示坐标
        // 设置文本样式
        g.setColor(Color.BLUE);
        g.setFont(new Font("宋体", Font.BOLD | Font.ITALIC, 15));
        // 给图片添加文本
        Random rand = new Random();
        int position = 20;
        for (int i = 0; i < 4; i++) {
            g.drawString(rand.nextInt(10) + "", position, 20);// 给图片填充文本
            position += 20;
        }
        // 添加9条干扰线
        for (int i = 0; i < 9; i++) {
            g.drawLine(rand.nextInt(width), rand.nextInt(height), rand.nextInt(width), rand.nextInt(height));
        }
        // 将图片对象以流的方式输出的客户端
        ImageIO.write(img, "jpg", response.getOutputStream());
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
HttpSession服务端技术

HttpSession服务端技术
session服务器会为每一个用户创建一个独立的HttpSession
如何测试?
HttpSession原理: 当用户第一次访问servlet时,服务器端会给该用户创建一个独立的session并且生成一个SessionID,这个SessionID在响应浏览器的时候,会被装进cookie中,从而被保存到浏览器中
当用户再一次访问servlet时,请求中会携带者cookie中的sessionID去访问,
服务器会根据这个SessionID区查看是否有对应的session对象,有就拿出来使用 没有就创建一个Session(相当于用第一次访问) (看病案例)
Context域 > Session域 > Request域
Session域 只要绘画部结束就会存在 但是session有默认存活时间 30分钟

session的钝化、活化

session的
钝化 :服务器停止时 如果session中保存了对象 并且该对象实现了序列化接口 Serializable 系统会把保存的session序列化到服务器的work文件夹的工程下 一个文件
活化 :当重新启动了服务器系统会重新把钝化下来的文件加载 加载会session中
继承序列化接口:implements Serializable

public class Book implements Serializable {
    private static final long serialVersionUID = 1L;
    private String id;
    private String name;
    public Book() {
        super();
    }
    public Book(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }   
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Book [id=" + id + ", name=" + name + "]";
    }
}
请求转发 获取转发器
    请求转发 获取转发器  
    请求转发只是服务器内部的访问 
    不管你怎么写路径 始终都在项目路径下
    RequestDispatcher dispatcher = request.getRequestDispatcher("/demo03");
    dispatcher.forward(request, response);
请求重定向 查看是否能获取域中的值
    请求重定向 查看是否能获取域中的值
    重定向是两次请求 不能获取到request域中的值
    请求转发能不能 访问本地服务器以外的服务器?
    重定向即可以访问本地服务器 又可以访问非本地服务器
    response.sendRedirect("/web-0328/demo03");
    //相当于=>
    response.setStatus(302); 
    response.setHeader("location", "/web-0328/demo03");
请求包含
    请求包含
    相当于把两个页面的响应 合成一个响应 返回给浏览器
    请求转发 浏览器只能响应一次数据
request.getRequestDispatcher("/demo03").include(request, response);
    out.write("123")//响应一句话
jsp
    <%-- jsp注释 --%>

    <%!
        // <% 加 ! 会被翻译到类的下面 也就是 全局变量
        // 还可以声明方法 静态 
    %>

    <%=num1 %> 相当于 out.print(num1); 都是输出  并且可以运算-->

    <%@ page session="true" %><%-- session 默认是true 相对于一个开关 --%>

    <%@ page errorPage="/error.jsp" %> <!-- 报错 跳转到其他页面 -->

    include指令 file指包含哪个页面 
        静态包含:在翻译成.java文件前 就已经合成了页面
        动态包含:先翻译成.java文件 
        代码逐行执行 当执行到动态包含的时候才会去编译被包含的页面 这样会生成两套文件

    <%--@include file="4.jsp"--%>
    taglib标签 可以导入jstl核心库     prefix="c" 表示给jstl标签库中的标签起一个别名
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page isErrorPage="true" %> <!-- 默认是关闭状态 打开捕获异常信息的对象的创建 -->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值