博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JavaWeb学习-1
阅读量:5054 次
发布时间:2019-06-12

本文共 20957 字,大约阅读时间需要 69 分钟。

一,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
load();14 }
IUserDao.java
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 block114             e.printStackTrace();115         }finally116         {117             Util.close(connection);118             Util.close(preparedStatement);119             Util.close(resultSet);120         }121         return user;122     }123 124     @Override125     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 block145             e.printStackTrace();146         }finally147         {148             Util.close(connection);149             Util.close(preparedStatement);150             Util.close(resultSet);151         }152         return user;153     }154 155     @Override156     public List
load() 157 {158 Connection connection=Util.getConnection();159 String sql="select * from t_user ";160 PreparedStatement preparedStatement=null;161 ResultSet resultSet=null;162 List
users=new ArrayList
();163 User user=null;164 try165 {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 block178 e.printStackTrace();179 }finally180 {181 Util.close(connection);182 Util.close(preparedStatement);183 Util.close(resultSet);184 }185 return users;186 }187 }
UserDaoImpl.java
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 }
User.java
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 stub13     }14 15     public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {16         super(message, cause, enableSuppression, writableStackTrace);17         // TODO Auto-generated constructor stub18     }19 20     public UserException(String message, Throwable cause) {21         super(message, cause);22         // TODO Auto-generated constructor stub23     }24 25     public UserException(String message) {26         super(message);27         // TODO Auto-generated constructor stub28     }29 30     public UserException(Throwable cause) {31         super(cause);32         // TODO Auto-generated constructor stub33     }34 35 36 }
UserException.java
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  7  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
18 <%19 }20 if(password==null||"".equals(password.trim()))21 {22 request.setAttribute("error","密码不能为空!");23 %>24
25 <%26 }27 if(repassword==null||"".equals(repassword.trim()))28 {29 request.setAttribute("error","再次输入密码以示确认!");30 %>31
32 <%33 }34 if(!password.trim().equals(repassword.trim()))35 {36 request.setAttribute("error","两次输入的密码不一致。请确认后再进行操作!");37 %>38
39 <%40 }41 User user=new User();42 user.setUsername(username);43 user.setPassword(password);44 UserDaoImpl userDao=new UserDaoImpl();45 try46 {47 userDao.add(user);48 %>49

用户保存成功!

50 <%51 }catch(UserException e)52 {53 %>54

error:<%=e.getMessage() %>

55 <%56 }57 %> 58
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    
用户注册
用户名:
密码:
确认密码:
<% if(request.getAttribute("error")!=null) { %>

<%=request.getAttribute("error") %>

<% } %>
addInput.jsp
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  7  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
20 <%21 }22 if(!user.getPassword().equals(password))23 {24 request.setAttribute("error", "您输入的密码不正确,无权删除该用户!");25 %>26
27 <%28 }29 userDao.delete(username);30 %>31

学号为<%=user.getUsername() %>的用户已经被删除!

32
delete.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3  4  5  6     
7 用户删除 8 9 10
11
12
13
14
17
18
19
20
23
24
25
28
29
请输入要删除的用户名: 15 16
该用户的密码为: 21 22
26 27
30
31
32 <%33 if(request.getAttribute("error")!=null)34 {35 %>36

<%=request.getAttribute("error") %>

37 <%38 }39 %>40 41
deleteInput.jsp
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  7  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
17 <%18 }19 if(password==null||"".equals(password.trim()))20 {21 request.setAttribute("error","密码不能为空!");22 %>23
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
34 <%35 }36 if(!password.equals(user.getPassword()))37 {38 request.setAttribute("error","密码错误!");39 %>40
41 <%42 }43 else44 {45 %>46

登录成功!

47 用户注册48 删除用户49 修改密码50 查询用户51 <%52 }53 %>54
denglu.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3  4  5  6     
7 登录 8 9 10
11
12
13
14
17
18
19
20
23
26
27
28
31
32
用户名: 15 16
密码: 21 22 24 修改密码25
29 30
33
34
35 <%36 if(request.getAttribute("error")!=null)37 {38 %>39

<%=request.getAttribute("error") %>

40 <%41 }42 %>43 44
dengluInput.jsp
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  7  8 
9 搜索10 11 <%12 String username=request.getParameter("username");13 if(username==null||"".equals(username.trim()))14 {15 request.setAttribute("error", "查询内容不能为空!");16 %>17
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
28 <%29 }30 else31 {32 %>33

查询结果为:编号:<%=user.getId() %>学号:<%=user.getUsername() %>密码:<%=user.getPassword() %>

34 <%35 }36 %>37
select.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3  4  5  6     
7 用户搜索 8 9
10
11
12
13
16
17
18
21
22
请输入您要搜索的用户名: 14 15
19 20
23
24 <%25 if(request.getAttribute("error")!=null)26 {27 %>28

<%=request.getAttribute("error") %>

29 <%30 }31 %>32
selectInput.jsp
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  7  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
19 <%20 }21 if(password==null||"".equals(password.trim()))22 {23 request.setAttribute("error","原密码不能为空!!");24 %>25
26 <%27 }28 if(newpassword==null||"".equals(newpassword.trim()))29 {30 request.setAttribute("error","新密码不能为空!");31 %>32
33 <%34 }35 if(newrepassword==null||"".equals(newrepassword.trim()))36 {37 request.setAttribute("error","必须再次输入新密码,以免出现错误!");38 %>39
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
50 <%51 }52 if(!user.getPassword().equals(password))53 {54 request.setAttribute("error", "原密码不正确,无法修改!");55 %>56
57 <%58 }59 if(!newpassword.equals(newrepassword))60 {61 request.setAttribute("error", "两次输入的新密码不一致,请确认后再操作,修改失败!");62 %>63
64 <%65 }66 user.setUsername(username);67 user.setPassword(newpassword);68 userDao.update(user);69 %>70

用户修改成功!

71
update.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3  4  5  6     
7 用户修改 8 9 10
11
12
13
14
17
18
19
20
23
24
25
26
29
30
31
32
35
36
37
41
42
要进行修改的用户名: 15 16
原密码: 21 22
新密码: 27 28
请再次输入密码: 33 34
38 39 40
43
44 <%45 if(request.getAttribute("error")!=null)46 {47 %>48

<%=request.getAttribute("error") %>

49 <%50 }51 %>52 53
updateInput.jsp
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 block17                 e1.printStackTrace();18             } catch (IllegalAccessException e1) {19                 // TODO Auto-generated catch block20                 e1.printStackTrace();21             } catch (ClassNotFoundException e1) {22                 // TODO Auto-generated catch block23                 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 block33             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 block45             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 block56             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 block67             e.printStackTrace();68         }69     }70     71 }
Util.java

 

课堂测试完成了

 

三.学习目标

    能自己独立做一个网站

    时间计划,每周将花十个小时以上来学习这门课

转载于:https://www.cnblogs.com/sdysyhj/p/7886338.html

你可能感兴趣的文章
【安卓5】高级控件——拖动条SeekBar
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
Swift 中的指针使用
查看>>
Swift - 使用闭包筛选过滤数据元素
查看>>
alue of type java.lang.String cannot be converted to JSONObject
查看>>
搜索引擎选择: Elasticsearch与Solr
查看>>
JAVA设计模式之简单工厂模式与工厂方法模式
查看>>
③面向对象程序设计——封装
查看>>
【19】AngularJS 应用
查看>>
Spring
查看>>
Linux 系统的/var目录
查看>>
Redis学习---Redis操作之其他操作
查看>>
WebService中的DataSet序列化使用
查看>>
BZOJ 1200 木梳
查看>>
【Linux】【C语言】菜鸟学习日志(一) 一步一步学习在Linxu下测试程序的运行时间...
查看>>
hostname
查看>>
SpringBoot使用其他的Servlet容器
查看>>
关于cookie存取中文乱码问题
查看>>