java+jsp实现滑动图片验证的完整代码
时间: 2024-02-02 10:05:30 浏览: 99
java图片滑动验证码
3星 · 编辑精心推荐
以下是Java+JSP实现滑动图片验证的完整代码:
1. 前端页面代码(sliding_verification.jsp)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>滑动图片验证</title>
<style>
#sliderContainer {
position: relative;
width: 400px;
height: 200px;
}
#slider {
position: absolute;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
width: 60px;
height: 60px;
background-color: #fff;
border: 2px solid #ddd;
border-radius: 50%;
box-shadow: 0 0 4px #ddd;
z-index: 10;
transition: all 0.3s ease;
}
#slider:hover {
box-shadow: 0 0 6px #ddd;
}
#sliderIcon {
display: block;
margin: 0 auto;
width: 30px;
height: 30px;
background-image: url(images/icon.png);
background-size: contain;
background-repeat: no-repeat;
background-position: center;
transition: all 0.3s ease;
}
#sliderText {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
width: 100%;
height: 30px;
line-height: 30px;
text-align: center;
font-size: 16px;
color: #333;
transition: all 0.3s ease;
}
#sliderText.on {
color: #fff;
background-color: #4CAF50;
}
#sliderText.off {
color: #333;
background-color: #ddd;
}
#sliderBg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: url(images/bg.jpg);
background-size: cover;
background-repeat: no-repeat;
background-position: center;
filter: blur(4px);
z-index: 1;
}
#sliderBlock {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 60px;
height: 60px;
background-color: #4CAF50;
border: 2px solid #ddd;
border-radius: 50%;
box-shadow: 0 0 4px #ddd;
z-index: 1;
transition: all 0.3s ease;
}
#sliderBlock:hover {
box-shadow: 0 0 6px #ddd;
}
#sliderBar {
position: absolute;
top: 50%;
transform: translateY(-50%);
width: 200px;
height: 4px;
background-color: #ddd;
z-index: 1;
}
#sliderBar.on {
background-color: #4CAF50;
}
#sliderBar.off {
background-color: #ddd;
}
</style>
</head>
<body>
<div id="sliderContainer">
<div id="sliderBg"></div>
<div id="sliderBlock"></div>
<div id="sliderBar" class="off"></div>
<div id="slider">
<span id="sliderIcon"></span>
<span id="sliderText" class="off">请按住滑块,拖动到最右边</span>
</div>
</div>
<script>
var sliderContainer = document.getElementById('sliderContainer');
var slider = document.getElementById('slider');
var sliderIcon = document.getElementById('sliderIcon');
var sliderText = document.getElementById('sliderText');
var sliderBlock = document.getElementById('sliderBlock');
var sliderBar = document.getElementById('sliderBar');
var isMouseDown = false;
var startX = 0, endX = 0;
var barWidth = sliderContainer.clientWidth - slider.clientWidth - 2;
slider.addEventListener('mousedown', function(e) {
isMouseDown = true;
startX = e.pageX - slider.offsetLeft;
sliderIcon.style.backgroundImage = 'url(images/icon-active.png)';
sliderText.classList.add('on');
sliderBlock.style.left = startX + 'px';
sliderBlock.style.boxShadow = '0 0 6px #ddd';
sliderBar.classList.remove('off');
});
document.addEventListener('mousemove', function(e) {
if (isMouseDown) {
var x = e.pageX - slider.offsetLeft;
if (x < 0) {
x = 0;
} else if (x > barWidth) {
x = barWidth;
}
sliderBlock.style.left = x + 'px';
sliderBar.style.width = x + 'px';
}
});
document.addEventListener('mouseup', function(e) {
if (isMouseDown) {
isMouseDown = false;
endX = e.pageX - slider.offsetLeft;
sliderIcon.style.backgroundImage = 'url(images/icon.png)';
sliderText.classList.remove('on');
sliderBlock.style.boxShadow = '0 0 4px #ddd';
if (endX >= barWidth) {
sliderBlock.style.left = barWidth + 'px';
sliderBar.style.width = barWidth + 'px';
sliderBar.classList.add('on');
sliderText.innerHTML = '验证通过';
setTimeout(function() {
alert('验证通过!');
}, 500);
} else {
sliderBlock.style.left = 0;
sliderBar.style.width = 0;
sliderBar.classList.add('off');
sliderText.innerHTML = '验证失败,请重试';
}
}
});
</script>
</body>
</html>
```
2. 后端代码(SlidingVerificationServlet.java)
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/sliding_verification")
public class SlidingVerificationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("sliding_verification.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
```
3. 图片资源
在项目的WebContent目录下创建images文件夹,并将以下图片资源存放其中:
- bg.jpg:背景图片,建议尺寸为400x200像素
- icon.png:未按下滑块时的图标,建议尺寸为30x30像素
- icon-active.png:按下滑块时的图标,建议尺寸为30x30像素
4. 部署
将以上代码打包成war包,上传至Tomcat服务器并启动。通过浏览器访问http://localhost:8080/项目名/sliding_verification即可查看效果。
注意:以上代码仅供参考,实际应用中可能需要根据业务需求进行改进和完善。
阅读全文