2020.12.14

一、今日学习内容

     今天完成了登录界面的代码

package Dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import DBUtil.DBUtil;
import entity.Login;
import entity.User;

public class LoginDao {
    public boolean loginadd(Login login) {
        String sql = "insert into login(IDuser,password,flag) values('" + login.getIDuser() + "','" + login.getPassword() + "','" 
                     + login.getFlag() + "')";
        //创建数据库链接
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;

        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
    
    public boolean useradd(User user) {
        String sql = "insert into user(username,IDuser,password,userclass,usersex) values('" + user.getUsername()+"','"+user.getIDuser() + "','" + user.getPassword() + "','" 
                     + user.getUserclass() +"','"+user.getUsersex()+ "')";
        //创建数据库链接
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;

        try {
            state = conn.createStatement();
            state.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭连接
            DBUtil.close(state, conn);
        }

        if (a > 0) {
            f = true;
        }
        return f;
    }
package Servlet;
import java.util.Date; 
import java.text.SimpleDateFormat; 

import java.io.IOException;
import java.util.List;

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 entity.Login;
import entity.User;
import Service.LoginService;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet{
    
    private static final long serialVersionUID = 1L;
    
    LoginService service=new LoginService();
    
     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            String method = req.getParameter("method");

            if ("loginadd".equals(method)) {
                loginadd(req, resp);
            } else if("useradd".equals(method)) {
                useradd(req,resp);
            }else if("chaxun".equals(method)) {
                chaxun(req,resp);
            }else if("liulan".equals(method)) {
                liulan(req,resp);
            }else if("delete2".equals(method)) {
                delete2(req,resp);
            }else if("getByname".equals(method)) {
                getByname(req,resp);
            }else if("getByname1".equals(method)) {
                getByname1(req,resp);
            }else if("xiugai".equals(method)) {
                xiugai(req,resp);
            }
     }
     
     private void loginadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
            req.setCharacterEncoding("utf-8");
            //获取数据
            String IDuser=req.getParameter("IDuser");
            String password=req.getParameter("password");
            String flag = req.getParameter("flag");
            Login login=new Login(IDuser,password,flag);
            
            service.loginadd(login);
            if(flag.equals("管理员")&&password.equals("123456")) {
                    req.setAttribute("message", "登录成功");
                    req.getRequestDispatcher("manager.jsp").forward(req,resp);
            }else if(flag.equals("管理员")&&!password.equals("123456")) {
                req.setAttribute("message", "密码错误,登录失败");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }else if(flag.equals("读者")) {
                if(service.loginjudge(IDuser, password)) {
                    req.setAttribute("message", "登录成功");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }else {
                    req.setAttribute("message", "用户名或密码错误,登录失败");
                    req.getRequestDispatcher("login.jsp").forward(req,resp);
                }
            }
            
        }
     
     private void useradd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
             req.setCharacterEncoding("utf-8");
            //获取数据
             String username=req.getParameter("username");
            String IDuser=req.getParameter("IDuser");
            String password=req.getParameter("password");
            String userclass=req.getParameter("userclass");
            String usersex=req.getParameter("usersex");
            User user=new User(username,IDuser,password,userclass,usersex);
            
            service.useradd(user);
            req.setAttribute("message", "录入成功");
            req.getRequestDispatcher("mareaadd.jsp").forward(req,resp);
     }
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
     <h1>登录页面</h1>
     <div class="content">
        <div class="main">
        <br/><br/>
        <form name="form" action="LoginServlet?method=loginadd" method="post" ">
               用户名 <input type="text" name="IDuser" placeholder="八位数字组成" /><br/><br/>
                 密&nbsp;&nbsp;码<input type="password" name="password"/><br/><br/>
                身&nbsp;&nbsp;份
                        <select name="flag" class="shortselect">
                            <option value="读者">读者</option>
                            <option value="管理员">管理员</option>
                    </select><br/><br/>
            <input type="submit" value="登录" />
        </form>
        </div>
    </div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title> </title>

</head>
<body>
    <%
          Object message = request.getAttribute("message");
          if(message!=null && !"".equals(message)){
      
     %>
          <script type="text/javascript">
               alert("<%=request.getAttribute("message")%>");
          </script>
     <%} %>
     <div align="center">
        <h1 style="color: black;">管理员功能页面</h1>
        <a href="login.jsp">返回登录页面</a>

        <div class="a">
            <a href="mareaadd.jsp">添加读者信息</a>
        </div>
        <div class="a">
            <a href="delete2.jsp">删除读者信息</a>
        </div>
        <div class="a">
            <a href="LoginServlet?method=liulan">浏览读者信息</a>
        </div>
        <div class="a">
            <a href="chaxun.jsp">查询读者信息</a>
        </div>
         <div class="a">
            <a href="xiugai.jsp">修改读者信息</a>
        </div>
    </div>
</body>
</html>

二、遇到的问题

    今天没有遇到什么困难

三、明日计划

   明天继续代码

posted @ 2020-12-14 21:36  小仙女W  阅读(93)  评论(0编辑  收藏  举报