首页 > 编程知识 正文

登陆页面,css实现漂亮的用户登录界面

时间:2023-05-05 14:07:27 阅读:141431 作者:112

1.在main路径下创建一个Java总类,并标记为根路径(这样才能在Java路径下创建包)

2.创建一些需要用到的类

创建如下类并写入代码

登录界面代码块(LoginController)

package example2. project2.com troller; import javax.servlet.servlet context; import javax.servlet.servlet exception; import javax.servlet.annotation.web servlet; import javax.servlet.http.http servlet; import javax.servlet.http.http servlet request; import javax.servlet.http.http无servlet轮询; import java.io.IOException; import java.io.PrintWriter; /**用户注册* @author失眠之夜* @data 2020/4/6 10:00上午*/@webservlet((/login ) ) )。 publicclasslogincontrollerextendshttp servlet { @ overrideprotectedvoiddoget (httpservletresponseresp ) 欢迎使用stringbuilder.append (' fontsize=' 20 ' XXX管理系统/font ' ); stringbuilder.append (' formaction=' ' method=' post ' ); stringBuilder.append (登录名: input type=' text ' name=' loginname ' value='/br ' ); stringBuilder.append ('登录密码: input type=' password ' name=' logi npwd ' value='/br ' ); stringBuilder.append (认证码: input type=' text ' name=' valid code '/img src='/captcha ' width=100 height=40/stringbuilder.append(/form ); showmsg(resp,stringBuilder.toString ); } @ overrideprotectedvoiddopost (httpservletrequestreq,HttpServletResponse resp ) throws ServletException,io exception { stred } stringvalidcode=req.getparameter (' valid code ); servletcontextapplication=this.getservletcontext (; application.setattribute('c ',1 ); stringsavecode=(string ) req.getSession ) ).getattribute ) ' code ); //如果您的登录名是您的学号,密码是1234,请执行以下操作: 否则,将显示帐户或密码错误,String msg=null; if (保存代码!=null(if ) savecode.equals (valid code ) ) if('00'.equals(loginname ) (1234'.equals ) loginpwd ) ) msg loging }else{

msg="<font size= '20'>账号或密码错误!</font>"; } }else{ msg="验证码错误"; } }else{ msg="请输入验证码"; } showMsg(resp,msg); } private void showMsg(HttpServletResponse resp,String msg){ resp.setCharacterEncoding("utf-8"); try{ PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<header>"); out.println("<title>test</title>"); out.println("<meta charset='utf-8'>"); out.println("</header>"); out.println("<body>"); out.println(msg); out.println("</body>"); out.println("</html>"); out.close(); }catch(Exception ex){ System.out.println(ex.getMessage()); } }}

验证码代码块(CaptchaController)

package example2.Project2.comtroller;import Util.StrUtil;import javafx.util.Builder;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.awt.*;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;/** * 随机生成验证码 * @authur 失眠的黑夜 * @data 2020/3/31 10:00 上午 */@WebServlet("/captcha")public class CaptchaController extends HttpServlet { private static int WIDTH =100; private static int HEIGHT =80; @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage. TYPE_INT_RGB); Graphics g = img.getGraphics(); Graphics2D G = (Graphics2D)g; g. setColor(Color.white);//验证码背景颜色 g.fillRect( 0, 0, WIDTH, HEIGHT); randomLine(80,G);//干扰线 randomDrop(80,G);//干扰点 g. setFont(new Font( "宋体" , Font.BOLD , 40)); G. setColor(getColor(0)); String code = StrUtil.randomString(4);//随机生成4位验证码 HttpSession session=req.getSession(); session.setAttribute("code",code); g.drawString( code, 10, 35); g. dispose(); //图片输出 ServletOutputStream out=resp.getOutputStream(); ImageIO.write(img,"jpg",out); try { out. flush(); }catch (Exception ex){} finally { out.close(); } } //随机干扰点 public static void randomDrop(int count, Graphics2D g) { for (int i = 0; i < count; i++) { Random rd = new Random(); int x = rd.nextInt(WIDTH);//产生小于WIDTH的随机整数 int y = rd.nextInt(HEIGHT);//产生小于HEIGHT的随机整数 g.setColor(getColor(0));//随机颜色 g.drawLine(x, y, x + 2, y + 2);//绘制超短的线,看着是个点 } } //随机干扰线 public static void randomLine(int count, Graphics2D g) { for (int i = 0; i < count; i++) { Random rd = new Random(); int x1 = rd.nextInt(WIDTH); //产生WIDTH的随机数值,起点 int x2 = rd.nextInt( x1+ 30); //终端点 int y1 = rd.nextInt(HEIGHT); //产生HEIGTH的随机数值,起点 int y2 = rd.nextInt(y1 + 30); //终端点 g.setColor(getColor(0)); g.drawLine(x1, y1, x2, y2); //绘制线条 } } //随机颜色 public static Color getColor(int a) { Random rd = new Random(); //生成0——1的随机数 //设置颜色参数 int r = rd.nextInt(256 - a); return new Color(r); }}

StrUtil工具类(随机生成4个字符)

package Util;import java.awt.*;import java.util.Random;/** * 随机生成多个字符的字符串 * @return 生成的字符串 */public class StrUtil { public static String randomString(int count){ //count :字符串中字符的个数 StringBuilder builder = new StringBuilder(); //要生成的字符模板 String source ="qwertyuipasdfghjklxcvbnm0123456789"; //随机验证码 Random rnd=new Random(); for(int i=0;i<count;i++) { int pos = rnd.nextInt(source.length()); String s = source.substring(pos, pos + 1); builder.append(s); } return builder.toString(); //转换成字符 }}

运行结果

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。