01-实现简单的登录界面
1.需要掌握的技术有:Java语言(Java database Connectivity技术、Servlet技术、jsp(Java Server Pages)技术,JavaBean(Application)应用组件技术)、面向对象分析设计思想、设计模式和框架结构、XML语言、网页脚本语言、开发工具(数据库、web服务器、集成开发环境(IDE))。
2.源代码:
1 package com.jaovo.msg.Util; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 public class DBUtil { 7 public static Connection getConnection() { 8 try { 9 Class.forName("com.mysql.jdbc.Driver").newInstance(); 10 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { 11 // TODO Auto-generated catch block 12 e.printStackTrace(); 13 } 14 String user = "root"; 15 String password = "root"; 16 String url = "jdbc:mysql://localhost:3306/jaovo_msg"; 17 Connection connection = null; 18 try { 19 connection = DriverManager.getConnection(url, user, password); 20 } catch (SQLException e) { 21 // TODO Auto-generated catch block 22 e.printStackTrace(); 23 } 24 return connection; 25 } 26 }
1 <%@page import = "com.jaovo.msg.Util.DBUtil" %> 2 <%@page import = "java.sql.*" %> 3 <%@page import = "java.sql.PreparedStatement" %> 4 <%@ page language="java" contentType="text/html; charset=UTF-8" 5 pageEncoding="UTF-8"%> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <% 9 String username = request.getParameter("username"); 10 String password = request.getParameter("password"); 11 if(username == null || "".equals(username.trim())){ 12 request.setAttribute("result", "账户不能为空!"); 13 %> 14 <jsp:forward page = "addInput.jsp"></jsp:forward> 15 <% 16 } 17 if(password == null || "".equals(password.trim())){ 18 request.setAttribute("result", "密码不能为空!"); 19 %> 20 <jsp:forward page = "addInput.jsp"></jsp:forward> 21 <% 22 } 23 Connection connection = DBUtil.getConnection(); 24 boolean flag = false; 25 String sql = "select * from user where username = ?"; 26 PreparedStatement preparedstatement = null; 27 ResultSet resultset = null; 28 preparedstatement = connection.prepareStatement(sql); 29 preparedstatement.setString(1,username); 30 resultset = preparedstatement.executeQuery(); 31 while(resultset.next()){ 32 if(resultset.getString("password").equals(password)){ 33 flag = true; 34 request.setAttribute("result", "登陆成功!"); 35 %> 36 <%=request.getAttribute("result")%> 37 <% 38 } 39 else{ 40 request.setAttribute("result", "密码错误!请重新输入!"); 41 %> 42 <jsp:forward page = "addInput.jsp"></jsp:forward> 43 <% 44 45 } 46 } 47 if(!flag){ 48 request.setAttribute("result", "该账户不存在!"); 49 %> 50 <jsp:forward page = "addInput.jsp"></jsp:forward> 51 <% 52 } 53 %> 54 </html>