Rewriting URL mechanism: In order to ensure that the address of a URL is unique, the parameters passed to the server can be different each time. Here, when implementing the function of changing the verification code, the value of the parameter list is changed to refresh. Please refer to this article for the detailed code
Rewriting URL mechanism: In order to ensure that the address of a URL is unique, the parameters passed to the server can be different each time.
The header information of the data request can be analyzed: the header information includes http protocol, IP address, port number, project name, and request parameter list. If the resources you want to access do not change, you can only change the parameters. Continuous table.
When implementing the function of changing the verification code, the value of the parameter list is changed to refresh.
Detailed code implementation:
<%@page import="javax.imageio.ImageIO"%> <%@page import="java.awt.Font"%> <%@page import="java.awt.Color"%> <%@page import="java.awt.Graphics"%> <%@page import="java.awt.image.BufferedImage"%> <%@ page contentType="image/jpeg" language="java" import="java.util.*" pageEncoding="UTF-8"%> <% int w=100; int h=30; BufferedImage bi=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); Graphics g=bi.getGraphics(); Color c=g.getColor(); Font f=g.getFont(); Random r=new Random(); Color bg=new Color(150+r.nextInt(100),150+r.nextInt(100),150+r.nextInt(100)); g.setColor(bg); g.fillRect(0, 0, w, h); String code=""; for(int i=1;i<=4;i++){ int num=r.nextInt(10); code=code+num; Color num_c=new Color(r.nextInt(150),r.nextInt(150),r.nextInt(150)); g.setColor(num_c); g.drawString(String.valueOf(num), 20*i, h/2); } request.getSession().setAttribute("code", code); //清空缓存 response.setHeader("pragma", "bo-cache"); response.setHeader("cache-control", "bo-cache"); response.addDateHeader("expires", 0); ImageIO.write(bi, "jpeg", response.getOutputStream()); out.close(); %>
Add login page:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" pageEncoding="UTF-8"%> <%String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> <script type="text/javascript"> function changeimage(){ var d=new Date();//生成时间戳, document.getElementById("img").src="image.jsp?t="+d;//由变化的时间使参数连表发生变化,url重写 } </script> </head> <body> <font color="red">${requestScope.msg }</font> <form action="loginServlet" method="post"> name:<input type="text" name="uname"><br> pwd:<input type="pwd" name="upwd"><br> code:<input type="text" name="code" size="5"><img id="img" alt="" src="image.jsp "><a onclick="changeimage()">换一张</a><br> <input type="submit" > </form> </body> </html>
Use of time changes , each time a timestamp is generated and passed to the requested URL, the purpose of rewriting the URL is achieved, thus realizing the refresh function of changing one page.
The above is the detailed content of URL rewriting to implement verification code transformation. For more information, please follow other related articles on the PHP Chinese website!