By GokiSoft.com|
21:29 17/08/2020|
JSP Servlet
[Share Code] Bài tập quản lý sản phẩm sử dụng Session, JSTL, JavaBean trong lập trình JSP/Servlet
Bài tập quản lý sản phẩm sử dụng Session, JSTL, JavaBean trong lập trình JSP/Servlet
#add.jsp
<%--
Document : add
Created on : Aug 17, 2020, 8:22:48 PM
Author : Diep
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Add Product</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Add Product</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Name:</label>
<input type="text" value="${index}" name="index" style="display: none;"/>
<input required="true" value="${product.name}" type="text" class="form-control" id="name" name="name"/>
</div>
<div class="form-group">
<label for="email">Category:</label>
<input required="true" value="${product.category}" type="text" class="form-control" id="category" name="category"/>
</div>
<div class="form-group">
<label for="price">Price:</label>
<input type="number" value="${product.price}" class="form-control" id="price" name="price"/>
</div>
<div class="form-group">
<label for="description">Description:</label>
<input type="text" value="${product.description}" class="form-control" id="description" name="description"/>
</div>
<button class="btn btn-success">Register</button>
</form>
</div>
</div>
</div>
</body>
</html>
#list.jsp
<%--
Document : add
Created on : Aug 17, 2020, 8:22:48 PM
Author : Diep
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Add Product</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">List Product</h2>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Description</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<c:forEach var="product" items="${productList}" varStatus="loop">
<tr>
<td>${loop.index + 1}</td>
<td>${product.name}</td>
<td>${product.category}</td>
<td>${product.price}</td>
<td>${product.description}</td>
<td>
<a href="add?id=${loop.index}">
<button class="btn btn-warning">Edit</button>
</a>
</td>
<td>
<form method="post">
<input name="index" value="${loop.index}" style="display: none"/>
<button class="btn btn-danger" name="action" value="delete">Delete</button>
</form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
#AddProductServlet.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import bean.Product;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author Diep
*/
public class AddProductServlet extends HttpServlet {
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int index = -1;
if(request.getParameter("id") != null) {
index = Integer.parseInt(request.getParameter("id"));
}
//save product into session
HttpSession session = request.getSession(false);
List<Product> productList = new ArrayList<>();
if(session.getAttribute("productList") != null) {
productList = (List<Product>) session.getAttribute("productList");
}
Product product = new Product();
if(productList.size() > index && index != -1) {
product = productList.get(index);
} else {
index = -1;
}
System.out.println(product);
request.setAttribute("product", product);
request.setAttribute("index", index);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/product/add.jsp");
dispatcher.forward(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String category = request.getParameter("category");
float price = Float.parseFloat(request.getParameter("price"));
String description = request.getParameter("description");
int index = Integer.parseInt(request.getParameter("index"));
Product product = new Product(name, category, description, price);
//save product into session
HttpSession session = request.getSession(false);
List<Product> productList = new ArrayList<>();
if(session.getAttribute("productList") != null) {
productList = (List<Product>) session.getAttribute("productList");
}
if(index >= 0) {
productList.set(index, product);
} else {
productList.add(product);
}
session.setAttribute("productList", productList);
response.sendRedirect("add");
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
#ListProductServlet.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package servlet;
import bean.Product;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author Diep
*/
public class ListProductServlet extends HttpServlet {
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/product/list.jsp");
dispatcher.forward(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
int index = Integer.parseInt(request.getParameter("index"));
if(action.equalsIgnoreCase("delete")) {
//save product into session
HttpSession session = request.getSession(false);
List<Product> productList = new ArrayList<>();
if(session.getAttribute("productList") != null) {
productList = (List<Product>) session.getAttribute("productList");
productList.remove(index);
}
session.setAttribute("productList", productList);
}
response.sendRedirect("list");
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
#Product.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bean;
import java.io.Serializable;
/**
*
* @author Diep
*/
public class Product implements Serializable{
String name, category, description;
float price;
public Product() {
}
public Product(String name, String category, String description, float price) {
this.name = name;
this.category = category;
this.description = description;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" + "name=" + name + ", category=" + category + ", description=" + description + ", price=" + price + '}';
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)