Kaptcha is an open source project based on SimpleCaptcha's verification code.
Kaptcha is more convenient to use, just add the jar package dependency and simply configure it to use. All kaptcha configurations can be done via web.xml. If Spring MVC is used in your project, there is another way to do this.
First, a simple jsp-servlet project 1. Add jar package dependenciesIf you use maven to centrally manage jar packages, add dependency to the project's pom.xml
Xml code
"! -- kaptcha --"
Dependency
"groupId" com.google.code.kaptcha"/groupId"
"arTIfacTId" kaptcha "/arTIfacTId"
"version" 2.3.2 "/version"
/dependency
"! -- kaptcha --"
Dependency
"groupId" com.google.code.kaptcha"/groupId"
"artifactId" kaptcha "/artifactId"
"version" 2.3.2 "/version"
/dependency
If it is a non-maven managed project, download the kaptcha jar package directly on the official website and add it to the project lib library.
2. Configure web.xmlAs mentioned above, kaptcha is configured in web.xml. We must configure kaptcha servlet in web.xml, as follows:
Xml code
Servlet
"servlet-name" Kaptcha "/servlet-name"
Servlet-class com.google.code.kaptcha.servlet.KaptchaServlet/servlet-class
/servlet
Servlet-mapping
"servlet-name" Kaptcha "/servlet-name"
"url-pattern"/kaptcha.jpg "/url-pattern"
/servlet-mapping
Servlet
"servlet-name" Kaptcha "/servlet-name"
Servlet-class com.google.code.kaptcha.servlet.KaptchaServlet/servlet-class
/servlet
Servlet-mapping
"servlet-name" Kaptcha "/servlet-name"
"url-pattern"/kaptcha.jpg "/url-pattern"
/servlet-mapping
The url-pattern of the servlet can be customized.
All of kaptcha's parameters have a default configuration, and if we don't display the configuration, the default configuration will be taken.
If you want to display the configuration kaptcha, when configuring the Servlet corresponding to kaptcha, add the parameter configuration of the response in init-param. An example is as follows:
Xml code
Servlet
"servlet-name" Kaptcha "/servlet-name"
Servlet-class com.google.code.kaptcha.servlet.KaptchaServlet/servlet-class
Init-param
"param-name" kaptcha.image.width "/param-name"
Param-value 200/param-value
Description Width in pixels of the kaptcha image. "/description"
"/init-param"
Init-param
"param-name" kaptcha.image.height "/param-name"
Param-value 50/param-value
Description "Height in pixels of the kaptcha image." /description
"/init-param"
Init-param
"param-name" kaptcha.textproducer.char.length "/param-name"
"param-value" 4 "/param-value"
"Description" The number of characters to display. "/description"
"/init-param"
Init-param
"param-name" kaptcha.noise.impl "/param-name"
"param-value" com.google.code.kaptcha.impl.NoNoise "/param-value"
Description "The noise producer. "/description"
"/init-param"
/servlet
Servlet
"servlet-name" Kaptcha "/servlet-name"
Servlet-class com.google.code.kaptcha.servlet.KaptchaServlet/servlet-class
Init-param
"param-name" kaptcha.image.width "/param-name"
Param-value 200/param-value
Description Width in pixels of the kaptcha image. "/description"
"/init-param"
Init-param
"param-name" kaptcha.image.height "/param-name"
Param-value 50/param-value
Description "Height in pixels of the kaptcha image." /description
"/init-param"
Init-param
"param-name" kaptcha.textproducer.char.length "/param-name"
"param-value" 4 "/param-value"
"Description" The number of characters to display. "/description"
"/init-param"
Init-param
"param-name" kaptcha.noise.impl "/param-name"
"param-value" com.google.code.kaptcha.impl.NoNoise "/param-value"
Description "The noise producer. "/description"
"/init-param"
/servlet
3. Page callHtml code
Formaction=“submit.actionâ€
Inputtype=“textâ€name=â€kaptchaâ€value=â€â€/â€imgsrc=“kaptcha.jpgâ€/
/form
"form action="submit.action"
"input type="text" name=“kaptcha†value=â€â€ /â€img src=“kaptcha.jpg†/》
/form
4. Verify the code in the submit action methodJava code
/ / From the session to extract the verification code text value generated by the servlet
String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
/ / Get the verification code entered on the user page
String kaptchaReceived = request.getParameter("kaptcha");
/ / Verify that the verification code is correct
If (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)){
setError("kaptcha", "Invalid validation code.");
}
/ / From the session to extract the verification code text value generated by the servlet
String kaptchaExpected = (String)request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
/ / Get the verification code entered on the user page
String kaptchaReceived = request.getParameter("kaptcha");
/ / Verify that the verification code is correct
If (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)){
setError("kaptcha", "Invalid validation code.");
}
Note: Make sure the JDK is set to -Djava.awt.headless=true
Basic Physics Experiment Instrument Series
Basic physics experiment instrument series, used in physics laboratories of colleges and universities.
Basic Physics Experiment Instrument,Light And Optical Instruments,Optical Viewing Instrument,Microscope Light Source Instrument
Yuheng Optics Co., Ltd.(Changchun) , https://www.yhenoptics.com