Java EE Model1 model implements merchandise browsing records (removing duplicate browsing Records) (i) _java

Source: Internet
Author: User

In Java EE, the MODEL1 model is centered on JSP pages, the JSP is not only to make logical processing of the browser request (using JavaBean), access to the database to display the relevant pages.
In the MODEL1 model, there is no servlet.
The MODEL1 results are shown below:

Model1 maintainability Scalability is poor only for small projects.

Run the results first

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 () + PA 
th + "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

Picture of a product in code

<span style= "White-space:pre" >               </span>

<a href= "Details.jsp?id=<%=item.getid ()%>" >

 </a 
 
> 

Pass the ID of the current item to the details page by clicking on the product's picture
DETAILS.JSP displays detailed merchandise by ID of the product, while browsing records are 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 () + PA 
th + "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >  

DAO Layer   responsible for the query operation of the commodity 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 for merchandise public class Itemsdao {//Get all merchandise information public arraylist<items> Getallitems () {//Product collection Arra 
    ylist<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 while (Rs.next ()) {The 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 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; 
    ///According to the commodity number get the merchandise information public items GetItemByID (int id) {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 this ID is found to initialize if (Rs.next ()) for the item object {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")); 
    The 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; /////The last three items that are browsed based on cookies public arraylist<items> Gethistoryview (String cookie) {Arraylist<items&gt ; List = new Arraylist<items> (); 
    String ids[] = Cookie.split (","); int counts = 3;//Browse last three records if (IDs!= null && ids.length > 0) {for (int i = ids.length-1; ; = 0 && i > ids.length-counts-1; 
        i--) {Items = GetItemByID (Integer.parseint (ids[i));  
          * * To first determine if there is a current item in the collection if there is a counts+1 read once (guaranteed 3 objects in the list collection) do not add this item */if (List.contains (item)) { 
          counts++; 
        Continue 
      } list.add (item); 
  } return list; 
 } 
}

Entity class items for merchandise

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; 
  The 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 to 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; 
 } 
}

Rewrite the Hascode and equals methods here to modify the comparison method (all items are a new object even if the contents of all two products are not equal.) So to modify the comparison method)
Because we are not able to refresh the current product browsing record as for the browsing record. All we have to do is make sure that there is only one item in the browsing record.
So the Gethistoryview method in the DAO layer has this code

<span style= "White-space:pre" >       </span>if (List.contains (item)) { 
          counts++; 
          Continue 
        

And then the tool class .
dbhelpher single case mode for connection objects

 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 the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.