Check codes

1. Consider first filter is filtering the background image access codes method
2.equalsIgnoreCase (Comparative if the strings are equal regardless of the case)

A reception:
1. This is jsp pages tag:
<Script the src = "../ Styles / JS / jQuery-3.2.1.min.js" = type "text / JavaScript"> </ Script>
<Link the href = "../ Styles / CSS / doctorlogin.css" the rel = "this stylesheet" />
<Link the rel = 'this stylesheet' the href = '.. / Styles / on Bootstrap / CSS / bootstrap.min.css'>
2. The following is script, take over the direct use on the line, pay attention to the address table next to jump into their own, with my can.
function reload () {
document.getElementById ( "Image"). src = "<% = request.getContextPath ? ()%> / picCheck / imageServlet DATE = "+ new new a Date () the getTime ();.
$ (" # checkcode ") (") Val. "; // Clear the codes
}

function verificationcode () {
var text TRIM $ = ($ ( "# checkcode") Val ().);.

var SP = $ ( '# span') Val ();.

$ .post ( "${pageContext.request.contextPath}/Verification/verificationServlet",{op:text},function(data){
Data = the parseInt ($ TRIM (Data).);
IF (Data> 0) {
.. $ ( "# span") text ( "! verification is successful") CSS ( "Color", "Green");
} the else {
. / * $ ( "# span ") text ( " verification failed!") CSS ( "Color", "Red");. * /
IF (text == "") {
$ ( "# span") text. ( "authentication is empty. Please reenter! ') CSS (" Color "," Red ");.
} the else {
$ (" # span ") text (" error codes ") .css (" color ", . "Red");
}



reload (); // validation fails to be replaced after a verification code
}
});
. $ ( "# checkcode") Val ( ""); // Clear the codes
}
3. here is a body member section (tag inside of my class is designed for static css styles, do not want to design can be ignored)
<div class = "Home">
<div class = "home_login">

<h4 class = "title"> <strong> Love Meme da hospital management system 1 Ya ◡ Ya 1 </ strong> </ h3>
<the INPUT of the type = "text" name = "checkcode" the above mentioned id = "checkcode" class = "home_login_button3" placeholder = "Please enter PIN" /> <br />
<div>
<IMG the src = "<% = request.getContextPath ()%> / picCheck / imageServlet" Alt = "codes" ID = "Image" class = "home_login_check_code" />
<A the href = "JavaScript: reload ( ); "class =" home_login_button3_font " > <label> another one </ label> </a> <br>
<span ID =" span "> </ span>
</ div>
<Button type =" Button "class = "home_login_button1" onclick = "javascript : verificationcode ()"> submit </ Button>

</ div>
</ div>
II the following is a background portion (the background (controller level), a total of two portions, one is written verification codes controller, a verification code is to determine whether the proper controller)
1. determines whether the correct verification code controller layer (brought directly on the line)
/ **
*
* /
Package mvct.Controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* @author zhouxingxing
* @package mvct.Controller
* @Description TODO
* @date:2018年4月25日下午4:09:22
*/
@Controller
@RequestMapping("Verification")
public class PicCodeVerificationServletController extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

@RequestMapping("/verificationServlet")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

HttpSession session =request.getSession();
String verificationCode = (String)session.getAttribute("verificationCode");
String checkcode = request.getParameter("op");
PrintWriter out = response.getWriter();
if(checkcode.equals(verificationCode)){
out.println(1);
}else{
out.println(0);
}
out.flush();
out.close();
}





}

2.编写验证码的controller(直接拿过来用即可)
/**
*
*/
package mvct.Controller;

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

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author zhouxingxing
* @package mvct.Controller
* @Description TODO
* @date:2018年4月25日下午4:00:23
*/
@Controller
@RequestMapping("/picCheck")
public class PicCheckController extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

@ResponseBody
@RequestMapping("/imageServlet")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");

BufferedImage bfi = new BufferedImage(80,25,BufferedImage.TYPE_INT_RGB);
Graphics g = bfi.getGraphics();
g.fillRect(0, 0, 80, 25);

//验证码字符范围
char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
Random r = new Random();
int index;
StringBuffer sb = new StringBuffer(); //保存字符串
for(int i=0; i<4; i++){
index = r.nextInt(ch.length);
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
Font font = new Font("宋体", 30, 20);
g.setFont(font);
g.drawString(ch[index]+"", (i*20)+2, 23);
sb.append(ch[index]);
}

// 添加噪点
int area = (int) (0.02 * 80 * 25);
for(int i=0; i<area; ++i){
int x = (int)(Math.random() * 80);
Y = int (int) (Math.random () * 25);
bfi.setRGB (X, Y, (int) (Math.random () * 255));
}

// set the interference line verification codes
for ( 0 = I int; I <. 6; I ++) {
// Get random disturbance start and end lines
int Xstart = (int) (Math.random () * 80);
int yStart = (int) (Math.random () 25 *);
int the xend = (int) (Math.random () * 80);
int 's ending = (int) (Math.random () * 25);
g.setColor (Interline (. 1, 255));
G. the drawLine (Xstart, yStart, the xend, 's ending);
}
the HttpSession Request.getSession the session = (); // save to the session
session.setAttribute ( "verificationCode", sb.toString ());
ImageIO.write (BFI, "JPG" // written to the output stream;, response.getOutputStream ())
}



/ **
between the acquired random * color values, r, g, b values in the Low to High
* @Param L left section
* @param R Right interval
* @return return random color value
* /
Private static Color Interline (Low int, int High) {
IF (Low> 255)
Low = 255;
IF (High> 255)
High 255 =;
IF (Low <0)
Low = 0;
IF (High <0)
High = 0;
int = interval The High - Low;
int + R & lt Low = (int) (Math.random () * interval The);
int G Low + = (int) (Math.random () * interval The);
int Low + B = (int) (Math.random () * interval The);
return new new Color (R & lt, G, B);
}

---------------------
Original: https: //blog.csdn.net/qq_36831718/article/details/80095770

Guess you like

Origin www.cnblogs.com/YingJian/p/11009476.html