fly0744

  用java做简单的登陆都做的恶心了,以前的学校就是,但有网友问起,还是做了一个。

           登陆页面实现成功登陆案例

实现目标:登陆成功后进入成功页面,失败时进入失败页面(采用mvc模式,将控制与页面分离)

效果展示:

 

输入正确的用户名密码时:

 

用户名和密码错误时:

 

 

数据库表结构:

 

Jsp页面,主要由登陆页面(index.jsp)、操作页面(check.jsp)、成功页面(success。jsp)和失败页面(error.jsp)组成。

Index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<script type="text/javascript">

 function checkout(){

     

  var user = document.getElementById("user").Value;

 

  var password = document.getElementById("password").Value;

  if(user==""){

        alert("请输入用户名!!!");

        return false;

      }

  else if(password==""){

        alert("请输入密码!!");

        return false;

      }else {

         return true;

         }

     }

 

</script>

<body>

<form id="form1" action="check.jsp" method="post">

用户名:<input type="text" name="user" id="user"><br>

密码: <input type="password" name="password" id="password">

  <input type="submit" value="登陆" onclick="checkout()">

 </form>

 

</body>

</html>

 

 

Check.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

 

 

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<%

String user = request.getParameter("user");

String password = request.getParameter("password");

boolean flag = false;

if(user !=null && password !=null){

    Chc c = new Chc();

   flag =c.isExist(user,password);

}

if(flag==true){

    RequestDispatcher dispatcher = request.getRequestDispatcher("success.jsp");

    dispatcher.forward(request,response);

}else {

    RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");

    dispatcher.forward(request,response);

}

 

%>

<body>

 

</body>

 

<%@page import="com.dao.Chc"%></html>

 

Success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<h1>欢迎您的登陆!</h1>

</body>

</html>

 

Error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<h1>用户名和密码错误,请重新输入</h1>

</body>

</html>

 

Dao

package com.dao;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

 

import com.util.Conection;

 

public class Chc {

    Connection con = Conection.getConnection();

    public boolean isExist(String user,String password){

       boolean flag = false;

       String sql = "select * from t_user where user=? and password=?";

       try {

           PreparedStatement ps = con.prepareStatement(sql);

           ps.setString(1, user);

           ps.setString(2, password);

           ResultSet rs = ps.executeQuery();

           if(rs.next()){

              flag = true;

           }

          

       } catch (SQLException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

       return flag;

    }

 

}

 

 

Util jdbc 连接mysql数据库

package com.util;

 

import java.sql.Connection;

import java.sql.DriverManager;

 

public class Conection {

    public static Connection getConnection(){

       String dbClassName="com.mysql.jdbc.Driver";

       String dbUrl ="jdbc:mysql://localhost:3306/rubish?useUnicode=true&characterEncoding=utf8";

       String dbUser ="root";

       String dbPwd ="fly";

       Connection con = null;

     

    try{

       Class.forName(dbClassName);

       //getConnection(String url, String user, String password) ��ͼ����������ݿ� URL �����ӡ�

       con = DriverManager.getConnection(dbUrl, dbUser, dbPwd);

        }

    catch (Exception e) {

            e.printStackTrace();

        }

       return con;

    }

 

}

 

分类:

技术点:

相关文章:

猜你喜欢
相关资源
相似解决方案