zoukankan      html  css  js  c++  java
  • JSP第六次作业提交~~~~~~~~~~~~~~~~~~~~~

    1.安装MySQL数据库,建立用户表 uid uname upwd 并插入3条数据 (利用注册页面 创建的数据)

    2.制作jsp登录页面 login.jsp 提交到dologin.jsp,使用jdbc连数据库,判断输入的用户名密码是否存在

    package com.example.servlet;
    
    import com.example.Dao.ConnectionPool;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    import java.sql.*;
    
    public class Login extends HttpServlet {
        @Override
        public void init() throws ServletException {
            super.init();
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=UTF-8");
            String uusername = req.getParameter("username").trim();
            String ppassword = req.getParameter("password").trim();
            int code1Int= 0;
            int code2Int= 0;
            int yzmInt= 0;
            Connection connection=null;
            PreparedStatement pre=null;
            try {
                 connection = ConnectionPool.pop();
                String sql="SELECT * FROM USER WHERE user_name=? AND user_password=?;";
                 pre = connection.prepareStatement(sql);
                pre.setString(1,uusername);
                pre.setString(2,ppassword);
                ResultSet rs = pre.executeQuery();
                boolean m = rs.next();
                if (code1Int+code2Int==yzmInt){
                    try {
                        String yzm=req.getParameter("verify").trim();
                        String code1=req.getParameter("code1").trim();
                        String code2=req.getParameter("code2").trim();
                        code1Int = Integer.parseInt(code1);
                        code2Int = Integer.parseInt(code2);
                        yzmInt = Integer.parseInt(yzm);
                        if (code1Int+code2Int==yzmInt){
                            if (m==true){
                                HttpSession session = req.getSession();
                                session.setAttribute("uusername",uusername);
                                req.getRequestDispatcher("login_success.jsp").forward(req,resp);
                            }else {
                                resp.getWriter().println("<h1>登陆失败<h1>");
                                resp.getWriter().println("<h1>用户名或密码输入错误<h1>");
                                resp.getWriter().println("<h1>5s后页面跳转....</h1>");
                                resp.getWriter().println("<a href="login.jsp">返回至登陆界面</a>");
                                resp.setHeader("Refresh","5;url=login.jsp");
                            }
                        }else {
                            resp.getWriter().println("<h1>验证码输入错误<h1>");
                            resp.getWriter().println("<h1>3s后页面跳转....</h1>");
                            resp.getWriter().println("<a href="login.jsp">返回至登陆界面</a>");
                            resp.setHeader("Refresh","3;url=login.jsp");
                        }
                    } catch (NumberFormatException e) {
                        resp.getWriter().println("<h1>验证码为空,千万不可以为空哦<h1>");
                        resp.getWriter().println("<h1>3s后页面跳转....</h1>");
                        resp.getWriter().println("<a href="login.jsp">返回至登陆界面</a>");
                        resp.setHeader("Refresh","3;url=login.jsp");
                    }
                }
    
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                try {
                    if (pre!=null)
                    pre.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                ConnectionPool.push(connection);
            }
    
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doGet(req, resp);
        }
    
        public void success(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        }
    }

    3.如果存在,把用户名保存在SESSION中,跳转到welcome.jsp,welcome.jsp中读取session中的用户名,显示欢迎你xxx

    <%--
      Created by IntelliJ IDEA.
      User: 86177
      Date: 2021/3/30
      Time: 12:09
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>登陆成功页面</title>
    </head>
    <body>
    <%
        String login= (String) session.getAttribute("uusername");
        if (login==null){
            response.sendRedirect("login.jsp");
        }
    %>
     <h1>欢迎你 <%=login%></h1>
    </body>
    </html>

    4.若不存在,跳到登录页面。

    5.利用管理员 查看所有用户信息

    package com.example.servlet;
    
    import com.example.bean.User;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Admin extends HttpServlet {
        @Override
        public void init() throws ServletException {
            super.init();
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=UTF-8");
            String adminuser = req.getParameter("adminuser").trim();
            String adminpassword=req.getParameter("adminpassword").trim();
            if (adminuser.equals("axing")&&adminpassword.equals("11")){
                HttpSession session = req.getSession();
                session.setAttribute("adminuser",adminuser);
                req.getRequestDispatcher("guanli_success.jsp").forward(req,resp);
             }else {
    //            resp.getWriter().println("<h1>登陆失败<h1>");
                 req.getRequestDispatcher("guanli.jsp").forward(req,resp);
             }
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doGet(req, resp);
        }
    }

    注册页面

     登陆页面

     登陆成功

     

    登陆失败 不跳转~~~~~~~~~~~~~~~~~~~~~~~~~~~~  验证码输入错误 不跳转~~~~~~~~~~~~~~~~~~~~~

    利用管理员查看 用户的所有数据

     

  • 相关阅读:
    Linux-3.14.12内存管理笔记【构建内存管理框架(3)】
    Linux-3.14.12内存管理笔记【构建内存管理框架(2)】
    Linux-3.14.12内存管理笔记【构建内存管理框架(1)】
    Linux-3.14.12内存管理笔记【建立内核页表(3)
    Linux power supply class(1)_软件架构及API汇整【转】
    Linux-3.14.12内存管理笔记【建立内核页表(2)】-低端内存的建立
    Linux-3.14.12内存管理笔记【建立内核页表(1)】
    linux防火墙相关 iptables
    shell 备份 source code
    crontab清理日志
  • 原文地址:https://www.cnblogs.com/skyfail/p/14631258.html
Copyright © 2011-2022 走看看