In Javaee, Model1 model is centered on the JSSP page. JSSP must not only logically process the browser's request (using Javabean) and also display relevant pages when accessing the database.
In the model1 model, there is no servlet.
The result diagram of Model1 is as follows:
Model1 has poor maintainability and scalability, which are only suitable for small projects.
First run the results
goods.jsp
<%@page import="entity.Items"%> <%@page import="dao.ItemsDao"%> <%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <style type="text/css"> div { float: left; margin: 10px; } div dd { margin: 0px; font-size: 10pt; } div dd.dd_name { color: blue; } div dd.dd_city { color: #000; } </style> </head> <body> <center> <h1>Product Display</h1> <hr> <table cellpadding="0" cellpacing="0" > <tr> <td> <% ItemsDao dao = new ItemsDao(); ArrayList<Items> list = new ArrayList<Items>(); //Get all products from dao and save them to the list collection list = dao.getAllItems(); if (list != null && list.size() > 0) { //Loop through the collection and display for (int i = 0; i < list.size(); i++) { Items item = list.get(i); %> <div> <dl> <dt> <a href="details.jsp?id=<%=item.getId()%>"><img src="images/<%=item.getPicture()%>" /> </a> </dt> <dd><%=item.getName()%></dd> <dd> Origin: <%=item.getCity()%> Price:¥ <%=item.getPrice()%></dd> </dl> </div> <% } } %> </td> </tr> </table> </table> </center> </body> </html> Image representing the product in the code
<span style="white-space:pre"> </span><a href="details.jsp?id=<%=item.getId()%>"><img src="images/<%=item.getPicture()%>" /> </a>
By clicking on the product image, pass the id of the current product to the details page
details.jsp displays detailed products through the id of the product, and browsing history is maintained by cookies
<%@page import="org.apache.taglibs.standard.tag.common.xml.ForEachTag"%> <%@page import="entity.Items"%> <%@page import="dao.ItemsDao"%> <%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <style type="text/css"> #historyview { border: 1; background: #EAEAEE; } #historyview td { font-size: 10px; } </style> </head> <body> <center> <h1>Product details</h1> <hr> <table cellpadding="0" cellpacing="0"> <tr> <td> <center> <table> <% ItemsDao dao = new ItemsDao(); //Get the corresponding product object in dao according to the product id sent from the request Items item = dao.getItemById(Integer.parseInt(request .getParameter("id"))); if (item != null) { %> <tr> <td rowspan="5"><img src="images/<%=item.getPicture()%>" ></td> </tr> <tr> <td><b><%=item.getName()%></b> </td> </tr> <tr> <td id="cityname">Origin: <%=item.getCity()%></td> </tr> <tr> <td id="pricename">Price: <%=item.getPrice()%> ¥</td> </tr> <tr> <td id="pricename">Price: <%=item.getPrice()%> ¥</td> </tr> <% } //Add this product Cookies[] cookies = request.getCookies(); String historyStr = ""; for (Cookie c : cookies) { if (c.getName().equals("history")) { historyStr = c.getValue(); } } historyStr += item.getId() + ","; Cookie c = new Cookie("history", historyStr); //Reset cookies response.addCookie(c); %> </table> </center></td> <td valign="top" id="historyview"> <center> <table> <tr> <td><b>The products you have browsed</b></td> </tr> <% //Fetch the last three browsed records from dao based on cookies and save them to the list collection ArrayList<Items> historyItems = dao.getHistoryView(historyStr); if (historyItems != null && historyItems.size() > 0) { //Transfer the set for (Items historyItem : historyItems) { %> <tr> <td><a href="details.jsp?id=<%=historyItem.getId()%>"><img src="images/<%=historyItem.getPicture()%>" > </a></td> </tr> <tr> <td><b><%=historyItem.getName()%></b> </td> </tr> <tr> <td>Origin: <%=historyItem.getCity()%></td> </tr> <% } } %> </table> </tr> </tr> </table> </center> </body> </html> The dao layer is responsible for the query operation of products in the database
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import util.DBHelper; import entity.Items; //Business logic class of product public class ItemsDao { // Get all product information public ArrayList<Items> getAllItems() { // Product collection ArrayList<Items> list = new ArrayList<Items>(); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = DBHelper.getConnection(); String sql = "select * from items";// sql statement ps = conn.prepareStatement(sql); rs = ps.executeQuery(); // Add the results of the query to the collection in turn while (rs.next()) { Items item = new Items(); item.setId(rs.getInt("id")); item.setName(rs.getString("name")); item.setCity(rs.getString("city")); item.setPrice(rs.getDouble("price")); item.setPicture(rs.getString("picture")); item.setNumber(rs.getInt("number")); list.add(item); } } catch (SQLException e) { e.printStackTrace(); } finally { // Close the resource if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return list; } // Get product information based on the product number public Items getItemById(int id) { Items item = new Items(); Connection con = null; PreparedStatement ps = null; ResultSet rs = null; String sql = "select * from items where id = ?"; try { con = DBHelper.getConnection(); ps = con.prepareStatement(sql); ps.setInt(1, id); rs = ps.executeQuery(); // If the id is found, initialize it for the item object if (rs.next()) { item.setId(rs.getInt("id")); item.setName(rs.getString("name")); item.setCity(rs.getString("city")); item.setPrice(rs.getDouble("price")); item.setPicture(rs.getString("picture")); item.setPicture(rs.getString("picture")); item.setNumber(rs.getInt("number")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { // Close resource if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return item; } // Get the last three items browsed based on cookies public ArrayList<Items> getHistoryView(String cookie) { ArrayList<Items> list = new ArrayList<Items>(); String ids[] = cookie.split(","); int counts = 3;// The last three records browsed if (ids != null && ids.length > 0) { for (int i = ids.length - 1; i >= 0 && i > ids.length - counts - 1; i--) { Items item = getItemById(Integer.parseInt(ids[i])); /* * First determine whether the current item exists in the collection. If counts+1 exists, read it once more (guaranteed to have 3 objects in the list collection) Do not add this item*/ if (list.contains(item)) { counts++; continue; } list.add(item); } } return list; } } Items of the entity category
package entity; public class Items { private int id; private String name; private String city; private double price; private int number; private String picture; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getPicture() { return picture; } public void setPicture(String picture) { this.picture = picture; } @Override public int hashCode() { // TODO Auto-generated method stub return this.getId()+this.getName().hashCode(); } @Override public boolean equals(Object obj) { if(this==obj) { return true; } else { if(obj instanceof Items) { Items item=(Items) obj; if(this.getId()==item.getId()&&this.getName().equals(item.getName())) { return true; } } } return false; } } Here we rewritten the hasCode and equals methods to modify the comparison method (all items are a new object. Even if the contents of the two products are the same, they will not be equal. So we need to modify the comparison method)
Because for browsing history, we cannot refresh the current product browsing history and all the products are the products. We just need to ensure that there is only one product in the browsing history. So the getHistoryView method in the dao layer has this code
<span style="white-space:pre"> </span>if (list.contains(item)) { counts++; continue; } Then there is the tool class
DBHelpher singleton pattern obtains connection object
package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBHelper { private static final String driver = "com.mysql.jdbc.Driver"; private static final String url = "jdbc:mysql://localhost:3306/shopping?useUnicode=true&charcterEncoding=UTF-8"; private static final String username = "root"; private static final String password = "123"; private static Connection con = null; // The static block code is responsible for loading the driver static { try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() { if (con == null) { try { con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return con; } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.