Java 如何获取验证码的当前会话值?
请帮助我任何人!我卡住了 我试图比较文本字段中插入的值和Captcha(图像)中显示的值,如果它们匹配,它将显示警报('Yes!')否则警报('No!')。但是,当我试图获取Captcha的会话值(Java 如何获取验证码的当前会话值?,java,javascript,html,jsp,Java,Javascript,Html,Jsp,请帮助我任何人!我卡住了 我试图比较文本字段中插入的值和Captcha(图像)中显示的值,如果它们匹配,它将显示警报('Yes!')否则警报('No!')。但是,当我试图获取Captcha的会话值(request.getSession().getAttribute(“Captcha”))时,它会获取它以前的值 如何确保Catcha的值与用户在图像上看到的相同 Captcha.jsp: <%@ page import="java.util.*"%> <%@ page import
request.getSession().getAttribute(“Captcha”)
)时,它会获取它以前的值
如何确保Catcha的值与用户在图像上看到的相同
Captcha.jsp:
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.awt.image.*"%>
<%@ page import="javax.imageio.*"%>
<%@ page import="java.awt.geom.*"%>
<%
String imageFormat = "jpg";
response.setContentType("image/" + imageFormat);
try {
// you can pass in fontSize, width, height via the request
Color backgroundColor = Color.red;
Color borderColor = Color.black;
Color textColor = Color.white;
Color circleColor = new Color(160, 160, 160);
Font textFont = new Font("Arial", Font.PLAIN, paramInt(request,
"fontSize", 24));
int charsToPrint = 6;
int width = paramInt(request, "width", 150);
int height = paramInt(request, "height", 80);
int circlesToDraw = 6;
float horizMargin = 20.0f;
float imageQuality = 0.95f; // max is 1.0 (this is for jpeg)
double rotationRange = 0.7; // this is radians
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
g.setColor(backgroundColor);
g.fillRect(0, 0, width, height);
// lets make some noisey circles
g.setColor(circleColor);
for (int i = 0; i < circlesToDraw; i++) {
int circleRadius = (int) (Math.random() * height / 2.0);
int circleX = (int) (Math.random() * width - circleRadius);
int circleY = (int) (Math.random() * height - circleRadius);
g.drawOval(circleX, circleY, circleRadius * 2,
circleRadius * 2);
}
g.setColor(textColor);
g.setFont(textFont);
FontMetrics fontMetrics = g.getFontMetrics();
int maxAdvance = fontMetrics.getMaxAdvance();
int fontHeight = fontMetrics.getHeight();
// i removed 1 and l and i because there are confusing to users...
// Z, z, and N also get confusing when rotated
// 0, O, and o are also confusing...
// lowercase G looks a lot like a 9 so i killed it
// this should ideally be done for every language...
// i like controlling the characters though because it helps prevent confusion
String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYabcdefhjkmnpqrstuvwxy23456789";
char[] chars = elegibleChars.toCharArray();
float spaceForLetters = -horizMargin * 2 + width;
float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
AffineTransform transform = g.getTransform();
StringBuffer finalString = new StringBuffer();
for (int i = 0; i < charsToPrint; i++) {
double randomValue = Math.random();
int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
char characterToShow = chars[randomIndex];
finalString.append(characterToShow);
// this is a separate canvas used for the character so that
// we can rotate it independently
int charImageWidth = maxAdvance * 2;
int charImageHeight = fontHeight * 2;
int charWidth = fontMetrics.charWidth(characterToShow);
int charDim = Math.max(maxAdvance, fontHeight);
int halfCharDim = (int) (charDim / 2);
BufferedImage charImage = new BufferedImage(charDim,
charDim, BufferedImage.TYPE_INT_ARGB);
Graphics2D charGraphics = charImage.createGraphics();
charGraphics.translate(halfCharDim, halfCharDim);
double angle = (Math.random() - 0.5) * rotationRange;
charGraphics.transform(AffineTransform
.getRotateInstance(angle));
charGraphics.translate(-halfCharDim, -halfCharDim);
charGraphics.setColor(textColor);
charGraphics.setFont(textFont);
int charX = (int) (0.5 * charDim - 0.5 * charWidth);
charGraphics
.drawString(
"" + characterToShow,
charX,
(int) ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics
.getAscent()));
float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
int y = (int) ((height - charDim) / 2);
//System.out.println("x=" + x + " height=" + height + " charDim=" + charDim + " y=" + y + " advance=" + maxAdvance + " fontHeight=" + fontHeight + " ascent=" + fontMetrics.getAscent());
g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
charGraphics.dispose();
}
// let's do the border
g.setColor(borderColor);
g.drawRect(0, 0, width - 1, height - 1);
//Write the image as a jpg
Iterator iter = ImageIO
.getImageWritersByFormatName(imageFormat);
if (iter.hasNext()) {
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
if (imageFormat.equalsIgnoreCase("jpg")
|| imageFormat.equalsIgnoreCase("jpeg")) {
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(imageQuality);
}
writer.setOutput(ImageIO.createImageOutputStream(response.getOutputStream()));
IIOImage imageIO = new IIOImage(bufferedImage, null, null);
writer.write(null, imageIO, iwp);
} else {
throw new RuntimeException("no encoder found for jsp");
}
// set session
request.getSession().setAttribute("captcha", finalString.toString());
System.out.println("Captcha.jsp: "+request.getSession().getAttribute("captcha"));//for console output
out.clear();
out = pageContext.pushBody();
g.dispose();
} catch (IOException ioe) {
throw new RuntimeException("Unable to build image", ioe);
}
%>
<%!public static String paramString(HttpServletRequest request,
String paramName, String defaultString) {
return request.getParameter(paramName) != null ? request
.getParameter(paramName) : defaultString;
}
public static int paramInt(HttpServletRequest request, String paramName,
int defaultInt) {
return request.getParameter(paramName) != null ? Integer
.parseInt(request.getParameter(paramName)) : defaultInt;
}%>
<html>
<head>
<script type="text/javascript">
function ValidateCaptcha(){
<%String captcha=(String)request.getSession().getAttribute("captcha");%>
var answer = document.getElementById("answer").value;
var captcha = '<%=captcha%>';
if(answer==captcha){
alert('Yes');
}
else{
alert('No');
}
}
</script>
</head>
<body>
<img src="Captcha.jsp"/>
<input id="answer" type="text">
<input type="button" value="Submit" onclick="ValidateCaptcha();">
<%System.out.println("ValidateCapture.jsp: "+request.getSession().getAttribute("captcha"));%> <!--for console output-->
</body>
要获取会话,我使用-request.getSession().getAttribute(“验证码”)代码>
控制台输出:
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="javax.servlet.*"%>
<%@ page import="javax.servlet.http.*"%>
<%@ page import="java.awt.*"%>
<%@ page import="java.awt.image.*"%>
<%@ page import="javax.imageio.*"%>
<%@ page import="java.awt.geom.*"%>
<%
String imageFormat = "jpg";
response.setContentType("image/" + imageFormat);
try {
// you can pass in fontSize, width, height via the request
Color backgroundColor = Color.red;
Color borderColor = Color.black;
Color textColor = Color.white;
Color circleColor = new Color(160, 160, 160);
Font textFont = new Font("Arial", Font.PLAIN, paramInt(request,
"fontSize", 24));
int charsToPrint = 6;
int width = paramInt(request, "width", 150);
int height = paramInt(request, "height", 80);
int circlesToDraw = 6;
float horizMargin = 20.0f;
float imageQuality = 0.95f; // max is 1.0 (this is for jpeg)
double rotationRange = 0.7; // this is radians
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
g.setColor(backgroundColor);
g.fillRect(0, 0, width, height);
// lets make some noisey circles
g.setColor(circleColor);
for (int i = 0; i < circlesToDraw; i++) {
int circleRadius = (int) (Math.random() * height / 2.0);
int circleX = (int) (Math.random() * width - circleRadius);
int circleY = (int) (Math.random() * height - circleRadius);
g.drawOval(circleX, circleY, circleRadius * 2,
circleRadius * 2);
}
g.setColor(textColor);
g.setFont(textFont);
FontMetrics fontMetrics = g.getFontMetrics();
int maxAdvance = fontMetrics.getMaxAdvance();
int fontHeight = fontMetrics.getHeight();
// i removed 1 and l and i because there are confusing to users...
// Z, z, and N also get confusing when rotated
// 0, O, and o are also confusing...
// lowercase G looks a lot like a 9 so i killed it
// this should ideally be done for every language...
// i like controlling the characters though because it helps prevent confusion
String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYabcdefhjkmnpqrstuvwxy23456789";
char[] chars = elegibleChars.toCharArray();
float spaceForLetters = -horizMargin * 2 + width;
float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
AffineTransform transform = g.getTransform();
StringBuffer finalString = new StringBuffer();
for (int i = 0; i < charsToPrint; i++) {
double randomValue = Math.random();
int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
char characterToShow = chars[randomIndex];
finalString.append(characterToShow);
// this is a separate canvas used for the character so that
// we can rotate it independently
int charImageWidth = maxAdvance * 2;
int charImageHeight = fontHeight * 2;
int charWidth = fontMetrics.charWidth(characterToShow);
int charDim = Math.max(maxAdvance, fontHeight);
int halfCharDim = (int) (charDim / 2);
BufferedImage charImage = new BufferedImage(charDim,
charDim, BufferedImage.TYPE_INT_ARGB);
Graphics2D charGraphics = charImage.createGraphics();
charGraphics.translate(halfCharDim, halfCharDim);
double angle = (Math.random() - 0.5) * rotationRange;
charGraphics.transform(AffineTransform
.getRotateInstance(angle));
charGraphics.translate(-halfCharDim, -halfCharDim);
charGraphics.setColor(textColor);
charGraphics.setFont(textFont);
int charX = (int) (0.5 * charDim - 0.5 * charWidth);
charGraphics
.drawString(
"" + characterToShow,
charX,
(int) ((charDim - fontMetrics.getAscent()) / 2 + fontMetrics
.getAscent()));
float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
int y = (int) ((height - charDim) / 2);
//System.out.println("x=" + x + " height=" + height + " charDim=" + charDim + " y=" + y + " advance=" + maxAdvance + " fontHeight=" + fontHeight + " ascent=" + fontMetrics.getAscent());
g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
charGraphics.dispose();
}
// let's do the border
g.setColor(borderColor);
g.drawRect(0, 0, width - 1, height - 1);
//Write the image as a jpg
Iterator iter = ImageIO
.getImageWritersByFormatName(imageFormat);
if (iter.hasNext()) {
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
if (imageFormat.equalsIgnoreCase("jpg")
|| imageFormat.equalsIgnoreCase("jpeg")) {
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(imageQuality);
}
writer.setOutput(ImageIO.createImageOutputStream(response.getOutputStream()));
IIOImage imageIO = new IIOImage(bufferedImage, null, null);
writer.write(null, imageIO, iwp);
} else {
throw new RuntimeException("no encoder found for jsp");
}
// set session
request.getSession().setAttribute("captcha", finalString.toString());
System.out.println("Captcha.jsp: "+request.getSession().getAttribute("captcha"));//for console output
out.clear();
out = pageContext.pushBody();
g.dispose();
} catch (IOException ioe) {
throw new RuntimeException("Unable to build image", ioe);
}
%>
<%!public static String paramString(HttpServletRequest request,
String paramName, String defaultString) {
return request.getParameter(paramName) != null ? request
.getParameter(paramName) : defaultString;
}
public static int paramInt(HttpServletRequest request, String paramName,
int defaultInt) {
return request.getParameter(paramName) != null ? Integer
.parseInt(request.getParameter(paramName)) : defaultInt;
}%>
<html>
<head>
<script type="text/javascript">
function ValidateCaptcha(){
<%String captcha=(String)request.getSession().getAttribute("captcha");%>
var answer = document.getElementById("answer").value;
var captcha = '<%=captcha%>';
if(answer==captcha){
alert('Yes');
}
else{
alert('No');
}
}
</script>
</head>
<body>
<img src="Captcha.jsp"/>
<input id="answer" type="text">
<input type="button" value="Submit" onclick="ValidateCaptcha();">
<%System.out.println("ValidateCapture.jsp: "+request.getSession().getAttribute("captcha"));%> <!--for console output-->
</body>
我看到了代码。特别是关于固定时段。我认为你不应该设置属性值。首先,当浏览器关闭时会话无效,或者在tomcat配置文件中没有操作时间长达30分钟。但是,如果您没有关闭浏览器,您将获得与以前值相同的验证码。因此,我们可以这样做,遵循以下代码:request.setAttribute(“captcha”,finalString.toString())
//设置值和request.getAttribute(“captcha”)//获得价值
像这样。当我们点击图片时,我们会得到不同的验证码值。请求对象也会改变。为什么要这样做?因为当“请求”到服务器并且服务器稍后响应时,请求对象无效。下一个请求将生成新的请求对象。仅此而已,祝你好运。所以实际上你要求我添加这一行-request.getAttribute(“captcha”);在这一行之后-request.setAttribute(“captcha”,finalString.toString());内部Captcha.jsp?我刚做了,但仍然得到了相同的输出:(