JavaWeb学习-1
一,Java Web项目需要掌握的技术
·java语言
-| JDBC
-| Servlet
-| JSP
-| JavaBean
·面向对象分析设计思想
·设计模式和框架结构
·XML语言
·网页脚本语言
·开发工具
-|数据库
-|应用服务器
-|集成开发环境(IDE)
二.源代码:
1 package dao; 2 3 import java.util.List; 4 5 import model.*; 6 public interface IUserDao 7 { 8 public void add(User user); 9 public void delete(String username); 10 public void update(User user); 11 public User load(int id); 12 public User load(String username); 13 public List<User> load(); 14 }
1 package dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import Util.*; 11 import model.*; 12 13 public class UserDaoImpl implements IUserDao 14 { 15 @Override 16 public void add(User user) 17 { 18 Connection connection=Util.getConnection(); 19 String sql="select count(*) from t_user where username=?"; 20 PreparedStatement preparedStatement=null; 21 ResultSet resultSet=null; 22 try { 23 preparedStatement=connection.prepareStatement(sql); 24 preparedStatement.setString(1, user.getUsername()); 25 resultSet=preparedStatement.executeQuery(); 26 while(resultSet.next()) 27 { 28 if(resultSet.getInt(1)>0) 29 { 30 throw new UserException("用户已存在!"); 31 } 32 } 33 } catch (SQLException e) { 34 // TODO Auto-generated catch block 35 e.printStackTrace(); 36 } 37 sql="insert into t_user(username,password) value(?,?)"; 38 try { 39 preparedStatement=connection.prepareStatement(sql); 40 preparedStatement.setString(1, user.getUsername()); 41 preparedStatement.setString(2, user.getPassword()); 42 preparedStatement.executeUpdate(); 43 } catch (SQLException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 }finally 47 { 48 Util.close(connection); 49 Util.close(preparedStatement); 50 Util.close(resultSet); 51 } 52 } 53 54 @Override 55 public void delete(String username) { 56 Connection connection=Util.getConnection(); 57 String sql="delete from t_user where username = ?"; 58 PreparedStatement preparedStatement=null; 59 try { 60 preparedStatement=connection.prepareStatement(sql); 61 preparedStatement.setString(1, username); 62 preparedStatement.executeUpdate(); 63 } catch (SQLException e) { 64 // TODO Auto-generated catch block 65 e.printStackTrace(); 66 }finally 67 { 68 Util.close(connection); 69 Util.close(preparedStatement); 70 } 71 } 72 73 @Override 74 public void update(User user) { 75 Connection connection=Util.getConnection(); 76 String sql="update t_user set password = ? where username = ?"; 77 PreparedStatement preparedStatement=null; 78 try { 79 preparedStatement=connection.prepareStatement(sql); 80 preparedStatement.setString(1, user.getPassword()); 81 preparedStatement.setString(2, user.getUsername()); 82 preparedStatement.executeUpdate(); 83 } catch (SQLException e) { 84 // TODO Auto-generated catch block 85 e.printStackTrace(); 86 }finally 87 { 88 Util.close(connection); 89 Util.close(preparedStatement); 90 } 91 } 92 93 @Override 94 public User load(int id) 95 { 96 Connection connection=Util.getConnection(); 97 String sql="select * from t_user where id=?"; 98 User user=null; 99 PreparedStatement preparedStatement=null; 100 ResultSet resultSet=null; 101 try { 102 preparedStatement=connection.prepareStatement(sql); 103 preparedStatement.setInt(1, id); 104 resultSet=preparedStatement.executeQuery(); 105 while(resultSet.next()) 106 { 107 user=new User(); 108 user.setId(id); 109 user.setUsername(resultSet.getString("username")); 110 user.setPassword(resultSet.getString("password")); 111 } 112 } catch (SQLException e) { 113 // TODO Auto-generated catch block 114 e.printStackTrace(); 115 }finally 116 { 117 Util.close(connection); 118 Util.close(preparedStatement); 119 Util.close(resultSet); 120 } 121 return user; 122 } 123 124 @Override 125 public User load(String username) 126 { 127 Connection connection=Util.getConnection(); 128 String sql="select * from t_user where username=?"; 129 User user=null; 130 PreparedStatement preparedStatement=null; 131 ResultSet resultSet=null; 132 try { 133 preparedStatement=connection.prepareStatement(sql); 134 preparedStatement.setString(1, username); 135 resultSet=preparedStatement.executeQuery(); 136 while(resultSet.next()) 137 { 138 user=new User(); 139 user.setId(resultSet.getInt("id")); 140 user.setUsername(username); 141 user.setPassword(resultSet.getString("password")); 142 } 143 } catch (SQLException e) { 144 // TODO Auto-generated catch block 145 e.printStackTrace(); 146 }finally 147 { 148 Util.close(connection); 149 Util.close(preparedStatement); 150 Util.close(resultSet); 151 } 152 return user; 153 } 154 155 @Override 156 public List<User> load() 157 { 158 Connection connection=Util.getConnection(); 159 String sql="select * from t_user "; 160 PreparedStatement preparedStatement=null; 161 ResultSet resultSet=null; 162 List<User> users=new ArrayList<User>(); 163 User user=null; 164 try 165 { 166 preparedStatement=connection.prepareStatement(sql); 167 resultSet=preparedStatement.executeQuery(); 168 while(resultSet.next()) 169 { 170 user=new User(); 171 user.setId(resultSet.getInt("id")); 172 user.setUsername(resultSet.getString("username")); 173 user.setPassword(resultSet.getString("password")); 174 users.add(user); 175 } 176 }catch (SQLException e) { 177 // TODO Auto-generated catch block 178 e.printStackTrace(); 179 }finally 180 { 181 Util.close(connection); 182 Util.close(preparedStatement); 183 Util.close(resultSet); 184 } 185 return users; 186 } 187 }
1 package model; 2 3 public class User 4 { 5 private int id; 6 private String username; 7 private String password; 8 public int getId() { 9 return id; 10 } 11 public void setId(int id) { 12 this.id = id; 13 } 14 public String getUsername() { 15 return username; 16 } 17 public void setUsername(String username) { 18 this.username = username; 19 } 20 public String getPassword() { 21 return password; 22 } 23 public void setPassword(String password) { 24 this.password = password; 25 } 26 27 }
1 package model; 2 3 public class UserException extends RuntimeException{ 4 5 /** 6 * 7 */ 8 private static final long serialVersionUID = 1L; 9 10 public UserException() { 11 super(); 12 // TODO Auto-generated constructor stub 13 } 14 15 public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { 16 super(message, cause, enableSuppression, writableStackTrace); 17 // TODO Auto-generated constructor stub 18 } 19 20 public UserException(String message, Throwable cause) { 21 super(message, cause); 22 // TODO Auto-generated constructor stub 23 } 24 25 public UserException(String message) { 26 super(message); 27 // TODO Auto-generated constructor stub 28 } 29 30 public UserException(Throwable cause) { 31 super(cause); 32 // TODO Auto-generated constructor stub 33 } 34 35 36 }
1 <%@page import="dao.UserDaoImpl"%> 2 <%@page import="model.*"%> 3 <%@page import="Util.*"%> 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 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <% 10 String username=request.getParameter("username"); 11 String password=request.getParameter("password"); 12 String repassword=request.getParameter("repassword"); 13 if(username==null||"".equals(username.trim())) 14 { 15 request.setAttribute("error","用户名不能为空!"); 16 %> 17 <jsp:forward page="addInput.jsp"></jsp:forward> 18 <% 19 } 20 if(password==null||"".equals(password.trim())) 21 { 22 request.setAttribute("error","密码不能为空!"); 23 %> 24 <jsp:forward page="addInput.jsp"></jsp:forward> 25 <% 26 } 27 if(repassword==null||"".equals(repassword.trim())) 28 { 29 request.setAttribute("error","再次输入密码以示确认!"); 30 %> 31 <jsp:forward page="addInput.jsp"></jsp:forward> 32 <% 33 } 34 if(!password.trim().equals(repassword.trim())) 35 { 36 request.setAttribute("error","两次输入的密码不一致。请确认后再进行操作!"); 37 %> 38 <jsp:forward page="addInput.jsp"></jsp:forward> 39 <% 40 } 41 User user=new User(); 42 user.setUsername(username); 43 user.setPassword(password); 44 UserDaoImpl userDao=new UserDaoImpl(); 45 try 46 { 47 userDao.add(user); 48 %> 49 <h2 style="color:blue">用户保存成功!</h2> 50 <% 51 }catch(UserException e) 52 { 53 %> 54 <h2 style="color:red">error:<%=e.getMessage() %></h2> 55 <% 56 } 57 %> 58 </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> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户注册</title> </head> <body> <form action="add.jsp" method="get"> <table align="center" border="2" 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> <td>确认密码:</td> <td> <input type="password" name="repassword" /> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="提交" /> <input type="reset" value="重置" /> </td> </tr> </table> </form> <br> <% if(request.getAttribute("error")!=null) { %> <h2 align="center" style="color:red"><%=request.getAttribute("error") %></h2> <% } %> </body> </html>
1 <%@page import="dao.*"%> 2 <%@page import="model.*"%> 3 <%@page import="Util.*"%> 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 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <% 10 String username=request.getParameter("username"); 11 String password=request.getParameter("password"); 12 UserDaoImpl userDao=new UserDaoImpl(); 13 User user=null; 14 user=userDao.load(username); 15 if(user==null) 16 { 17 request.setAttribute("error", "您要删除的用户不存在"); 18 %> 19 <jsp:forward page="deleteInput.jsp"></jsp:forward> 20 <% 21 } 22 if(!user.getPassword().equals(password)) 23 { 24 request.setAttribute("error", "您输入的密码不正确,无权删除该用户!"); 25 %> 26 <jsp:forward page="deleteInput.jsp"></jsp:forward> 27 <% 28 } 29 userDao.delete(username); 30 %> 31 <h2 align="center" style="color:blue">学号为<%=user.getUsername() %>的用户已经被删除!</h2><br> 32 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>用户删除</title> 8 </head> 9 <body> 10 <form action="delete.jsp" method="get"> 11 <table align="center" border="2" width="500"> 12 <tr> 13 <td>请输入要删除的用户名:</td> 14 <td> 15 <input type="text" name="username" /> 16 </td> 17 </tr> 18 <tr> 19 <td>该用户的密码为:</td> 20 <td> 21 <input type="password" name="password" /> 22 </td> 23 </tr> 24 <tr align="center"> 25 <td colspan="2"> 26 <input type="submit" value="删除" /> 27 </td> 28 </tr> 29 </table> 30 </form> 31 <br> 32 <% 33 if(request.getAttribute("error")!=null) 34 { 35 %> 36 <h2 align="center" style="color:red"><%=request.getAttribute("error") %></h2> 37 <% 38 } 39 %> 40 </body> 41 </html>
1 <%@page import="dao.*"%> 2 <%@page import="model.*"%> 3 <%@page import="Util.*"%> 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 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <% 10 String username=request.getParameter("username"); 11 String password=request.getParameter("password"); 12 if(username==null||"".equals(username.trim())) 13 { 14 request.setAttribute("error","用户名不能为空!"); 15 %> 16 <jsp:forward page="dengluInput.jsp"></jsp:forward> 17 <% 18 } 19 if(password==null||"".equals(password.trim())) 20 { 21 request.setAttribute("error","密码不能为空!"); 22 %> 23 <jsp:forward page="dengluInput.jsp"></jsp:forward> 24 <% 25 } 26 User user=null; 27 UserDaoImpl userDao=new UserDaoImpl(); 28 user = userDao.load(username); 29 if(user==null) 30 { 31 request.setAttribute("error","该用户名不存在!"); 32 %> 33 <jsp:forward page="dengluInput.jsp"></jsp:forward> 34 <% 35 } 36 if(!password.equals(user.getPassword())) 37 { 38 request.setAttribute("error","密码错误!"); 39 %> 40 <jsp:forward page="dengluInput.jsp"></jsp:forward> 41 <% 42 } 43 else 44 { 45 %> 46 <h2 align="center" style="color:blue">登录成功!</h2> 47 <a href="addInput.jsp">用户注册</a> 48 <a href="deleteInput.jsp">删除用户</a> 49 <a href="updateInput.jsp">修改密码</a> 50 <a href="selectInput.jsp">查询用户</a> 51 <% 52 } 53 %> 54 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>登录</title> 8 </head> 9 <body> 10 <form action="denglu.jsp" method="get"> 11 <table align="center" border="2" width="500"> 12 <tr> 13 <td>用户名:</td> 14 <td> 15 <input type="text" name="username" /> 16 </td> 17 </tr> 18 <tr> 19 <td>密码:</td> 20 <td> 21 <input type="password" name="password" /> 22 </td> 23 <td> 24 <a href="updateInput.jsp">修改密码</a> 25 </td> 26 </tr> 27 <tr align="center"> 28 <td colspan="2"> 29 <input type="submit" value="登录" /> 30 </td> 31 </tr> 32 </table> 33 </form> 34 <br> 35 <% 36 if(request.getAttribute("error")!=null) 37 { 38 %> 39 <h2 align="center" style="color:red"><%=request.getAttribute("error") %></h2> 40 <% 41 } 42 %> 43 </body> 44 </html>
1 <%@page import="dao.*"%> 2 <%@page import="model.*"%> 3 <%@page import="Util.*"%> 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 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>搜索</title> 10 </head> 11 <% 12 String username=request.getParameter("username"); 13 if(username==null||"".equals(username.trim())) 14 { 15 request.setAttribute("error", "查询内容不能为空!"); 16 %> 17 <jsp:forward page="selectInput.jsp"></jsp:forward> 18 <% 19 } 20 User user=null; 21 UserDaoImpl userDao=new UserDaoImpl(); 22 user=userDao.load(username); 23 if(user==null) 24 { 25 request.setAttribute("error", "您查找的用户不存在!"); 26 %> 27 <jsp:forward page="selectInput.jsp"></jsp:forward> 28 <% 29 } 30 else 31 { 32 %> 33 <h2 align="center" style="color:blue">查询结果为:编号:<%=user.getId() %>学号:<%=user.getUsername() %>密码:<%=user.getPassword() %></h2> 34 <% 35 } 36 %> 37 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>用户搜索</title> 8 </head> 9 <form action="select.jsp" method="get"> 10 <table align="center" border="2" width="500"> 11 <tr> 12 <td>请输入您要搜索的用户名:</td> 13 <td> 14 <input type="text" name="username" /> 15 </td> 16 </tr> 17 <tr align="center"> 18 <td colspan="2"> 19 <input type="submit" value="查询" /> 20 </td> 21 </tr> 22 </table> 23 </form> 24 <% 25 if(request.getAttribute("error")!=null) 26 { 27 %> 28 <h2 align="center" style="color:red"><%=request.getAttribute("error") %></h2> 29 <% 30 } 31 %> 32 </html>
1 <%@page import="dao.*"%> 2 <%@page import="model.*"%> 3 <%@page import="Util.*"%> 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 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <% 10 String username=request.getParameter("username"); 11 String password=request.getParameter("password"); 12 String newpassword=request.getParameter("newpassword"); 13 String newrepassword=request.getParameter("newrepassword"); 14 if(username==null||"".equals(username.trim())) 15 { 16 request.setAttribute("error","请输入要修改的用户名!"); 17 %> 18 <jsp:forward page="updateInput.jsp"></jsp:forward> 19 <% 20 } 21 if(password==null||"".equals(password.trim())) 22 { 23 request.setAttribute("error","原密码不能为空!!"); 24 %> 25 <jsp:forward page="updateInput.jsp"></jsp:forward> 26 <% 27 } 28 if(newpassword==null||"".equals(newpassword.trim())) 29 { 30 request.setAttribute("error","新密码不能为空!"); 31 %> 32 <jsp:forward page="updateInput.jsp"></jsp:forward> 33 <% 34 } 35 if(newrepassword==null||"".equals(newrepassword.trim())) 36 { 37 request.setAttribute("error","必须再次输入新密码,以免出现错误!"); 38 %> 39 <jsp:forward page="updateInput.jsp"></jsp:forward> 40 <% 41 } 42 User user=new User(); 43 UserDaoImpl userDao=new UserDaoImpl(); 44 user=userDao.load(username); 45 if(user==null) 46 { 47 request.setAttribute("error","该用户不存在,请重新输入。"); 48 %> 49 <jsp:forward page="updateInput.jsp"></jsp:forward> 50 <% 51 } 52 if(!user.getPassword().equals(password)) 53 { 54 request.setAttribute("error", "原密码不正确,无法修改!"); 55 %> 56 <jsp:forward page="updateInput.jsp"></jsp:forward> 57 <% 58 } 59 if(!newpassword.equals(newrepassword)) 60 { 61 request.setAttribute("error", "两次输入的新密码不一致,请确认后再操作,修改失败!"); 62 %> 63 <jsp:forward page="updateInput.jsp"></jsp:forward> 64 <% 65 } 66 user.setUsername(username); 67 user.setPassword(newpassword); 68 userDao.update(user); 69 %> 70 <h2 style="color:blue">用户修改成功!</h2> 71 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>用户修改</title> 8 </head> 9 <body> 10 <form action="update.jsp" method="get"> 11 <table align="center" border="2" width="500"> 12 <tr> 13 <td>要进行修改的用户名:</td> 14 <td> 15 <input type="text" name="username" /> 16 </td> 17 </tr> 18 <tr> 19 <td>原密码:</td> 20 <td> 21 <input type="password" name="password" /> 22 </td> 23 </tr> 24 <tr> 25 <td>新密码:</td> 26 <td> 27 <input type="password" name="newpassword" /> 28 </td> 29 </tr> 30 <tr> 31 <td>请再次输入密码:</td> 32 <td> 33 <input type="password" name="newrepassword" /> 34 </td> 35 </tr> 36 <tr align="center"> 37 <td colspan="2"> 38 <input type="submit" value="提交" /> 39 <input type="reset" value="重置" /> 40 </td> 41 </tr> 42 </table> 43 </form> 44 <% 45 if(request.getAttribute("error")!=null) 46 { 47 %> 48 <h2 align="center" style="color:red"><%=request.getAttribute("error") %></h2> 49 <% 50 } 51 %> 52 </body> 53 </html>
1 package Util; 2 3 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 10 public class Util { 11 public static Connection getConnection() { 12 13 try { 14 Class.forName("com.mysql.jdbc.Driver").newInstance(); 15 } catch (InstantiationException e1) { 16 // TODO Auto-generated catch block 17 e1.printStackTrace(); 18 } catch (IllegalAccessException e1) { 19 // TODO Auto-generated catch block 20 e1.printStackTrace(); 21 } catch (ClassNotFoundException e1) { 22 // TODO Auto-generated catch block 23 e1.printStackTrace(); 24 } 25 String user = "root"; 26 String password = "root"; 27 String url = "jdbc:mysql://localhost:3306/jaovo_msg"; 28 Connection connection = null; 29 try { 30 connection = DriverManager.getConnection(url,user,password); 31 } catch (SQLException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 return connection; 36 } 37 public static void close(Connection connection ) { 38 try { 39 if (connection != null) { 40 connection.close(); 41 } 42 43 } catch (SQLException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 } 48 public static void close(PreparedStatement preparedStatement ) { 49 try { 50 if (preparedStatement != null) { 51 preparedStatement.close(); 52 } 53 54 } catch (SQLException e) { 55 // TODO Auto-generated catch block 56 e.printStackTrace(); 57 } 58 } 59 public static void close(ResultSet resultSet ) { 60 try { 61 if (resultSet != null) { 62 resultSet.close(); 63 } 64 65 } catch (SQLException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } 69 } 70 71 }
课堂测试完成了
三.学习目标
能自己独立做一个网站
时间计划,每周将花十个小时以上来学习这门课