验证码一致提示出错
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> <style>#code{height:30px;}</style> </head> <body> <form action="submit.action"> <p>验证码:<input type="text" name="kaptcha" id="code" maxlength="4" placeholder="请输入验证码" /> <img src="http://localhost:8080/code/kaptcha.jpg" id="checkcode" /> </p> <p><input type="button" value="登陆" id="login" ></p> <div id="result"></div> </form> <script src="http://localhost:8080/code/js/jquery-1.12.4.min.js"></script> <script> $(function(){ $("#changecode").on("click",function(){ $(this).attr("src","http://localhost:8080/code/kaptcha.jpg?d="+new Date().getTime()); }); //登陆按钮绑定点击事件 $("#login").on("click",function(){ var code=$("#code").val(); alert(code); var params={"code":code}; $.post("http://localhost:8080/code/loginServlet",params,function(data){ if(data=="success"){ $("#result").html("验证码输入正确"); }else{ $("#result").html("验证码输入不正确"); $("#code").val("").focus(); } }) }) }); </script> </body> </html>
package com.imooc; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jdt.internal.compiler.impl.Constant; import com.google.code.kaptcha.Constants; /** * Servlet implementation class loginServlet */ @WebServlet("/loginServlet") public class loginServlet extends HttpServlet { 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(Constants.KAPTCHA_SESSION_KEY); out.println("sessionCode:"+sessionCode); if(code!=null&&sessionCode!=null) { if(code.equalsIgnoreCase(sessionCode)) { out.print("success"); }else { out.println("fail"); } }else { out.println("fail"); } out.flush(); out.close(); } 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://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>code</display-name> <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> </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.NoNoise</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Kaptcha</servlet-name> <url-pattern>/kaptcha.jpg</url-pattern> </servlet-mapping> </web-app>
14
收起
正在回答 回答被采纳积分+1
2回答
慕粉1473412646
2018-06-21 11:53:46
明明输入正确,一直说输入不正确
chrismorgen
2018-06-21 11:35:35
将你的代码粘贴到老师的代码中,并没提示错误,只是验证码无法显示,建议你将错误的提示信息贴出来,或者将其他相关的代码贴出来方便我们检查。祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星