第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

為何我每次第一次執(zhí)行程序的話都會(huì)出現(xiàn)session中的保存驗(yàn)證碼取出來(lái)為空?

public?class?LoginServlet?extends?HttpServlet?{
	
	public?void?doPost(HttpServletRequest?req,?HttpServletResponse?resp)
			throws?ServletException,?IOException?{
		doGet(req,?resp);
	}
	public?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)
			throws?ServletException,?IOException?{
		//后臺(tái)記錄的驗(yàn)證碼(均為大寫字母)
		String?verifyPic?=?(String)?req.getSession().getAttribute("verifyPicture");
		//前臺(tái)提交過(guò)來(lái)的驗(yàn)證碼(用戶不一定輸入大寫字母)
		String?verifyCode?=?req.getParameter("verifyCode");
		System.out.println("verifyPicture為:"+verifyPic);
		//前臺(tái)輸入的全部轉(zhuǎn)換為大寫字母
		verifyCode?=?verifyCode.toUpperCase();
		System.out.println("verifyCode為:"+verifyCode);
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter?out?=?resp.getWriter();
		
		if(verifyCode.equals(verifyPic)){
			out.print("驗(yàn)證碼輸入正確!");
		}else{
			out.print("驗(yàn)證碼輸入錯(cuò)誤!");
		}
		out.flush();
		out.close();
	}
}
public?class?ImageServlet?extends?HttpServlet?{
	
	public?void?doGet(HttpServletRequest?req,?HttpServletResponse?resp)
			throws?ServletException,?IOException?{
		//1、定義一個(gè)BufferedImage對(duì)象
		BufferedImage?bfi?=?new?BufferedImage(65,?23,?BufferedImage.TYPE_INT_RGB);
		//2、獲取Graphics對(duì)象
		Graphics?g?=?bfi.getGraphics();
		Color?c?=?new?Color(180,200,255);//Color(int?r,?int?g,?int?b)?指定紅色、綠色和藍(lán)色值的不透明的?sRGB顏色,最后一個(gè)值默認(rèn)為255
		g.setColor(c);
		g.fillRect(0,?0,?65,?23);
		
		//3、通過(guò)Random隨機(jī)產(chǎn)生驗(yàn)證碼信息
		char[]?ch?=?"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
		Random?random?=?new?Random();
		int?len?=?ch.length;
		int?index;//索引
		StringBuffer?sb?=?new?StringBuffer();
		for(int?i=0;i<4;i++){//生成4位驗(yàn)證碼
			index?=?random.nextInt(len);//在ch數(shù)組中隨機(jī)字符
			//4、使用Graphics繪制圖片
			g.setColor(new?Color(random.nextInt(88),?random.nextInt(188),?random.nextInt(255)));
			g.drawString(ch[index]+"",?(i*15)+3,?18);
			sb.append(ch[index]);
		}
		//5、記錄驗(yàn)證碼信息到session中
		req.getSession().setAttribute("verifyPicture",?sb.toString());
		//6、使用ImageIO輸出圖片
		ImageIO.write(bfi,?"JPG",?resp.getOutputStream());
	}
}

index.jsp頁(yè)面:

<%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%>
<%
String?path?=?request.getContextPath();
String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">
<html>
??<head>
????<base?href="<%=basePath%>">
????
????<title>驗(yàn)證碼</title>
	<meta?http-equiv="pragma"?content="no-cache">
	<meta?http-equiv="cache-control"?content="no-cache">
	<meta?http-equiv="expires"?content="0">????
	<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">
	<meta?http-equiv="description"?content="This?is?my?page">
	<!--
	<link?rel="stylesheet"?type="text/css"?href="styles.css">
	-->
	<script?type="text/javascript">
		function?reloadCode(){
			var?time?=?new?Date().getTime();
			document.getElementById("picCode").src?=?"<%=request.getContextPath()%>/myServlet/ImageServlet?d="+time;
		}
	</script>
??</head>
??
??<body>
??<form?action="<%=request.getContextPath()%>/myServlet/LoginServlet"?method="get">
???	?驗(yàn)證碼:<input?type="text"?name="verifyCode">
????<img?alt="驗(yàn)證碼"?id="picCode"?src="<%=request.getContextPath()%>/myServlet/ImageServlet">
????<a?href="javascript:reloadCode();">看不清楚</a>?<br>
????<input?type="submit"?value="提交">&nbsp;&nbsp;&nbsp;<input?type="reset"?value="重置">
??</form>
??</body>
</html>

第一次執(zhí)行每次都有問(wèn)題,如果先刷新再填寫驗(yàn)證碼就沒(méi)問(wèn)題了,第一次獲取不到session中的值,為何?求大神指導(dǎo)!

正在回答

4 回答

我也是 取不到session的值 都是null?

0 回復(fù) 有任何疑惑可以回復(fù)我~

我也是同樣的問(wèn)題,第一次總是空指針異常,刷新下就能正常運(yùn)行

0 回復(fù) 有任何疑惑可以回復(fù)我~

解決了吧

1 回復(fù) 有任何疑惑可以回復(fù)我~

我執(zhí)行了一下你的代碼,沒(méi)有你說(shuō)的這個(gè)問(wèn)題,一開(kāi)始就成功了

1 回復(fù) 有任何疑惑可以回復(fù)我~
#1

寶慕林9393252

如果是struts怎么接收保存在session里面的驗(yàn)證碼呢??
2015-09-05 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

為何我每次第一次執(zhí)行程序的話都會(huì)出現(xiàn)session中的保存驗(yàn)證碼取出來(lái)為空?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)