动手动脑——登录界面

1. 需要网站系统开发需要掌握的技术;

Java语言(Java database Connectivity(JjavawebDbc)技术、Servlet技术、jsp(Java Server Pages)技术,JavaBean(Application)应用组件技术)、面向对象分析设计思想、设计模式和框架结构、XML语言、网页脚本语言、开发工具(数据库、web服务器、集成开发环境(IDE))

2. 本次课堂测试的源程序代码;

User.java

package com.login.msg.model;

public class User {

    private String name;
    private String password;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

}
package com.login.msg.dao;

import java.util.List;

import com.login.msg.model.User;

public interface IUserDao {

    public User login(String username);
    public List<User> load();
}
package com.login.msg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.login.msg.Util.DBUtil;
import com.login.msg.Util.UserException;
import com.login.msg.model.User;
import com.login.msg.Util.DBUtil;
public class UserDaoImpl implements IUserDao{
 //登录
    @Override
    public User login(String username) {
        // TODO Auto-generated method stub
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from t_user  where username ="+"'"+username+"'";//定义一个查询语句
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                if (resultSet.getInt(username) < 0) {
                    throw new UserException("用户名或者密码失败") ;
                }
                
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  user;
    }
    public List<User> load() {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from t_user ";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //集合中只能放入user对象
        List<User> users = new ArrayList<User>();
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                user = new User();
                user.setName(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                users.add(user);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  users;
    }

}
package com.login.msg.Util;

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

public class DBUtil {
    public static Connection getConnection(){
        //1 加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/login_msg";
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
    //关闭资源的方法
        public static void close(Connection connection ) {
            try {
                if (connection != null) {
                    connection.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(PreparedStatement preparedStatement ) {
            try {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void close(ResultSet resultSet ) {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
}
package com.login.msg.Util;

public class UserException extends RuntimeException{

    public UserException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public UserException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public UserException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
    
}
<%@page import="com.login.msg.Util.UserException"%>
<%@page import="com.login.msg.dao.UserDaoImpl"%>
<%@page import="com.login.msg.model.User"%>
<%@ 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>
<%
    //接收客户端传递过来的参数
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if(username == null || "".equals(username.trim())){
        request.setAttribute("error", "用户名不能为空");
    
%>
    <jsp:forward page="loginInput.jsp"></jsp:forward>
<%
    }
    User user = new User();
    user.setName(username);
    user.setPassword(password);
    UserDaoImpl userDao = new UserDaoImpl();
    try{
        
    userDao.login(username);
    //重定向
    response.sendRedirect("list.jsp");
%>

    
    
<%
    }catch(UserException e){
%>
    <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
    <%
    }
    %>
</html>
<%@ 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>
    <title>登录页面</title>
</head>
<body>
    <%=request.getAttribute("error") %>
    <form action="login.jsp" method="get">
        <table align="center" border="1" width="500">
            <tr>
                <td>用户名称 : </td>
                <td>
                    <input type="text" name="username" />
                </td>
            </tr>
                <tr>
                <td>用户密码:</td>
                <td>
                    <input type="password" name="password" />
                </td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="登录" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>
<%@page import="com.login.msg.model.User"%>
<%@page import="java.util.List"%>
<%@page import="com.login.msg.dao.UserDaoImpl"%>
<%@ 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=UTF-8">
<title>用户展示界面</title>
</head>
<%
    UserDaoImpl userDao = new UserDaoImpl();
    List<User> users = userDao.load();
%>
<body>
    <table align="center" border="1" width="500">
        <tr>
            <td>用户名称</td>
            <td>用户密码</td>
        </tr>
        <%
            for( User user : users ){
        %>
        <tr>
            <td> <%=user.getName() %></td>
            <td> <%=user.getPassword() %></td>
            <td> <a href="delete.jsp ? id="<%=user.getName() %>" >删除</a></td>
        </tr>
        <%
            }
        %>
    </table>
</body>
</html>

3. 运行结果截图;

4. 列出你对这门课的希望和自己的目标,并具体列出你计划每周花多少时间在这门课上。

能在八周之后单独完成一个简单的web应用;

每周14个小时

 

posted @ 2017-11-22 21:56  wxdbk  阅读(139)  评论(0编辑  收藏  举报