关于验证码提示
来源:5-1 完成验证功能
qq_左撇子A_0
2018-02-26 20:25:48
之前验证码的kaptcha.session.key在xml配置了kcode值,但是我是注释掉的,后台获取到的前台传过来验证码是一样的,就是前台的javscript里面的if判断一致是flase,请老师们看看
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String basePath= request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+
request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>验证码框架演示</title>
<script type="text/javascript" src="<%=basePath %>/resources/js/jquery-1.12.4.min.js"></script>
</head>
<body>
<form action="submit.action">
<p>验证码:<input type="text" name="kaptcha" id="code" maxlength="4" placeholder="请输入验证码"/>
<img src="http://localhost:8080/mooc_demo3/kaptcha.jpg" id="changecode"/>
</p>
<input type="button" value="登录" id="login" />
<div id="result"></div>
</form>
<script type="text/javascript">
$(function(){
$("#changecode").on("click",function(){
$(this).attr("src","http://localhost:8080/mooc_demo3/kaptcha.jpg?d="+new Date().getTime());
});
//给登录按钮绑定点击事件
$("#login").on("click",function(){
//获取用户输入的验证码
var code = $("#code").val();
var params = {"code":code};
alert(code);
$.post("http://localhost:8080/mooc_demo3/LoginServlet",params,function(data){
alert(data);
if(data=="success"){
$("#result").html("验证码输入正确");
}else{
$("#result").html("验证码输入有误,请重新输入...");
$("#code").val("").focus();
}
alert(data=="success");
});
});
})
</script>
</body>
</html>@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取输出流
PrintWriter out = response.getWriter();
//获取验证码
String code = request.getParameter("code");
//获取验证吗框架生成的验证码
String sessionCode = (String)request.getSession().getAttribute("kcode");
System.out.println(code+"--------"+sessionCode);
//判断
if(code != null && sessionCode != null) {
if(code.equalsIgnoreCase(sessionCode)) {
out.println("success");
System.out.println(code+"++++++++++++++"+sessionCode);
}else {
out.print("flase");
}
}else {
out.println("flase");
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>mooc_demo3</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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>150</param-value> </init-param> <init-param> <param-name>kaptcha.image.height</param-name> <param-value>50</param-value> </init-param> <init-param> <param-name>kaptcha.textproducer.char.length</param-name> <param-value>4</param-value> </init-param> <init-param> <param-name>kaptcha.noise.impl</param-name> <param-value>com.google.code.kaptcha.impl.DefaultNoise</param-value> </init-param> <init-param> <param-name>kaptcha.textproducer.char.string</param-name> <param-value>abcde2345678gfynmnpwx</param-value> </init-param> <init-param> <param-name>kaptcha.obscurificator.impl</param-name> <param-value>com.google.code.kaptcha.impl.FishEyeGimpy</param-value> </init-param> <init-param> <param-name>kaptcha.session.key</param-name> <param-value>kcode</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/kaptcha.jpg</url-pattern> </servlet-mapping> </web-app>
1回答

把out.println("succcess"),改成out.print("success"),再试试。加ln会换有个回车换行被接收。
祝学习愉快!
相似问题