5-1问题,没有弹出窗口
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>关于验证码框架</title>
<style>#code{height:30px;}</style>
</head>
<body>
<form action="submit.action" >
<p><font size="5" color="red">验证码:</font><input type="text" name="kaptcha" id="code" maxlength="4" placeholder="请输入验证码"/>
<img src="http://localhost:8080/imooccode/kaptcha.jpg" id="changecode"/>
</p>
<p>
<input type="button" id="login" value="登录">
</p>
<div id="result"></div>
</form>
<script src="jquery-1.12.4.min.js"></script>
<script>
$(function(){
$("#changecode").on("click",function(){
$(this).attr("src","http://localhost:8080/imooccode/kaptcha.jpg?d="+new Date().getTime());
});
$("#login").on("click",function(){
//获取用户输入的验证码
var code = $("#code").val();
//alert(code);
var params = {"code":code};
$.post("http://localhost:8080/imooccode/login",params,function(data){
if(data=="success"){
$("#result").html("验证码输入正确");
}else{
$("#result").html("验证码输入错误,请重新输入");
$("#code").val("").focus();
}
});
});
});
</script>
</body>
</html>
package com.imooc.code;
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;
/**
* Servlet implementation class LoginServlet1
*/
@WebServlet("/LoginServlet1")
public class LoginServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet1() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//需要获取浏览器输出流对象
PrintWriter out = response.getWriter();
//获取用户传递过来的验证码
String code = request.getParameter("code");
System.out.println(code);
//获取验证码框架产生的验证码
String sessionCode = (String)request.getSession().getAttribute("kcode");
if(code!=null && sessionCode!=null) {
if(code.equalsIgnoreCase(sessionCode)) {
out.print("success");
}
}else {
out.print("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://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" version="3.1">
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>100</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>40</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>28</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>qwertyuiopasdfghjklzxcvbnm123456789</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.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>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.imooc.code.LoginServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
正在回答 回答被采纳积分+1
如果想使用弹窗显示输入的内容你可以将下面注释的代码取消注释,试试看。如果我的建议解决了你的问题,请采纳,祝学习愉快~
你的html代码中 <script src="jquery-1.12.4.min.js"></script> 应改为 :
<script src="js/jquery-1.12.4.min.js" type="text/javascript"></script>
当然src="js/jquery-1.12.4.min.js"是根据你的相对位置来的,例如我是放在web目录下js文件夹下的,所以是src="js/jquery-1.12.4.min.js"你的可以根据你自己的位置进行调整。
另外你写的这个是在页面显示,并不是弹窗。如果是弹窗可以用alert(“ 验证码正确”)
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星