Giỏ hàng với JSP
Trong bài này tôi sẽ hướng dẫn bạn cách tạo 1 giỏ hàng dùng jsp chuẩn. Trước hết, bạn cấn tạo 1 database hết sức đơn giản có tên qlsp và trong đó có 1 bảng có tên Sanpham(maSP,tenSP,dongia). Ở đây tôi không có ý bàn về database, tôi chỉ dùng nó đơn giản là công cụ hỗ trợ lưu dữ liệu. Trong ứng dụng thực tế bạn không thiết kế cơ sở dữ liệu như thế này. Thiết kế của bảng dữ liệu như sau:

Bạn nhập 1 số mẫu tin vào để thử. Tôi có vài mẫu tin như sau:

Bây giờ bạn tạo 1 thư mục trong thư mục webapps của Tomcat có tên ShoppingCart_SJSP.
Bạn tạo thêm thư mục WEB-INF\lib, WEB-INF\classes trong thư mục trên.
Bây giờ bạn phải viết code java đặc tả các đối tượng có liên quan đến giỏ hàng của mình.
Đầu tiên là đối tượng Sanpham để đặc tả cho 1 sản phẩm. Code như sau:
package www.vvh.com.db;
public class SanPham {
private String msSP;
private String tenSP;
private double dongia;
public SanPham() {
super();
}
public SanPham(String msSP, String tenSP, double dongia) {
super();
this.msSP = msSP;
this.tenSP = tenSP;
this.dongia = dongia;
}
public String getMsSP() {
return msSP;
}
public void setMsSP(String msSP) {
this.msSP = msSP;
}
public String getTenSP() {
return tenSP;
}
public void setTenSP(String tenSP) {
this.tenSP = tenSP;
}
public double getDongia() {
return dongia;
}
public void setDongia(double dongia) {
this.dongia = dongia;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((msSP == null) ? 0 : msSP.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SanPham other = (SanPham) obj;
if (msSP == null) {
if (other.msSP != null)
return false;
} else if (!msSP.equals(other.msSP))
return false;
return true;
}
@Override
public String toString() {
return tenSP;
}
}
Sau đó bạn viết các lớp nối đến database phục vụ cho việc lấy dữ liệu. Có 2 lớp, lớp ConnectDBFactory dùng để kết nối đến database, ở đây chúng ta kết nối đến hệ quản trị cơ sở dữ liệu MySQL và lớp XulyDB bao gồm các thao tác xử lý căn bản.
package www.vvh.com.db;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectDBFactory {
private static Connection con;
public static Connection CreateMySqlConnection
(String database)throws Exception{
String url="com.mysql.jdbc.Driver";
Class.forName(url);
String dbURL = "jdbc:mysql://localhost:3306/"
+database+"?user=root&password=";
con=DriverManager.getConnection(dbURL);
return con;
}
}
package www.vvh.com.db;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class XulyDB {
private Connection con;
public XulyDB(){
try {
con=ConnectDBFactory.CreateMySqlConnection("qlsp");
} catch (Exception e) {
e.printStackTrace();
}
}
public SanPham getSanPham(String ms){
SanPham sp=null;
try {
Statement stmt=con.createStatement();
String sql="select * from sanpham where mssp='"+ms+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()){
String mssp=rs.getString("mssp");
String ten=rs.getString("tenSP");
double dg=rs.getDouble("dongia");
sp=new SanPham(mssp,ten,dg);
}
} catch (Exception e) {
e.printStackTrace();
}
return sp;
}
public ResultSet getAllProducts(){
ResultSet rs=null;
try {
Statement stmt=con.createStatement();
String sql="select * from sanpham";
rs=stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
}
Để cho việc kết nối thành công chúng ta cần phải copy driver của MySQL đến thư mục WEB-INF\lib.
Tiếp theo ta phải đặc tả 1 giỏ hàng để client có thể sử dụng. Ta nhận thấy rằng 1 giỏ hàng chứa nhiều món hàng, mỗi món hàng giữ thông tin về mã số của sản phẩm, số lượng đang có trong giỏ và đơn giá của mỗi sản phẩm. Đơn giá trong đối tượng này có thể không cần vì ta có thể dựa vào mã số sản phẩm có thể lấy được, tuy nhiên ở đây ý tôi muốn nói là bạn có thể lấy giá trong database làm giá cơ bản, bạn thêm vào các loại phí,… để tạo thành giá bán.
Code cho đối tượng món hàng như sau:
package www.vvh.com.spc;
public class MonHang {
private String msMH;
private int soluong;
private double dongia;
public double getDongia() {
return dongia;
}
public void setDongia() {
}
public MonHang(String msMH, int soluong, double dongia) {
super();
this.msMH = msMH;
this.soluong = soluong;
this.dongia = dongia;
}
public String getMsMH() {
return msMH;
}
public void setMsMH(String msMH) {
this.msMH = msMH;
}
public int getSoluong() {
return soluong;
}
public void setSoluong(int soluong) {
this.soluong = soluong;
}
public void setDongia(double dongia) {
this.dongia = dongia;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((msMH == null) ? 0 : msMH.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MonHang other = (MonHang) obj;
if (msMH == null) {
if (other.msMH != null)
return false;
} else if (!msMH.equalsIgnoreCase(other.msMH))
return false;
return true;
}
@Override
public String toString() {
return msMH;
}
}
Trong đối tượng giỏ hàng ta dùng 1 ArrayList để lưu trữ 1 danh sách các món hàng. Ở đây tôi chỉ viết 2 thao tác là thêm hàng vào giỏ và tính tổng giá tiền của giỏ hàng còn những phương thức khác như Xóa món hàng khỏi giỏ,… bạn có thể bổ sung thêm.
package www.vvh.com.spc;
import java.util.ArrayList;
public class Giohang {
private ArrayList<MonHang>cart;
public Giohang(){
cart=new ArrayList<MonHang>();
}
public void ThemHang(MonHang mh){
//Nếu món hàng đã có trong giỏ
//thì cập nhập lại số lượng
if(cart.contains(mh)){
MonHang hang=cart.get(cart.indexOf(mh));
hang.setSoluong(hang.getSoluong()+mh.getSoluong());
}
else{//còn không thì thêm mới
cart.add(mh);
}
}
public double Tongtien(){
double tien=0;
for(MonHang mh:cart){
tien+=mh.getDongia()*mh.getSoluong();
}
return tien;
}
public ArrayList<MonHang> getGH(){
return cart;
}
}
OK, như vậy phần code xong rồi! Bạn tiến hành biên dịch và đóng gói nó lại.
Bạn tạo 1 file có tên build.bat với nội dung sau
| javac -d . -encoding UTF-8 *.java
pause |
Thực thi file này, đảm bảo bạn không nhìn thấy lỗi nào. Sau đó tiến hành đóng gói nó, bạn tạo file pack.bat có nội dung sau:
| jar -cvf shop.jar www/vvh/com/spc/*.class www/vvh/com/db/*.class
pause |
Thực thi file này, bạn sẽ có file shop.jar, copy file này vào thư mục lib.
Bây giờ bạn hãy khởi động Tomcat(nếu đã khởi động thì restart lại).
Tiếp theo bạn thiết kế phần JSP.
Đầu tiên bạn thiết kế giao diện cho trang shopping.jsp, code như sau:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="loi.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.sql.ResultSet"%>
<%@page import="www.vvh.com.db.XulyDB"%>
<%@page import="www.vvh.com.spc.Giohang"%>
<%@page import="java.util.ArrayList"%>
<%@page import="www.vvh.com.spc.MonHang"%>
<%@page import="www.vvh.com.db.SanPham"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Mua hàng qua mạng</title>
</head>
<body>
<form action="xuly.jsp" method="post">
Chọn sản phẩm:<select name="ms">
<%
XulyDB db=new XulyDB();
ResultSet rs=db.getAllProducts();
while(rs.next()){
%>
<option label='<%=rs.getString("tensp") %>'
value='<%=rs.getString("mssp") %>'>
<%
}
%>
</select>
Số lượng:<input type="text" name="sl"/>
<input type="submit" value="Mua Hàng" name="mua"/>
<input type="submit" value="In Hóa đơn" name="in"/>
<hr/>
<h1>CHI TIẾT GIỎ HÀNG</h1>
<table border="1" width="70%">
<tr><th>MSSP</th>
<th>Ten san Phẩm</th>
<th>Số lượng</th>
<th>Đơn giá</th></tr>
<%
Giohang cart=(Giohang)session.getAttribute("gio");
if(cart!=null){
ArrayList<MonHang> ds=cart.getGH();
for(MonHang mh:ds){
SanPham sp=db.getSanPham(mh.getMsMH());
%>
<tr>
<td><%= mh.getMsMH()%></td>
<td><%= sp.getTenSP()%></td>
<td><%= mh.getSoluong()%></td>
<td><%= mh.getDongia()%></td>
</tr>
<%
}
%>
</table>
<h2 align="right">Tổng tiền:<%=cart.Tongtien() %></h2>
<%} %>
</form>
</body>
</html>
Mong muốn của bạn khi thực thi trang này sẽ là:

Khi khách hàng chọn sản phẩm, nhập vào số lượng rồi nhấn nút mua hàng, quá trình xử lý sẽ chuyển sang trang xuly.jsp. Code như sau:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="loi.jsp"%>
<%@page import="www.vvh.com.spc.Giohang"%>
<%@page import="www.vvh.com.db.XulyDB"%>
<%@page import="www.vvh.com.db.SanPham"%>
<%@page import="www.vvh.com.spc.MonHang"%><html>
<%
Giohang cart=(Giohang)session.getAttribute("gio");
if(request.getParameter("mua")!=null){
String mssp=request.getParameter("ms");
int soluong=Integer.parseInt(request.getParameter("sl"));
if(cart==null)
cart=new Giohang();
XulyDB db=new XulyDB();
SanPham sp=db.getSanPham(mssp);//l;ay 1 sp
MonHang mh=new MonHang(sp.getMsSP(),soluong,sp.getDongia());
//them vào giỏ
cart.ThemHang(mh);
session.setAttribute("gio",cart);
response.sendRedirect("shopping.jsp");
}
else if(request.getParameter("in")!=null){
response.sendRedirect("hoadon.jsp");
}
%>
Khi khách hàng nhấn nút In Hóa đơn, trang hoadon.jsp sẽ xử lý, nội dung của nó như sau:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="loi.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="www.vvh.com.spc.Giohang"%>
<%@page import="java.util.ArrayList"%>
<%@page import="www.vvh.com.spc.MonHang"%>
<%@page import="www.vvh.com.db.SanPham"%>
<%@page import="www.vvh.com.db.XulyDB"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" width="70%">
<tr><th>MSSP</th>
<th>Ten san Phẩm</th>
<th>Số lượng</th>
<th>Đơn giá</th></tr>
<%
Giohang cart=(Giohang)session.getAttribute("gio");
if(cart!=null){
XulyDB db=new XulyDB();
ArrayList<MonHang> ds=cart.getGH();
for(MonHang mh:ds){
SanPham sp=db.getSanPham(mh.getMsMH());
%>
<tr>
<td><%= mh.getMsMH()%></td>
<td><%= sp.getTenSP()%></td>
<td><%= mh.getSoluong()%></td>
<td><%= mh.getDongia()%></td>
</tr>
<%
}
%>
</table>
<h2 align="right">Tổng tiền:<%=cart.Tongtien() %></h2>
<%} %>
</body>
</html>
Cuối cùng là trang hiển thị thông tin lỗi. Code như sau:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Trang lỗi</title>
</head>
<body>
<h2>Bạn đã nhập liệu sai!</h2>
<hr/>
<%= exception.toString() %>
</body>
</html>
Cấu trúc chương trình:

Kết quả thử nghiệm:
Chúc thành công!
Võ Thanh Minh said
Thầy ơi để kết nối MS Access thì chỉ cấn sửa chổ kết nối hả thầy? Em mới học nhìn khó hiều quá. Thầy giúp dùm em. CÁm ơn thầy!
vovanhai said
Ừ, bạn chỉ thay đổi kết nối đến CSDL trong lớp ConnectionFactory thoi. Chúc vui!
Nguyen Dinh Thang said
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /Shopping.jsp(1,39) quote symbol expected
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:205)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:155)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:167)
org.apache.jasper.compiler.ParserController.getPageEncodingForJspSyntax(ParserController.java:451)
org.apache.jasper.compiler.ParserController.determineSyntaxAndEncoding(ParserController.java:392)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:173)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:103)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:154)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:315)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:282)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:586)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
Nguyen Dinh Thang said
sua dum em loi tren voi thay oi?
em lam bai gio hang chay file shoppinh.jsp no bao loi tren
vovanhai said
Lỗi là do ở đây: /Shopping.jsp(1,39) quote symbol expected. Có nghĩa là em quên đóng dấu ngoặc kép ở dòng 1.
08TLT said
thay co em hoi:
- Em lam theo huong dan o tren,nhung khi bien dich file giohang.java thi bao loi o dong:private ArrayListcart;
- Va em khong hieu:”Để cho việc kết nối thành công chúng ta cần phải copy driver của MySQL đến thư mục WEB-INF\lib” la nhu the nao.
- Va khi em bien dich 2file:build.bat va pack.bat thi khong duoc
Thay giup em voi nhe, cam on thay
vovanhai said
- Dòng khai báo private ArrayListcart; dùng để khai báo 1 danh sách các món hàng MonHang. Ở đây dùng generic(hỗ trợ JDK từ jdk1.5)
- Thư mục WEB-INF\lib là nơi chứa các thư viện mà trang web sẽ tham chiếu đến. Do đó driver truy xuất csdl phải nằm ở đây.
- Build không được tôi đoán có lẽ em chưa khai báo trong tùy biến môi trường PATH. Giải pháp: Tạo 1 biến môi trường có tên JAVA_HOME và có giá trị là đường dẫn đến thư mục cài đặt JDK. Sửa giá trị của biến môi trường Path bằng cách thêm vào: .;%JAVA_HOME%\bin.
Chúc vui!
08TLT said
Em dang lam gio hang bang jsp,dang rat can,nhung sao kho qua.Thay co the cho em code gio hang chi bang jsp ma ko su dung java nhu tren, duoc khong thay. Vi em sua loi file giohang.java hoai ma ko duoc. Neu duoc em cam on thay nhieu lam!
Thinh said
Thầy ơi cho em hỏi:
Em đang dùng jdk 1.6.0
Em dùng EditPlus để biên dịch file Giohang.java, thì báo lỗi thế này:
Giohang.java:6: expected
private ArrayListcart;
^
Giohang.java:9: ‘(‘ or ‘[‘ expected
cart=new ArrayList();
^
Giohang.java:29: ‘;’ expected
for(MonHang mh:cart)
^
Giohang.java:34: illegal start of expression
}
^
Giohang.java:36: expected
public ArrayList getGH()
^
Giohang.java:41: ‘;’ expected
}
^
6 errors
Em sửa hoài lỗi này mà không được, thầy sửa giùm em với, em cảm ơn thầy!
Thinh said
private ArrayListcart;
^
vovanhai said
private ArrayList cart; Chứ không phải : private ArrayListcart;
Thịnh said
Dĩ nhiên là em copy và paste nguyên bài của thầy, em dùng Ediplus nhưng khi biên dịch thì báo lỗi. Em cop nguyên đoạn lỗi gửi thầy sửa giùm nhưng khi gửi lên server thì những chữ nằm trong dấu ngoặc nhọn trong đoạn lỗi thì bị mất đi. Thầy có thể mail cho em để em có thể gửi câu hỏi chính xác, được không thầy. Thầy giúp em với, em đang làm đồ án môn học và rất cần giỏ hàng này. Em cảm ơn thầy.
vovanhai said
Email của tôi có trong trang About. Chúc vui!
Hoang Khoa said
for(ChoosenItem mh : ds)
đoạn code trên được hiểu như thế nào thưa thầy ? Em mới thấy lần đầu.
Cám ơn thầy.
vovanhai said
Đó là vòng lặp for (each) đó bạn.
truy van co so du lieu said
em chao thay.
thay cho em hoi:
neu muon viet cau truy van ngay tai giao dien cua database roi goi len code thi phai lam the nao?tuc la minh kho viet code truy van ngay tai code cua trang web
vovanhai said
Tôi không biết liệu có phải em hỏi về StoredProcedure trong SQL server hay Query trong MS Access không. Nếu đúng vậy thì bạn dùng CallableStatement để gọi. Bạn tìm trong blog có chỗ hướng dẫn đấy!
www.blog.kai.vn said
Ban co the su dung code nay de tham khao, ban tieng viet, gio hang voi jsp, shop jsp, share code jsp
>> download va xem huong dan su dung jsp shop tai day
welcome said
Chào thầy! em thấy trang web của thầy rất có ý nghĩa. ở đây bọn em có thể được thầy trả lời mọi thắc mắc, Đó thực sự là 1 điều hết sức ý nghĩa. Chúc thầy sức khoẻ và thành công
Van Ngoan said
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
org.apache.jsp.shopping_jsp._jspService(shopping_jsp.java:82)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.20 logs.
Apache Tomcat/6.0.20
Em chạy file shopping.jsp thì bị lỗi như trên.thầy giúp dùm em nhe,cám ơn thây
vovanhai said
Thêm kiểm tra null đi. trong bài tôi quên đoạn ấy.
Phuong said
Cảm ơn bạn ! bài viết rất hay
Nếu mà dùng PHP thì bạn có thể đọc bài viết này
http://kenphan.info/view/2010/03/Huong-dan-dung-AJAX-(jQuery)-amp-PHP-tao-gio-hang-(drag-amp-drop).html
Mình thấy bài viết cũng rất hay đó chứ
Quan said
org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 60 in the jsp file: /shopping.jsp
Generated servlet error:
Syntax error on token “<", invalid AssignmentOperator
An error occurred at line: 60 in the jsp file: /shopping.jsp
Generated servlet error:
Syntax error on token "=", != expected
An error occurred at line: 60 in the jsp file: /shopping.jsp
Generated servlet error:
Syntax error on token(s), misplaced construct(s)
An error occurred at line: 60 in the jsp file: /shopping.jsp
Generated servlet error:
Syntax error on token ")", : expected
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:296)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
em khong hieu loi nay la gi thua thay
vovanhai said
Lỗi ở dòng 60 của trang JSP. dấu ‘:’ bị thiếu
jomokino said
Thưa thầy em đã làm chạy được rồi, nhưng cho em hỏi tại sao trong combobox khi bấm đổ xuống lại không thấy chữ của các sản phẩm, tức là có thấy các dòng tương ứng với các Sp có trong database nhưng em không thấy chử mà chỉ thấy khoảng trắng.Khi nhập số lượng bấm MUA HANG thì table phía dưới đổ ra thì thấy thông tin đầy đủ gồm: Mã Sản Phẩm,Tên Sản Phẩm,Số Lượng,Đơn Giá. Mong thầy chỉ giúp em.
Võ Văn Hải said
Trình duyệt đấy. Cái sẽ hiển thị cho mỗi option là:
option value=”xx”>thành phần hiển thị/option>
(nhớ thêm dấu < vào)
Doan Cuong said
chào thầy em bị ở chỗ lấy driver của MySQL vào thư mục lib như thầy nói. Vậy thầy có thể hướng dẫn cho em chi tiết về việc đó được không thầy. Nếu thầy có thời gian xin chỉ dùm em, em gần đến hạn nộp đề tài rồi
hương said
thưa thầy e chạy giỏ hàng như trên của thầy khi e nhập dữ liệu vào textbox số lượng thì báo lỗi java.lang.NullPointerException vậy khắc phục sao hả thầy
Võ Văn Hải said
Download MySQL JDBC Driver->Giải nén sẽ thấy file mysql-connector-java-5.x.x-bin.jar->Copy file này vào thư mục WEB-INF\lib. OK.
Hoàng said
Xin hỏi thầy thuật toán để tạo ra giỏ hàng này là như thế nào, thầy có thể nói rõ hơn để bọn em hiểu rỏ đc không thầy!
Cảm ơn thầy nhiều lắm!
Võ Văn Hải said
Dùng collection(ArrayList) để chứa 1 danh sách các đối tượng. Trong collection đã có hỗ trợ các hầu hết phương thức làm việc nên không có thuật toán gì là phúc tạp ở đây.
thanh said
public int hashCode() {
37 final int prime = 31;
38 int result = 1;
39 result = prime * result + ((msSP == null) ? 0 : msSP.hashCode());
40 return result;
41 }
cai doan code nay dung de lam gi vay Thay..
Võ Văn Hải said
Phương thức để sinh mã băm dùng trong coolection của java. Mục đích là có duy nhất 1 mã băm cho 1 đối tượng trong tập hợp. Bạn xem thêm khái niệm hash table
tuan said
thay cho em hoi loi nay la gi vay thay
em lam y chang bai cua thay luon no báo lổi
HTTP Status 404 – /WebApplication6/
——————————————————————————–
type Status report
message /WebApplication6/
description The requested resource (/WebApplication6/) is not available.
——————————————————————————–
Apache Tomcat/7.0.5
tuan said
mình tao hai file nay để ở đâu vây thầy?
Bạn tạo 1 file có tên build.bat với nội dung sau
javac -d . -encoding UTF-8 *.java
pause
Thực thi file này, đảm bảo bạn không nhìn thấy lỗi nào. Sau đó tiến hành đóng gói nó, bạn tạo file pack.bat có nội dung sau:
jar -cvf shop.jar www/vvh/com/spc/*.class www/vvh/com/db/*.class
pause
tuan said
thầy cho em hỏi. em co 1 file quan ly làm bằng jsp . em muốn add no vào netbean để no chạy thì làm sao hả thầy.
mong thay giúp e
tuan said
thầy ơi giúp e với khi em chạy localhost:8080/ShoppingCart_SJSP/shopping.jsp
thì nó báo lổi thầy sửa dùm em với?????
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 7 in the generated java file
Only a type can be imported. http://www.vvh.com.db.XulyDB resolves to a package
An error occurred at line: 8 in the generated java file
Only a type can be imported. http://www.vvh.com.spc.Giohang resolves to a package
An error occurred at line: 10 in the generated java file
Only a type can be imported. http://www.vvh.com.spc.MonHang resolves to a package
An error occurred at line: 11 in the generated java file
Only a type can be imported. http://www.vvh.com.db.SanPham resolves to a package
An error occurred at line: 37 in the jsp file: /shopping.jsp
XulyDB cannot be resolved to a type
34:
35: <%
36:
37: XulyDB db=new XulyDB();
38:
39: ResultSet rs=db.getAllProducts();
40:
An error occurred at line: 37 in the jsp file: /shopping.jsp
XulyDB cannot be resolved to a type
34:
35: <%
36:
37: XulyDB db=new XulyDB();
38:
39: ResultSet rs=db.getAllProducts();
40:
An error occurred at line: 79 in the jsp file: /shopping.jsp
Giohang cannot be resolved to a type
76:
77: <%
78:
79: Giohang cart=(Giohang)session.getAttribute("gio");
80:
81: if(cart!=null){
82:
An error occurred at line: 79 in the jsp file: /shopping.jsp
Giohang cannot be resolved to a type
76:
77: <%
78:
79: Giohang cart=(Giohang)session.getAttribute("gio");
80:
81: if(cart!=null){
82:
An error occurred at line: 83 in the jsp file: /shopping.jsp
MonHang cannot be resolved to a type
80:
81: if(cart!=null){
82:
83: ArrayList ds=cart.getGH();
84:
85: for(MonHang mh:ds){
86:
An error occurred at line: 85 in the jsp file: /shopping.jsp
MonHang cannot be resolved to a type
82:
83: ArrayList ds=cart.getGH();
84:
85: for(MonHang mh:ds){
86:
87: SanPham sp=db.getSanPham(mh.getMsMH());
88:
An error occurred at line: 87 in the jsp file: /shopping.jsp
SanPham cannot be resolved to a type
84:
85: for(MonHang mh:ds){
86:
87: SanPham sp=db.getSanPham(mh.getMsMH());
88:
89: %>
90:
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:321)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:328)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.33 logs.
Apache Tomcat/6.0.33
Võ Văn Hải said
Em xem lại cách tổ chức code (các pakages). Lỗi chưa biên dịch được chứ không phải lỗi runtime.
Võ Văn Hải said
copy/paste thôi.
Minh said
Em củng bị lổi như Tuấn thầy nói rỏ hơn được ko
Minh said
em củng tổ chức code như hình thầy để ở trên nhưng vẫn lỗi
Huy Cường said
Có cần thiết 2 class monhang va sanpham không thầy em thấy 2 lớp này cũng tương tự nhau…..
Tuân said
Thưa thầy em chạy nó báo lỗi
Bạn đã nhập liệu sai!
Hãy nhập lại
java.lang.NullPointerException
ron said
em đã làm đúng hết hướng dẫn trên của thầy, khi chạy shopping.jsp thì nó chuyển ngay sang trang loi.jsp mặc dù chưa thao tác gì cả. Lỗi này là lỗi nào hả thầy
Dũng said
sao anh không upload bài đề mô của anh cho mọi người xem ?
Lân said
thầy cho em hỏi bài giỏ hàng jsp của thầy chạy trên trình duyệt IE thì được còn trên Firefox thì ngay phần chọn sản phẩm không hiện mặt hàng lên được ạ.
Võ Văn Hải said
“thầy cho em hỏi bài giỏ hàng jsp của thầy chạy trên trình duyệt IE thì được còn trên Firefox thì ngay phần chọn sản phẩm không hiện mặt hàng lên được ạ.”
Bạn sử lại chút ngay chỗ trang này
Chọn sản phẩm:<select name="ms"> <% XulyDB db=new XulyDB(); ResultSet rs=db.getAllProducts(); while(rs.next()){ %> <option value='<%=rs.getString("mssp") %>'> <%=rs.getString("tensp") %> </option> <% } %> </select>HTTM said
Thầy ơi cho em hỏi??
OK, như vậy phần code xong rồi! Bạn tiến hành biên dịch và đóng gói nó lại.
Bạn tạo 1 file có tên build.bat với nội dung sau
javac -d . -encoding UTF-8 *.java
pause
Thực thi file này, đảm bảo bạn không nhìn thấy lỗi nào. Sau đó tiến hành đóng gói nó, bạn tạo file pack.bat có nội dung sau:
jar -cvf shop.jar www/vvh/com/spc/*.class www/vvh/com/db/*.class
pause
Thực thi file này, bạn sẽ có file shop.jar, copy file này vào thư mục lib.
Tại sao ở chỗ nay của em chạy build.bat nó không hiểu. và e cũng gõ thử trong CMD cú pháp javac mà nó cũng không hiểu. Thây giúp em.
Võ Văn Hải said
“…Tại sao ở chỗ nay của em chạy build.bat nó không hiểu. và e cũng gõ thử trong CMD cú pháp javac mà nó cũng không hiểu. Thây giúp em.”
Em phải cấu hình command-line.