您好,登錄后才能下訂單哦!
<%@ 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>表單提交</title>
<script type="text/javascript">
function _hyz() {
/*
1. 獲取img元素
2. 改變src的指向
*/
var img = document.getElementById("img");
//需要加一個參數才能干掉瀏覽器的緩存
img.src = "/tools/VerifyCodeServlet?a=" + new Date().getTime();
}
</script>
</head>
<body>
<!--
生成驗證碼:
1. 寫表單,其中包含圖片(驗證碼)
2. 讓圖片顯示出來:
把<img>的src指向VerifyCodeServlet,你需要在web.xml中部署VerifyCodeServlet
-->
<form action="/tools/loginServlet" method="post">
<!-- 添加一個參數:method=login -->
<input type="hidden" name="method" value="login">
用戶名:<input type="text" name="name"/><br />
密碼:<input type="password" name="pwd" /><br />
驗證碼:<input type="text" name="verifyCode" /><br />
<img src="/tools/VerifyCodeServlet" id="img"/><br />
<a href="javascript:_hyz()">換一張</a>
<input type="submit" value="提交" />
</form>
</body>
</html>
<servlet>
<servlet-name>VerifyCodeServlet</servlet-name>
<servlet-class>cn.itcast.vcode.servlet.VerifyCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VerifyCodeServlet</servlet-name>
<url-pattern>/VerifyCodeServlet</url-pattern>
</servlet-mapping>
package cn.itcast.test;
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;
import cn.itcast.servlet.BaseServlet;
/**
備注:*/
@WebServlet("/loginServlet")
br/>*/
@WebServlet("/loginServlet")
public String login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//檢驗驗證碼
//1. 獲取表單中的驗證碼
String verifyCode = request.getParameter("verifyCode");
//2. 獲取圖片上的文字
String vcode = (String)request.getSession().getAttribute("vCode");
//3. 檢驗
System.out.println(verifyCode.equalsIgnoreCase(vcode));
return null;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。