Form表单简单用户登录

1.jsp页面  

<div id="loginFormDiv">
    <div id="loginHeader"><p>欢&ensp;迎&ensp;您&ensp;!</p></div>
      <form action="login" method="post" id="loginForm">
        <input type="text" name="userName" id="userName" placeholder="请输入用户名"/>
        <input type="password" name="passWord" id="passWord" placeholder="请输入密码"/><br/>
        <input type="button" value="登录" οnclick="login()"/>
      </form>
    </div>

script中提交form

function login() {

  var loginFormObj = document.getElementById("loginForm");
        loginFormObj.submit();
 }

2.servlet中接收数据

request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord");

3.连接数据库

//1.加载驱动

try {
    Class.forName("com.mysql.jdbc.Driver");
     //2.得到connection对象
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "root", "123456");
    //3.得到PreparedStatement对象
    stmt = conn.prepareStatement("SELECT * FROM user WHERE user_name = ?");
    stmt.setString(1, userName);
    //4.得到数据并处理
    rs = stmt.executeQuery();
    if (rs.next()) {
        int userId = rs.getInt("Id");
        String userNameDB = rs.getString("user_name");
        String passWordDB = rs.getString("password");

    }

} catch (ClassNotFoundException e) {
              e.printStackTrace();
} catch (SQLException e) {
              e.printStackTrace();
} finally {  //注意打开连接数据库,最后要关闭连接
     try {
         if (rs != null) {
              rs.close();
         }
          if (rs != null) {
              stmt.close();
          }
         if (rs != null) {
              conn.close();
         }
} catch (SQLException e) {
       e.printStackTrace();
}

4.跳转页面

request.getRequestDispatcher().forward(request, response);

response.sendRedirect()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值