软件工程概论 第一课:课后作业1

一、网站系统开发需要掌握的技术;
1、界面和用户体验:

  ①要了解Web在各大浏览器的执行情况,必须保证站点在主要浏览器上都能正常运行。至少要测试Gecko、Webkit、IE和Opera等引擎。不同的操作系统,可能也会影响浏览器如何呈现网站。除了浏览器,网站还有其他使用方式:手机、搜索引擎等等。应该充分了解这些情况下,网站的运行状况。

  ②知道怎么能在基本不影响用户使用的情况下升级网站。通常来说,必须有版本控制系统和数据备份机制。

  ③保密性,不要直接显示用户的Email地址和各项信息,至少不要用纯文本显示。

  ④作为一个友好的网站,不要让用户看到那些不友好的出错提示。

  ⑤为你的网站设置一些合理的使用限制,一旦超过门槛值,就自动停止服务,这与网站安全相关。

  ⑥知道如何实现网页的渐进式增强。

  ⑦用户发出POST请求后,总是将其重导向至另外一个网页。

  ⑧不要忘记网站的可访问性,比如要考虑残疾人如何使用网站。

2、安全性:

  ①阅读《OWASP开发指南》,它提供了全面的网站安全指导。

  ②永远不要信任用户提交的数据

  ③了解SQL注入及其预防方法,做好安全措施,防止信息泄露。

  ④不要明文储存用户的密码,要hash处理后再储存。

  ⑤不要对你的用户认证系统太自信,它可能很容易就被攻破,并不是那么安全,总会有人去找出一些漏洞,要提早意识到这个问题。

  ⑥在登录页面及其他处理敏感信息的页面,使用SSL、HTTPS。

  ⑦了解如何处理信用卡。

  ⑧要了解如何对付session劫持。

  ⑨避免“跨站点执行”。

  ⑩避免“跨域伪造请求”。

  除此之外,还有一些要注意的地方:

  及时打上补丁,让系统始终跟上最新版本。确认数据库连接信息的安全性。跟踪攻击技术的最新发展,以及使用的平台的最新安全漏洞。阅读Google的《浏览器安全手册》。阅读《网络软件的黑客手册》。

3、性能:

  ①只要有可能,就使用缓存。要正确理解和使用Http caching与HTML5离线储存。

  ②优化图片。不要把一个图片文件,作为重复出现的网页背景图案。

  ③学习如何用gzip和deflate内容。

  ④将多个样式表文件或脚本文件,合为一个文件,这样可以减少浏览器的http请求数,以及减小gzip压缩后的文件总体积。

  ⑤如果你的网页用到大量的小体积图片(比如工具栏),就应该使用Css image script,目的是减少http请求数。

  ⑥大流量的网站应该考虑将网页对象分布在多个域名。

  ⑦静态内容都应该放在一个不需要使用cookie的独立域名之上。因为域名之下如果有,那么客户端向该域名发出的每次http请求,都会附上cookie内容。这里的一个好方法就是使用"内容分发网络"。

  ⑧将浏览器完成网页渲染所需要的http请求数最小化。

  ⑨使用Google的Closure Complier压缩JavaScript文件,YUI Compresser亦可。

  ⑩确保网站根目录下有favicon.ico文件,因为即使网页中根本不包括这个文件,浏览器也会自动发出对它的请求。如果这个文件不存在,就会产生大量的404错误,消耗光服务器的带宽。

4、搜索引擎优化:

  ①使用"搜索引擎友好"的URL形式,比如example.com/pages/45-article-title,而不是example.com/index.php?page=45。

  ②不要使用"点击这里"之类的超级链接,因为这样等于浪费了一个SEO机会,而且降低了"屏幕朗读器"的使用效果。

  ③创建一个XML sitemap文件,它的缺省位置一般是/sitemap.xml(即放在网站根目录下)。

  ④当你有多个URL指向同一个内容时,在网页代码中使用<link rel ="canonical ".../>。

  ⑤使用Google的 Webmaster  Tools和Yahoo的 Site Explorer。

  ⑥从一开始就使用Google Analyties。

  ⑦知道robots.txt的作用,以及搜索引擎蜘蛛的工作原理。

  ⑧将www.example.com的访问请求导向example.com,或者采用相反的做法,目的是防止Google把它们当做两个网站,分开计算排名。

  ⑨知道存在着恶意或行为不正当的网络蜘蛛。

  ⑩如果网站有非文本的内容(比如视频、音频等等),你应该参考Google的sitemap扩展协议。

5、技术:

  ①理解HTTP协议,诸如GET、POST、sessions、cookies之类的概念,包括"无状态"是什么意思。

  ②确保XHTML和HTML和CSS符合W3C标准,使得它们能够通过检验。这可以使网页避免触发浏览器的古怪行为,而且使它在"屏幕朗读器"和手机上也能正常工作。

  ③理解浏览器如何处理JavaScript脚本。

  ④理解网页上的JavaScript文件、样式表文件和其他资源是如何装载及运行的,考虑它们对页面性能有何影响。在某些情况下,可能应该将脚本文件放置在网页的尾部。

  ⑤理解JavaScript沙箱的工作原理,尤其是如果打算使用iframe。

  ⑥了解301重定向和302重定向之间的区别(这也是一个SEO相关问题)。

  ⑦知道JavaScript可能无法使用或被禁用,以及Ajax并不是一定会运行。"不允许脚本运行"正在某些用户中变得流行,手机浏览器对脚本的支持千差万别,而Google索引网页时不运行大部分的脚本文件。

  ⑧尽可能多得了解部署平台。

  ⑨考虑使用样式表重置。

  ⑩考虑使用JavaScript框架,使用它们可以不用考虑浏览器之间的差异。

6、解决bug:

  ①建立一个有效的错误报告机制。

  ②程序员20%的时间用于编码,80%的时间用于维护,根据这一点相应合理的安排时间。

  ③建立某些途径或系统,让用户可以与后台接触,提出建议和批评。

  ④为将来的维护和客服人员撰写文档,解释清楚系统是怎么运行的。

  ⑤除了备份机制,你还必须有一个恢复机制。

  ⑥使用某种版本控制系统储存你的文件,比如Subversion或Git。

  ⑦不要忘记做单元测试,Selenium之类的框架会有用。

二、本次课堂测试的源程序代码:

IUserDao.java:

 1 package com.user.gb.dao;
 2 import java.util.List;
 3 import com.user.gb.model.User;
 4 public interface IUserDao 
 5 {
 6     public void add(User user);
 7     public void delete(String username);
 8     public void update(User user);
 9     public User load(int id);
10     public User load(String username);
11     public List<User> load();
12 }        

UserDaoImpl.java:

  1 package com.user.gb.dao;
  2 import java.sql.Connection;
  3 import java.sql.PreparedStatement;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 import com.user.gb.Util.GBUtil;
  9 import com.user.gb.model.User;
 10 import com.user.gb.model.UserException;
 11 public class UserDaoImpl implements IUserDao
 12 {
 13     public void add(User user) 
 14     {
 15         Connection connection=GBUtil.getConnection();
 16         String sql="select count(*) from xinxi where username=?";
 17         PreparedStatement preparedStatement=null;
 18         ResultSet resultSet=null;
 19         try 
 20         {
 21             preparedStatement=connection.prepareStatement(sql);
 22             preparedStatement.setString(1, user.getUsername());
 23             resultSet=preparedStatement.executeQuery();
 24             while(resultSet.next())
 25             {
 26                 if(resultSet.getInt(1)>0)
 27                 {
 28                     throw new UserException("用户名已存在!");
 29                 }
 30             }
 31         } 
 32         catch (SQLException e)
 33         {
 34             // TODO Auto-generated catch block
 35             e.printStackTrace();
 36         }
 37         sql="insert into xinxi(username,password) value(?,?)";
 38         try
 39         {
 40             preparedStatement=connection.prepareStatement(sql);
 41             preparedStatement.setString(1, user.getUsername());
 42             preparedStatement.setString(2, user.getPassword());
 43             preparedStatement.executeUpdate();
 44         } 
 45         catch (SQLException e) 
 46         {
 47             // TODO Auto-generated catch block
 48             e.printStackTrace();
 49         }
 50         finally
 51         {
 52             GBUtil.close(connection);
 53             GBUtil.close(preparedStatement);
 54             GBUtil.close(resultSet);
 55         }
 56     }
 57     @Override
 58     public void delete(String username) 
 59     {
 60         Connection connection=GBUtil.getConnection();
 61         String sql="delete from xinxi where username = ?";
 62         PreparedStatement preparedStatement=null;
 63         try 
 64         {
 65             preparedStatement=connection.prepareStatement(sql);
 66             preparedStatement.setString(1, username);
 67             preparedStatement.executeUpdate();
 68         } 
 69         catch (SQLException e) 
 70         {
 71             e.printStackTrace();
 72         }
 73         finally
 74         {
 75             GBUtil.close(connection);
 76             GBUtil.close(preparedStatement);
 77         }
 78     }
 79     public void update(User user) 
 80     {
 81         Connection connection=GBUtil.getConnection();
 82         String sql="update xinxi set password = ? where username = ?";
 83         PreparedStatement preparedStatement=null;
 84         try
 85         {
 86             preparedStatement=connection.prepareStatement(sql);
 87             preparedStatement.setString(1, user.getPassword());
 88             preparedStatement.setString(2, user.getUsername());
 89             preparedStatement.executeUpdate();
 90         } 
 91         catch (SQLException e) 
 92         {
 93             e.printStackTrace();
 94         }
 95         finally
 96         {
 97             GBUtil.close(connection);
 98             GBUtil.close(preparedStatement);
 99         }
100     }
101     public User load(int id) 
102     {
103         Connection connection=GBUtil.getConnection();
104         String sql="select * from xinxi where id=?";
105         User user=null;
106         PreparedStatement preparedStatement=null;
107         ResultSet resultSet=null;
108         try 
109         {
110             preparedStatement=connection.prepareStatement(sql);
111             preparedStatement.setInt(1, id);
112             resultSet=preparedStatement.executeQuery();
113             while(resultSet.next())
114             {
115                 user=new User();
116                 user.setId(id);
117                 user.setUsername(resultSet.getString("username"));
118                 user.setPassword(resultSet.getString("password"));
119             }
120         } 
121         catch (SQLException e) 
122         {
123             e.printStackTrace();
124         }
125         finally
126         {
127             GBUtil.close(connection);
128             GBUtil.close(preparedStatement);
129             GBUtil.close(resultSet);
130         }
131         return user;
132     }
133     public User load(String username) 
134     {
135         Connection connection=GBUtil.getConnection();
136         String sql="select * from xinxi where username=?";
137         User user=null;
138         PreparedStatement preparedStatement=null;
139         ResultSet resultSet=null;
140         try 
141         {
142             preparedStatement=connection.prepareStatement(sql);
143             preparedStatement.setString(1, username);
144             resultSet=preparedStatement.executeQuery();
145             while(resultSet.next())
146             {
147                 user=new User();
148                 user.setId(resultSet.getInt("id"));
149                 user.setUsername(username);
150                 user.setPassword(resultSet.getString("password"));
151             }
152         } 
153         catch (SQLException e) 
154         {
155             e.printStackTrace();
156         }
157         finally
158         {
159             GBUtil.close(connection);
160             GBUtil.close(preparedStatement);
161             GBUtil.close(resultSet);
162         }
163         return user;
164     }
165     public List<User> load() 
166     {
167         Connection connection=GBUtil.getConnection();
168         String sql="select * from xinxi ";
169         PreparedStatement preparedStatement=null;
170         ResultSet resultSet=null;
171         List<User> users=new ArrayList<User>();
172         User user=null;
173         try
174         {
175             preparedStatement=connection.prepareStatement(sql);
176             resultSet=preparedStatement.executeQuery();
177             while(resultSet.next())
178             {
179                 user=new User();
180                 user.setId(resultSet.getInt("id"));
181                 user.setUsername(resultSet.getString("username"));
182                 user.setPassword(resultSet.getString("password"));
183                 users.add(user);
184             }
185         }
186         catch (SQLException e) 
187         {
188             e.printStackTrace();
189         }
190         finally
191         {
192             GBUtil.close(connection);
193             GBUtil.close(preparedStatement);
194             GBUtil.close(resultSet);
195         }
196     return users;
197     }
198 }                                                    

User.java:

 1 package com.user.gb.model;
 2 public class User 
 3 {
 4     private int id;
 5     private String username;
 6     private String password;
 7     public int getId() 
 8     {
 9         return id;
10     }
11     public void setId(int id) 
12     {
13         this.id = id;
14     }
15     public String getUsername()
16     {
17         return username;
18     }
19     public void setUsername(String username) 
20     {
21         this.username = username;
22     }
23     public String getPassword() 
24     {
25         return password;
26     }
27     public void setPassword(String password) 
28     {
29         this.password = password;
30     }
31 }    

UserException.java:

 1 package com.user.gb.model;
 2 public class UserException extends RuntimeException
 3 {
 4     public UserException() 
 5     {
 6         super();
 7     }
 8     public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) 
 9     {
10         super(message, cause, enableSuppression,         writableStackTrace);
11     }
12     public UserException(String message, Throwable cause) 
13     {
14         super(message, cause);
15     }
16     public UserException(String message) 
17     {
18         super(message);
19     }
20     public UserException(Throwable cause) 
21     {
22         super(cause);
23     }
24 }    

GBUtil.java:

 1 package com.user.gb.Util;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 public class GBUtil 
 8 {
 9     public static Connection getConnection()
10     {
11         try
12         {
13             Class.forName("com.mysql.jdbc.Driver");
14         }
15         catch(ClassNotFoundException e)
16         {
17             e.printStackTrace();
18         }
19         String url="jdbc:mysql://localhost:3306/denglu?useSSL=false";
20         Connection connection=null;
21         try 
22         {
23             connection=DriverManager.getConnection(url, "root", "242772");
24         } 
25         catch (SQLException e) 
26         {
27             // TODO Auto-generated catch block
28             e.printStackTrace();
29             System.out.println("数据库连接失败");
30         }
31         return connection;
32     }
33     public static void close(Connection connection)
34     {
35         try
36         {
37             if(connection!=null)
38             {
39                 connection.close();
40             }
41         }
42         catch(SQLException e)
43         {
44             e.printStackTrace();
45         }
46     }
47     public static void close(PreparedStatement preparedStatement)
48     {
49         try
50         {
51             if(preparedStatement!=null)
52             {
53                 preparedStatement.close();
54             }
55         }
56         catch(SQLException e)
57         {
58             e.printStackTrace();
59         }
60     }
61     public static void close(ResultSet resultSet)
62     {
63         try
64         {
65             if(resultSet!=null)
66             {
67                 resultSet.close();
68             }
69         }
70         catch(SQLException e)
71         {
72             e.printStackTrace();
73         }
74     }
75 }            

add.jsp:

 1 <%@page import="com.user.gb.dao.UserDaoImpl"%>
 2 <%@page import="com.user.gb.model.User"%>
 3 <%@page import="com.user.gb.model.UserException"%>
 4 <%@page import="com.user.gb.Util.GBUtil"%>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6 pageEncoding="UTF-8"%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <%
11     String username=request.getParameter("username");
12     String password=request.getParameter("password");
13     String repassword=request.getParameter("repassword");
14     if(username==null||"".equals(username.trim()))
15     {
16         request.setAttribute("error","用户名不能为空!");
17 %>
18     <jsp:forward page="addInput.jsp"></jsp:forward>
19 <%
20     }
21     if(password==null||"".equals(password.trim()))
22     {
23     request.setAttribute("error","密码不能为空!");
24 %>
25     <jsp:forward page="addInput.jsp"></jsp:forward>
26 <%
27     }
28     if(repassword==null||"".equals(repassword.trim()))
29     {
30 request.setAttribute("error","再次输入密码以示确认!");
31 %>
32     <jsp:forward page="addInput.jsp"></jsp:forward>
33 <%
34     }
35     if(!password.trim().equals(repassword.trim()))
36     {
37         request.setAttribute("error","两次输入的密码不一致。请确认后再进行操作!");
38 %>
39     <jsp:forward page="addInput.jsp"></jsp:forward>
40 <%
41     }
42     User user=new User();
43     user.setUsername(username);
44     user.setPassword(password);
45     UserDaoImpl userDao=new UserDaoImpl();
46     try
47     {
48         userDao.add(user);
49 %>
50     <h2 style="color:blue">用户保存成功!</h2>
51 <%
52     }
53     catch(UserException e)
54     {
55 %>
56     <h2 style="color:red">error:<%=e.getMessage() %></h2>
57 <%
58     }
59 %> 
60 </html>

addInput.jsp:

 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="add.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="repassword" />
28                 </td>
29             </tr>
30             <tr align="center">
31                 <td colspan="2">
32                     <input type="submit" value="提交" />
33                     <input type="reset" value="重置" />
34                 </td>
35             </tr>
36         </table>
37     </form>
38 <br>
39 <%
40     if(request.getAttribute("error")!=null)
41     {
42 %>
43     <h2 align="center" style="color:red">        <%=request.getAttribute("error") %></h2>
44 <%
45     }
46 %>
47 </body>
48 </html>        

delete.jsp:

 1 <%@page import="com.user.gb.dao.UserDaoImpl"%>
 2 <%@page import="com.user.gb.model.User"%>
 3 <%@page import="com.user.gb.model.UserException"%>
 4 <%@page import="com.user.gb.Util.GBUtil"%>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6 pageEncoding="UTF-8"%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <%
11     String username=request.getParameter("username");
12     String password=request.getParameter("password");
13     UserDaoImpl userDao=new UserDaoImpl();
14     User user=null;
15     user=userDao.load(username); 
16     if(user==null)
17     {
18         request.setAttribute("error", "您要删除的用户不存在");
19 %>
20     <jsp:forward page="deleteInput.jsp"></jsp:forward>
21 <%
22     }
23     if(!user.getPassword().equals(password))
24     {
25         request.setAttribute("error", "您输入的密码不正确,无权删除该用户!");
26 %>
27     <jsp:forward page="deleteInput.jsp"></jsp:forward>
28 <%
29     }
30     userDao.delete(username);
31 %>
32     <h2 align="center" style="color:blue">学号为<%=user.getUsername() %>的用户已经被删除!</h2><br>
33 </html>

deleteInput.jsp:

 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>

select.jsp:

 1 <%@page import="com.user.gb.dao.UserDaoImpl"%>
 2 <%@page import="com.user.gb.model.User"%>
 3 <%@page import="com.user.gb.model.UserException"%>
 4 <%@page import="com.user.gb.Util.GBUtil"%>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6 pageEncoding="UTF-8"%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <title>搜索</title>
11 </head>
12 <%
13     String username=request.getParameter("username");
14     if(username==null||"".equals(username.trim()))
15     {
16         request.setAttribute("error", "查询内容不能为空!");
17 %>
18     <jsp:forward page="selectInput.jsp"></jsp:forward>
19 <%
20     }
21     User user=null;
22     UserDaoImpl userDao=new UserDaoImpl();
23     user=userDao.load(username);
24     if(user==null)
25     {
26         request.setAttribute("error", "您查找的用户不存在!");
27 %>
28     <jsp:forward page="selectInput.jsp"></jsp:forward>
29 <%
30     }
31 else
32     {
33 %>
34     <h2 align="center" style="color:blue">查询结果为:编号:    <%=user.getId() %>学号:<%=user.getUsername() %>密码:    <%=user.getPassword() %></h2>
35 <%
36     }
37 %>
38 </html>

selectInput.jsp:

 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>

update.jsp:

 1 <%@page import="com.user.gb.dao.UserDaoImpl"%>
 2 <%@page import="com.user.gb.model.User"%>
 3 <%@page import="com.user.gb.model.UserException"%>
 4 <%@page import="com.user.gb.Util.GBUtil"%>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6 pageEncoding="UTF-8"%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <%
11     String username=request.getParameter("username");
12     String password=request.getParameter("password");
13     String newpassword=request.getParameter("newpassword");
14     String newrepassword=request.getParameter("newrepassword");
15     if(username==null||"".equals(username.trim()))
16     {
17         request.setAttribute("error","请输入要修改的用户名!");
18 %>
19     <jsp:forward page="updateInput.jsp"></jsp:forward>
20 <%
21     }
22     if(password==null||"".equals(password.trim()))
23     {
24         request.setAttribute("error","原密码不能为空!!");
25 %>
26     <jsp:forward page="updateInput.jsp"></jsp:forward>
27 <%
28     }
29     if(newpassword==null||"".equals(newpassword.trim()))
30     {
31         request.setAttribute("error","新密码不能为空!");
32 %>
33     <jsp:forward page="updateInput.jsp"></jsp:forward>
34 <%
35     }
36     if(newrepassword==null||"".equals(newrepassword.trim()))
37     {
38         request.setAttribute("error","必须再次输入新密码,以免出现错误!");
39 %>
40     <jsp:forward page="updateInput.jsp"></jsp:forward>
41 <%
42     }
43     User user=new User();
44     UserDaoImpl userDao=new UserDaoImpl();
45     user=userDao.load(username);
46     if(user==null)
47     {
48         request.setAttribute("error","该用户不存在,请重新输入。");
49 %>
50     <jsp:forward page="updateInput.jsp"></jsp:forward>
51 <%
52     }
53     if(!user.getPassword().equals(password))
54     {
55         request.setAttribute("error", "原密码不正确,无法修改!");
56 %>
57     <jsp:forward page="updateInput.jsp"></jsp:forward>
58 <%
59     }
60     if(!newpassword.equals(newrepassword))
61     {
62         request.setAttribute("error", "两次输入的新密码不一致,请确认后再操作,修改失败!");
63 %>
64     <jsp:forward page="updateInput.jsp"></jsp:forward>
65 <%
66     }
67     user.setUsername(username);
68     user.setPassword(newpassword);
69     userDao.update(user);
70 %>
71     <h2 style="color:blue">用户修改成功!</h2> 
72 </html>

updateInput.jsp:

 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>

denglu.jsp:

 1 <%@page import="com.user.gb.dao.UserDaoImpl"%>
 2 <%@page import="com.user.gb.model.User"%>
 3 <%@page import="com.user.gb.model.UserException"%>
 4 <%@page import="com.user.gb.Util.GBUtil"%>
 5 <%@ page language="java" contentType="text/html; charset=UTF-8"
 6 pageEncoding="UTF-8"%>
 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 8 <html>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <% 
11     String username=request.getParameter("username");
12     String password=request.getParameter("password");
13     if(username==null||"".equals(username.trim()))
14     {
15         request.setAttribute("error","用户名不能为空!");
16 %>
17     <jsp:forward page="dengluInput.jsp"></jsp:forward>
18 <%
19     }
20     if(password==null||"".equals(password.trim()))
21     {
22         request.setAttribute("error","密码不能为空!");
23 %>
24     <jsp:forward page="dengluInput.jsp"></jsp:forward>
25 <%
26     }
27     User user=null;
28     UserDaoImpl userDao=new UserDaoImpl();
29     user=userDao.load(username);
30     if(user==null)
31     {
32         request.setAttribute("error","该用户名不存在!");
33 %>
34     <jsp:forward page="dengluInput.jsp"></jsp:forward>
35 <%
36     }
37     if(!password.equals(user.getPassword()))
38     {
39         request.setAttribute("error","密码错误!");
40 %>
41     <jsp:forward page="dengluInput.jsp"></jsp:forward>
42 <%
43     }
44     else
45     {
46 %>
47     <h2 align="center" style="color:blue">登录成功!</h2>
48     <a href="addInput.jsp">用户注册</a>
49     <a href="deleteInput.jsp">删除用户</a>
50     <a href="updateInput.jsp">修改密码</a>
51     <a href="selectInput.jsp">查询用户</a>
52 <%
53     }
54 %>
55 </html>

dengluInput.jsp:

 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="4">
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>

三、运行结果截图:

初始界面:

   

 

不输入任何东西直接登录:

   

 

密码错误时:

   

登录成功:

   

 

由四个选项分别将用户导入到不同的界面,对应增删改查:

增加(初始):

   

 

 

当用户名已存在:

   

用户保存成功:

   

删除(初始):

   

删除成功:                                            

   

修改(初始):      

   

修改用户不存在时:

    

修改用户时密码不正确:

   

修改成功:

    

查询:

   

成功查询:

   

用户不存在时:

   

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

       这几周的时间,我希望写出一个可用的,有实际用户的软件。 能够完整体验软件生命周期,对于各个阶段有实际的了解,对于软件设计有实际的掌握,对敏捷软件开发的具体技术有实践能力。在个人作业和团队作业中, 了解团队的各个角色,和各个角色的互动。对于其中一个角色有实际的深入体验。 学习如何与不同的角色打交道,培养团队精神,学会解决冲突的几种方法。

  每天可以花费三四个小时用来编程,每周下来三十个小时左右,编程重要的还是练习,把写程序当做日常。

posted @ 2017-11-23 15:03  我命倾尘  阅读(215)  评论(0编辑  收藏  举报