登陆界面:newinput.jsp 我点这个图片验证码也不会更新!! <html> <head> <title>验证码</title> <script type="text/javascript"> $(function(){ //给验证码的图片,绑定单击事件 $("#code_img").click(function(){ alert(this.src); //this.src = "${basePath}/kaptcha.jpg?d=" + new Date(); }); }); </script> </head> <body > <FORM action="http://localhost:8080/tmp_war_exploded/newInputServlet" method=get name=form> <P>请输入下列信息:<br/> 学号:<input type="text" name="ID" value="123456"><br/> 姓名:<input type="text" name="username" value="liu"><br/> 专业:<input type="radio" name="major" value="计算机"/>计算机 <input type="radio" name="major" value="软件工程"/>软件工程 <input type="radio" name="major" value="网络工程"/>网络工程<br/> 验证码:<input type="text" style= "width: 60px;" name= "code"> <img id="code_img" src="kaptcha.jpg" alt="" style= "width: 100px; height: 28px;"><br/> <input type="submit" value="登录" name="logIn"> </FORM> </body> </html>
验证码servlet:
public class NewInputServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取Session中的验证码 String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY); //删除Session验证码 req.getSession().removeAttribute(KAPTCHA_SESSION_KEY); String code = req.getParameter("code"); //获取用户名 //String username = req.getParameter("username"); if (token != null && token.equalsIgnoreCase(code)) { //System.out.println("该生的姓名为:"+username); resp.sendRedirect(req.getContextPath()+"/ok.jsp"); }else{ resp.sendRedirect(req.getContextPath()+"/fault.jsp"); } } }
登录成功后显示:
<body > <a href="http://localhost:8080/tmp_war_exploded/newinput.jsp">返回登录界面</a> <% request.setCharacterEncoding("utf-8"); String yourID=request.getParameter("ID"); String yourName=request.getParameter("username"); String yourMajor=request.getParameter("major"); out.println("<P> 您的姓名:"+yourName+"</P>"); out.println("<P> 您的学号:"+yourID+"</P>"); out.println("<P> 您的专业:"+yourMajor+"</P>"); %> </body>