hooyantsing's Blog

第87次课程_考前复习

字数统计: 2.3k阅读时长: 13 min
2019/12/03

源辰76班

第87次课程

2019.12.03

内容

考前复习[廖彦]

项目案例 market

前端

WebContent/login.jsp

df47bb79366f212c4ae4c9263204f557.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<%@ page language="java"  contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"  content="text/html; charset=utf-8">
<title>登录页面</title>
</head>
<body>
<h3>登录页面</h3>
<font color="red">${msg }</font>
<form action="login.s" method="post">
用户名:<input name="name">

密码:<input name="pwd">

<input type="submit" value="登录">
</form>
</body>
</html>

WebContent/queryProduct.jsp

325cc7d508e23366a2abc09c0270f795.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<%@page import="java.util.*"%>
<%@ page language="java"  contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"  content="text/html; charset=utf-8">
<title>商品查询</title>
</head>
<body>
<h3>商品查询</h3>
<form action="query.s">
<button>查询</button>
</form>
<table style="width:100%" border="1px">
    <tr>
       <th>ID</th>
       <th>名称</th>
       <th>价格</th>
       <th>库存</th>
       <th>进价</th>
       <th>类型</th>
       <th>描述</th>
       <th>图片</th>
       <th>操作</th>
    </tr>
    <%
       List<Map<?,?>> list =  (List<Map<?,?>>)request.getAttribute("list");
       if(list==null){
    %>
       <tr><td colspan="10">没有查询到商品记录</td></tr>
    <%  } else {
       for(Map<?,?> row : list){
           pageContext.setAttribute("r",  row);
    %>
    <tr>
       <td>${r.pid}</td>
       <td>${r.pname}</td>
       <td>${r.pprice}</td>
       <td>${r.cost}</td>
       <td>${r.inprice}</td>
       <td>${r.ptype}</td>
       <td>${r.pdesc}</td>
       <td><img alt="" src="${r.pimg}"  height="30px"></td>
       <td><a href="edit.s?pid=${r.pid }">修改</a></td>
    </tr>
    <%} }%>
</table>
</body>
</html>

WebContent/editProduct.jsp

78bf28581572276e04e0bf2899202953.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<%@ page language="java"  contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"  content="text/html; charset=utf-8">
<title>商品编辑</title>
</head>
<body>
<form action="save.s" method="post">
<input name="pid" type="hidden"  value="${row.pid }">
名称:<input name="pname" value="${row.pname  }">

价格:<input name="pprice"  value="${row.pprice }">

数量:<input name="cost" value="${row.cost  }">

进价:<input name="inprice"  value="${row.inprice }">

类型:<input name="ptype" value="${row.ptype  }">

描述:<input name="pdesc" value="${row.pdesc  }">

图片:<input name="pimg" value="${row.pimg  }">

<input type="submit" value="保存">
</form>
</body>
</html>

后端

src/com/yc/market/servlet/LoginFilter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.yc.market.servlet;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import  javax.servlet.http.HttpServletRequest;
@WebFilter(urlPatterns =  {"/edit.s","/query.s","/save.s","/editProduct.jsp","/queryProduct.jsp"})
public class LoginFilter implements Filter {
    public LoginFilter() {
        
    }
    public void destroy() {
       
    }
    public void doFilter(ServletRequest  request, ServletResponse response,  FilterChain chain) throws IOException,  ServletException {
       HttpServletRequest req =  (HttpServletRequest) request;
       if(req.getSession().getAttribute("loginedUser") == null) {
           req.setAttribute("msg", "请先登录系统");
           req.getRequestDispatcher("login.jsp").forward(req, response);
       }else {
           chain.doFilter(request,  response);
       }
    }
    public void init(FilterConfig fConfig)  throws ServletException {
       
    }
}

src/com/yc/market/servlet/LoginServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.yc.market.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/login.s")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1/market";
            String user = "root";
            String password = "a";
            conn = DriverManager.getConnection(url,user,password);
            String sql = "select * from user where account = ? and pwd = ? and isadmin = 1 ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            ResultSet rs = ps.executeQuery();
            Map<String,Object> row = new HashMap<>();
            if(rs.next()) {
                row.put("id", rs.getObject("id"));
                row.put("name", rs.getObject("name"));
                row.put("tel", rs.getObject("tel"));
                row.put("email", rs.getObject("email"));
                row.put("pwd", rs.getObject("pwd"));
                row.put("account", rs.getObject("account"));
                row.put("isadmin", rs.getObject("isadmin"));
                request.getSession().setAttribute("loginedUser", row);
                response.sendRedirect("query.s");
            } else {
                request.setAttribute("msg", "用户名或密码错误");
                request.getRequestDispatcher("login.jsp").forward(request, response);
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

src/com/yc/market/servlet/QueryProductServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.yc.market.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/query.s")
public class QueryProductServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1/market";
            String user = "root";
            String password = "a";
            conn = DriverManager.getConnection(url,user,password);
            String sql = "select * from product ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            List<Map<String,Object>> list = new ArrayList<>();
            while(rs.next()) {
                HashMap<String,Object> row = new HashMap<>();
                row.put("pid", rs.getObject("pid"));
                row.put("pname", rs.getObject("pname"));
                row.put("pprice", rs.getObject("pprice"));
                row.put("cost", rs.getObject("cost"));
                row.put("inprice", rs.getObject("inprice"));
                row.put("ptype", rs.getObject("ptype"));
                row.put("pdesc", rs.getObject("pdesc"));
                row.put("pimg", rs.getObject("pimg"));
                list.add(row);
            }
            request.setAttribute("list", list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            request.getRequestDispatcher("queryProduct.jsp").forward(request, response);
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

src/com/yc/market/servlet/SaveProductServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.yc.market.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/query.s")
public class QueryProductServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1/market";
            String user = "root";
            String password = "a";
            conn = DriverManager.getConnection(url,user,password);
            String sql = "select * from product ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            List<Map<String,Object>> list = new ArrayList<>();
            while(rs.next()) {
                HashMap<String,Object> row = new HashMap<>();
                row.put("pid", rs.getObject("pid"));
                row.put("pname", rs.getObject("pname"));
                row.put("pprice", rs.getObject("pprice"));
                row.put("cost", rs.getObject("cost"));
                row.put("inprice", rs.getObject("inprice"));
                row.put("ptype", rs.getObject("ptype"));
                row.put("pdesc", rs.getObject("pdesc"));
                row.put("pimg", rs.getObject("pimg"));
                list.add(row);
            }
            request.setAttribute("list", list);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            request.getRequestDispatcher("queryProduct.jsp").forward(request, response);
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

src/com/yc/market/servlet/EditProductServlet.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.yc.market.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/edit.s")
public class EditProductServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1/market";
            String user = "root";
            String password = "a";
            conn = DriverManager.getConnection(url,user,password);
            String sql = "select * from product where pid = ? ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, pid);
            ResultSet rs = ps.executeQuery();
            
            Map<String,Object> row = new HashMap<>();
            if(rs.next()) {
                row.put("pid", rs.getObject("pid"));
                row.put("pname", rs.getObject("pname"));
                row.put("pprice", rs.getObject("pprice"));
                row.put("cost", rs.getObject("cost"));
                row.put("inprice", rs.getObject("inprice"));
                row.put("ptype", rs.getObject("ptype"));
                row.put("pdesc", rs.getObject("pdesc"));
                row.put("pimg", rs.getObject("pimg"));
            }
            request.setAttribute("row", row);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            request.getRequestDispatcher("editProduct.jsp").forward(request, response);
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
CATALOG