First, based on JSP implementation, JSTL and EL expression operation
1. Task Overview
Along with the development of the Internet, a lot of small business website is no longer limited to a few static pages, small building our company has recently received a project, to do the company’s website, the website will not regularly updated products information, and potential users become the company’s VIP user can register account, how to implement the dynamic update and display of these product information? Html is no longer enough for this. Can you use your knowledge to tackle this problem?
In simple terms, it is to achieve user registration, user login, product information add, product information delete… (That’s too fancy a description)
2. Task process
- 1. Understand the nine built-in objects of JSP pages
- 2. Understand the meaning of JSP three instructions
- 3. Import the JSTL label library
- 4. The EL expression displays information
3. Reference process
3.1 Environment Configuration
PS: Don’t use too new version of IDEA, such as version 2020, which has big changes and many Settings are different from the previous version. I often use version 2018.3.6 (the installation package can be found in the group file at the bottom).
1. Modify the IDEA encoding configuration
Create classes and lib in web/WEB-INF, and modify the configuration.
3. Add the Tomcat configuration
The configuration is as follows:
4. The class extends HttpServlet flutters red?
See if there are any packages that introduce Tomcat in the External Libraries
If not, can be in the Project Structure/Modules/Dependencies
3.2 Project Structure
The full project link is here github.com/GoodbyeFire…
SRC directory
A web directory
3.3 Coding process (take adding goods as an example, servlet configuration with annotations, written in a top-down approach)
1. Compile index.jsp and productlist.jsp pages
After login, the user will jump to the index.jsp page, on which there is a hyperlink. After clicking the hyperlink, productList request will be sent to the background. After receiving the request, the background will get the productList information, save it, and forward it to productlist.jsp page.
Productlist.jsp can display product data, including two buttons to add and delete products (button to add a label to realize page jump);
index.jsp
<%@ page contentType="text/html; Charset =UTF-8" language=" Java "%> < HTML > <head> <title>$title $</title> </head> <body> <a href="/productList" > </body> </html>Copy the code
productList.jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> < title > product list < / title > < / head > < body > < table border = "1" > < tr > < td > id < / td > < td > product name < / td > < td > product price < / td > < td > product description < / td > < / tr > <c:forEach var="product" items="${productList}"> <tr> <td>${product.id}</td> <td>${product.productName}</td> <td>${product.price}</td> <td>${product.description}</td> </tr> </c:forEach> </table> <button><a <a href=" delproduct.jsp "> </a></button> </ HTML >Copy the code
Implement productListServlet
When you click on the A tag in index.jsp, send productList request and bind productList request to productListServlet with annotations.
Add private ProductService ProductService = new ProductServiceImpl(); It provides product-specific services (at this point, because the relevant interface and implementation class have not been implemented, it will float red in the editor window, don’t be afraid, it will be solved later);
Through service to get product list ArrayList list = productService. The selectList (); (Class is not implemented, so this method does not exist, it will go red but don’t panic, it will be dealt with later, I will write it down for now)
package com.xrh.servlet; import com.xrh.bean.Product; import com.xrh.service.ProductService; import com.xrh.service.impl.ProductServiceImpl; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; @WebServlet("/productList") public class ProductListServlet extends HttpServlet { private ProductService productService = new ProductServiceImpl(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ArrayList<Product> list = productService.selectList(); req.setAttribute("productList", list); Req.getrequestdispatcher ("/ productlist.jsp ").forward(req, resp); // Forward the page to avoid data loss}}Copy the code
3. Complete the interface and corresponding implementation class in the Service package
Private ProductDao ProductDao = new ProductDaoImpl(); (Also goes red)
Call productDao. SelectList (); Achieve commodity acquisition (again, floating red)
ProductService
package com.xrh.service;
import com.xrh.bean.Product;
import java.util.ArrayList;
public interface ProductService {
ArrayList<Product> selectList();
boolean addProduct(String productName, double price, String description);
boolean delProduct(int id);
}
Copy the code
ProductServiceImpl
package com.xrh.service.impl; import com.xrh.bean.Product; import com.xrh.dao.ProductDao; import com.xrh.dao.impl.ProductDaoImpl; import com.xrh.service.ProductService; import java.util.ArrayList; public class ProductServiceImpl implements ProductService { private ProductDao productDao = new ProductDaoImpl(); @Override public ArrayList<Product> selectList() { return productDao.selectList(); } @Override public boolean addProduct(String productName, double price, String description) { return productDao.addProduct(productName, price, description); } @Override public boolean delProduct(int id) { return productDao.delProduct(id); }}Copy the code
4. Complete the corresponding interface and implementation class in dao
The database operations are complete
ProductDao
package com.xrh.dao;
import com.xrh.bean.Product;
import java.util.ArrayList;
public interface ProductDao {
ArrayList<Product> selectList();
boolean addProduct(String productName, double price, String description);
boolean delProduct(int id);
}
Copy the code
ProductDaoImpl
package com.xrh.dao.impl; import com.xrh.bean.Product; import com.xrh.dao.ProductDao; import com.xrh.util.DruidUtil; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import java.util.Arrays; public class ProductDaoImpl implements ProductDao { @Override public ArrayList<Product> selectList() { Connection con = DruidUtil.getConnection(); String sql = "select * from products"; ResultSet temp = null; ArrayList<Product> result = new ArrayList<>(); try { temp = DruidUtil.query(sql, null); while (temp.next()) { int id = temp.getInt("id"); String productName = temp.getString("productName"); double price = temp.getDouble("price"); String description = temp.getString("description"); result.add(new Product(id, productName, price, description)); } } catch (Exception e) { e.printStackTrace(); } finally { DruidUtil.close(); } return result; } @Override public boolean addProduct(String productName, double price, String description) { Connection con = DruidUtil.getConnection(); String sql = "insert products (productName, price, description) values(? ,? ,?) "; ArrayList<Object> params = new ArrayList<>(Arrays.asList(productName, price, description)); int result = 0; try { result = DruidUtil.update(sql, params); } catch (Exception e) { e.printStackTrace(); } finally { DruidUtil.close(); } return result == 0 ? false : true; } @Override public boolean delProduct(int id) { Connection con = DruidUtil.getConnection(); String sql = "delete from products where id=?" ; ArrayList<Object> params = new ArrayList<>(Arrays.asList(id)); int result = 0; try { result = DruidUtil.update(sql, params); } catch (Exception e) { e.printStackTrace(); } finally { DruidUtil.close(); } return result == 0 ? false : true; }}Copy the code
5, repeat the above process to complete the product adding function (JSP = “Servlet =” service= “DAO)
One way to realize product addition: jump to the form page of product addition through the button under the product list, fill in the relevant information and submit it to upload to the database, click return list, you can return to the product list to observe the changes;
3.4 Result Presentation
Low EMOTIONAL intelligence: Don’t want to get involved
High EQ: Focus on backend functionality
3.5 tip
How do I automatically generate bean classes for database tables?
package com.xrh.bean; public class Users { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}Copy the code
The chapter summary is here (the better we understand you guys, the better we understand you guys).
Students who are interested in learning Java are welcome to join QQ Learning exchange group: 1126298731
Have a question welcome to ask, everyone together in learning Java on the road to play strange upgrade! (O ゜ – ゜▽゜)