问答详情
源自:2-3 验证码的校验

一直错误问题 什么原因额

package com.ljdy;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  response.setContentType("text/html;charset=utf8");  //request.getSession().setAttribute("name", str);  //对name对象赋值  //request.getSession().getAttribute("name")  //取得name对象的值  String piccode=(String)request.getSession().getAttribute("piccode");  //获取用户输入的验证码  String checkcode=request.getParameter("checkcode");  //不区分大小写  checkcode=checkcode.toUpperCase();  //创建一个写入对象  PrintWriter out = response.getWriter();  if(checkcode.equals(piccode)){   out.println("验证码输入正确");  }else{   out.println("验证码输入有误,请重新输出");  }  out.flush();  out.close(); }      }

<%@ 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>My JSP 'index.jsp' starting page</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(){      document.getElementById("imagecode").src='<%=request.getContextPath()%>/servlet/ImageServlet?ts='+new Date().getTime();     } --%>           function reloadCode(){       var time=new Date().getTime();     // ts是请求过去的参数 随便定义      document.getElementById("imagecode").src="<%=request.getContextPath()%>/servlet/ImageServlet?d="+time;     };  </script>  </head>    <body>  <div style="text-align: center;">    <form action="<%=request.getContextPath() %>/servlet/LoginServlet"  method="get">       验证码:<input type="text" name="checkcode"/>      <img alt="验证码" id="imagecode" src="<%=request.getContextPath() %>/servlet/ImageServlet"/>      <a href="javascript: reloadCode();">看不清楚</a><br>      <input type="submit" value="提交">      </form></div>  </body></html>

package com.ljdy;import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ImageServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {     //创建图片映像 白纸    BufferedImage bi=new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);     //获得画笔     Graphics g = bi.getGraphics();//    Color color = new Color(200,151,255);//颜色生成//     g.setColor(color);     g.setColor(Color.gray);     //设验证码背景     g.fillRect(0, 0, 68, 22);     //toCharArray将字符串转换为字符数组,获得一个含有字母和数字的数组,后续随机从中获取字符     char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789".toCharArray();     Random r=new Random();        int len=ch.length,index;   //     int len =ch.length;//     int index;     StringBuffer s=new StringBuffer();      for(int i=0;i<4;i++){      index=r.nextInt(len); // 将产生的随机字母和数字放入index中      g.setColor(new Color(r.nextInt(88),r.nextInt(120),r.nextInt(90)));  //给每个验证码创建随机颜色      g.drawString(ch[index]+"", (i)*15+3, 18);    //将获得的字符写在图片并设定位置      s.append(ch[index]);   //将随机获取的字符放入缓冲区中 保存图片     }     request.getSession().setAttribute("newcode",s.toString());  //存至session中,以便后续验证     ImageIO.write(bi, "JPG", response.getOutputStream());      //通过Image输出图片,并传入前台 } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {   doGet(request, response); }    }


提问者:我可不是一只圆滚滚的大熊猫 2019-03-07 12:37

个回答

  • 慕数据1456139
    2019-04-11 10:23:47

    报的错误是啥?