javaweb-实现不同用户登录后跳转到不同界面

今天下午,实现了公文流转系统的一小部分——登录界面验证不同用户跳转到不同界面

Dao

package com.office.Dao;

import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.office.DBUtil.DBUtil;

public class Dao {
    public String dopost(String username,String password) {
        String i="-1";
        String sql="select * from user where username = '"+username+"'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
       while(rs.next()){ String password1
= rs.getString("password");   if(password.equals(password1)) {    i=rs.getString("id");    }
        break;
       }   }
catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs,state, conn); } return i; } }

Servlet

package com.office.Servlet;

import java.io.IOException;
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 com.office.Dao.Dao;




@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    
    public Servlet() {
        super();
        
    }
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if ("dopost".equals(method)) {
            dopost(req, resp);
        } 
        
    }//end service 
    
    private void dopost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        Dao dao=new Dao();
        String id=dao.dopost(username, password);
        if(id.equals("-1")) {
            req.setAttribute("message", "登录失败!");
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }
        else if(id.equals("0")) {
            req.setAttribute("message", "登陆成功!");
            req.getRequestDispatcher("root.jsp").forward(req,resp);
        }
        else if(id.equals("1")) {
            req.setAttribute("message", "登陆成功!");
            req.getRequestDispatcher("user.jsp").forward(req,resp);
        }
        
    }
    
}
    

User

package com.office.Entity;

public class User {
    private String username;
    private String password;
    private String id;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}

DBUtil

package com.office.DBUtil;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class DBUtil {
    
    public static String db_url = "jdbc:mysql://localhost:3306/luizhuan";
    public static String db_user = "root";
    public static String db_pass = "wyp6666";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(db_url, db_user, db_pass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }//end getConn
    
    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql ="select * from user";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        if(rs.next()){
            System.out.println("连接成功");
        }else{
            System.out.println("连接失败");
        }
    }
}

index.jsp

<%@ 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")%>");
              var  asd=request.getAttribute("username");         
    </script>
    <%
        }
    %>
    <form action="Servlet?method=dopost" method="post">
        <div>
            用户名<input type="text" name="username"  />
        </div>
        <div>
            密码<input type="password" name="userpassword" />
        </div>
        <div>
            <input type="submit" value="登录" />
        </div>
    </form>
</body>
</html>
posted @ 2019-12-09 17:24  dululudululu  阅读(8182)  评论(0编辑  收藏  举报