重定向与转发的区别学习笔记

本文介绍了Web开发中请求重定向的基本概念与实现方法,并通过示例代码详细展示了如何使用Java Servlet实现页面重定向及自动生成包含数字和字母的验证码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文字说明:
一个web资源收到客户端请求后,通知客户端去访问另外一个web资源,这称之为请求重定向。

重定向与转发的区别学习笔记

实现方式
response.sendRedirect(“/welcome.html”)
实现原理:
302状态码和location头即可实现重定向

列如:

重定向与转发的区别学习笔记

java

    // 取得提交的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkCode = request.getParameter("checkCode");

        System.out.println("用户名:"+username);
        System.out.println("密码"+password);
        System.out.println("验证码:"+checkCode);

        //重定向到Message.html
        //这是/ 表示webapps目录
        response.sendRedirect("/day04/success.html");

login.html

<!DOCTYPE  HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登陆</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
   <form action='/day04/LoginServlet' method='post'>
       <table border="1" align="center">
           <caption>用户登陆【mvc】</caption>
           <tr>
               <th>用户</th>
               <td><input type="text" name="username"></td>
           </tr>
           <tr>
               <th>密码</th>
               <td><input type="password" name="password"></td>
           </tr>
           <tr>
              <th>验证码</th>
              <td>
              <input type="text" name="checkCode"/>
              </td>
              <td>
                <img alt="" src="/day04/Yanzm01">
              </td>             
           </tr>
           <tr>
           <td colspan="2" align="center">
              <input type="submit" value="提交">         
           </td>
           </tr>
       </table>

   </form>  
  </body>
</html>

验证码:


import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//生成随机图片
public class Yanzm01 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //设置浏览器不能缓存
        response.setHeader("expires","-1"); //ie浏览器
        response.setHeader("cache-control","no-cache");
        response.setHeader("pragma","no-cache");

        //在内存中构造一副图片
        BufferedImage image = new BufferedImage(100,25,BufferedImage.TYPE_INT_RGB);

        //取得画笔
        Graphics g =  image.getGraphics();

        //设置字体大小和颜色
        g.setColor(Color.YELLOW);
        g.setFont(new Font("黑体",Font.BOLD,22));
        //在图片中显示字符串 1azb
        g.drawString(getStringfor(),20,20);

        ImageIO.write(image, "jpg", response.getOutputStream());

    }

    /**产生一个随机数*/
    private String getString()
    {

        return UUID.randomUUID().toString();
    }

    /**产生一个4位随机数第一个数字,第二个字母,第三个数字,第四个字母*/
    private String getStringfor()
    {
        String str = "";
        String numberAndLetter="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        int length = numberAndLetter.length(); //取得全部长度
        for(int i=1;i<=4;i++){  //验证码长度
            if(i==1 || i==3){//如果1、3、位数字
                while(true){
                    Random random = new Random();
                    int index = random.nextInt(length);
                    //substring(int beginIndex, int endIndex)
                    //返回一个新的字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾。
                    String value = numberAndLetter.substring(index,index+1);  
                    if(value.matches("[0-9]")){ //正则判断
                        str += value;
                        break;
                    }
                }
            }else if(i==2 || i==4){//如果2、4位大小字母
                while(true){
                    Random random = new Random();
                    int index = random.nextInt(length);
                    String value = numberAndLetter.substring(index,index+1);
                    if(value.matches("[A-Za-z]")){
                        str += value;
                        break;
                    }
                }
            }
        }
        return str;

    }

}

转载于:https://blog.51cto.com/357712148/2104716

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值