ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-11-26 23:31:07  阅读:247  来源: 互联网

标签:account 界面 javaweb 不同 req password type public String


目录结构

  类包:

  • 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是该用户类型

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

标签:account,界面,javaweb,不同,req,password,type,public,String
来源: https://www.cnblogs.com/java-six/p/15610068.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有