1. 需要掌握的技术;
Java语言(Java database Connectivity技术、Servlet技术、jsp(Java Server Pages)技术,JavaBean(Application)应用组件技术)、面向对象分析设计思想、设计模式和框架结构、XML语言、网页脚本语言、开发工具(数据库、web服务器、集成开发环境(IDE))
源代码:
1.DBUtil
package com.jaovo.msg.util; import java.sql.*; public class DBUtil { public static Connection getConnection() { try { //1 加载驱动 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/jaovo_msg"; Connection connection = null; try { //2 创建链接对象connection 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(); } } }
2.add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import = "com.jaovo.msg.util.DBUtil" %> <%@ page import = "java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <% //接受客户端传递过来的参数 String username = request.getParameter("username"); if(username==null||"".equals(username)) { request.setAttribute("哎呀", "请输入用户名!"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } String password = request.getParameter("password"); String sql="select * from t_user where username=?"; Connection connection=DBUtil.getConnection(); PreparedStatement preparedStatement=connection.prepareStatement(sql); preparedStatement.setString(1, username); ResultSet resultset=preparedStatement.executeQuery(); boolean flag=false; while(resultset.next()) { flag=true; if(resultset.getString("password").equals(password)) { request.setAttribute("哎呀", "登陆成功,你是真的厉害!"); %> <%=request.getAttribute("哎呀")%> <% } else { request.setAttribute("哎呀", "登录失败,你好low!"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } } if(!flag) { request.setAttribute("哎呀","用户不存在!"); %> <jsp:forward page="addInput.jsp"></jsp:forward> <% } %> <body> </body> </html>
3.addInput.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>
<title>用户登录界面</title>
</head>
<body>
<form action="add.jsp" method="get">
<%=request.getAttribute("哎呀") %>
<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="提交" />
<input type="reset" value="重置" />
</td>
</tr>
</table>
</form>>
</body>
</html>