Java教程

javaweb登陆界面实现不同角色进入不同界面

本文主要是介绍javaweb登陆界面实现不同角色进入不同界面,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录结构

  类包:

  • AccountBean.java
  • AccountDao.java
  • JudgeServlet.java

  登陆界面:

  • index.jsp

代码实现

AccountBean.java

package login;

public class AccountBean {
    String account;
    String password;
    String type;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

AccountDao.java

package login;

import dao.Con_CloseSql;
import java.sql.*;

public class AccountDao {

    public AccountBean checkAccount(String user, String password) throws Exception {
        Connection connect = new Con_CloseSql().getConnect();
        Statement stat = connect.createStatement();
        String sql = "select * from account where account = '" + user + "' and password = '" + password + "'";
        ResultSet rs = stat.executeQuery(sql);
        AccountBean accountBean = new AccountBean();
        if (rs.next()){

            accountBean.setAccount(rs.getString("account"));
            accountBean.setPassword(rs.getString("password"));
            accountBean.setType(rs.getString("type"));
        }
        Con_CloseSql.closeConnection(connect);
        return accountBean;
    }

}

JudgeServlet.java

package login;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class JudgeServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;utf-8");

        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String type = req.getParameter("type");


        AccountDao dao = new AccountDao();
        try {
            AccountBean bean = dao.checkAccount(account, password);
            String type1 = bean.getType();
            if(type1.equals(type))
            {
                if(type.equals("学生"))
                    req.getRequestDispatcher("stuhome.jsp").forward(req,resp);
                if(type.equals("教师"))
                    req.getRequestDispatcher("teahome.jsp").forward(req,resp);
                if(type.equals("管理员"))
                    req.getRequestDispatcher("Adminhome.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            resp.getWriter().print("<script>alert(\"账号或用户名出错\"); </script>");
        }
        
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form method="post" action="judge">
    账号:<input type="text" name="account"><br>
    密码:<input type="password" name="password"><br>
    用户类型:
    <select name="type">
        <option value="学生" name="xuesheng">学生</option>
        <option value="教师" name="jiaoshi">教师</option>
        <option value="管理员" name="guanliyuan">管理员</option>
    </select><br>
    <input type="submit" value="登陆">
</form>


</body>
</html>

数据库表结构为

 

 其中account与password是用户密码

type是该用户类型

如果全部匹配就可进入对应界面

这篇关于javaweb登陆界面实现不同角色进入不同界面的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!