开始从JSP页面请求servlet,并带回参数,后台将参数存入到数据库
1.后台给前端返回用户ID。将来用户ID、商品名称、商品图片路径、商品单价、商品数量,保存在数据库的购物车表里面。
工具类修改一下
package com.servlet;
import java.sql.*;
public class Login {
static int id ;
static final String url = "jdbc:mysql://111.112.241.180:3306/testsys?useUnicode=true&characterEncoding=utf-8&useSSL=false";
static final String username = "root";//数据库名称
static final String password = "******";//访问数据库密码
public static int login(String userId, String pwd) throws SQLException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");// 加载数据库驱动,注册到驱动管理器
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url,username,password);// 创建Connection连接
stmt = conn.createStatement();// 获取Statement(获得发送sql的对象)
String sql = "select * from com_user where userId = '"+ userId+"'";
rs = stmt.executeQuery(sql); // 执行查询
boolean bool = rs.first();
if(bool == false){
rs.close(); // 关闭ResultSet
stmt.close(); // 关闭Statement
conn.close();// 关闭Connection
return 0;
}
String uname = rs.getString("userName");
System.out.println("用户密码"+ uname);
if(pwd.equals(uname)){
id = rs.getInt(1);
rs.close(); // 关闭ResultSet
stmt.close(); // 关闭Statement
conn.close();// 关闭Connection
return 1;
}else {
rs.close(); // 关闭ResultSet
stmt.close(); // 关闭Statement
conn.close();// 关闭Connection
return 10;
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
class test extends Login{
public static int LogId(){
Login l =new test();
int i = l.id;
return i;
}
}
我这里练习了一下继承,子类继承了父类的方法和属性
红框里面就是将id保存在session中
package com.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.*;
import java.sql.SQLException;
@WebServlet(name = "HelloServlet", value = "/HelloServlet")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
String userId = request.getParameter("Eamil").toString();
String pwd = request.getParameter("Password").toString();
System.out.println(userId);
System.out.println(pwd);
// PrintWriter out = response.getWriter();
// out.println(request.getParameter("first_name"));
// String title = "Using GET Method to Read Form Data";
// String docType =
// "<!doctype html public \"-//w3c//dtd html 4.0 " +
// "transitional//en\">\n";
// out.println(docType +
// "<html>\n" +
// "<head><title>" + title + "</title></head>\n" +
// "<body bgcolor=\"#f0f0f0\">\n" +
// "<h1 align=\"center\">" + title + "</h1>\n" +
// "<ul>\n" +
// " <li><b>名字</b>:"
// + request.getParameter("first_name") + "\n" +
// " <li><b>姓氏</b>:"
// + request.getParameter("last_name") + "\n" +
// "</ul>\n" +
// "</body></html>");
Login log = new Login();
test t = new test();
try {
int num = log.login(userId,pwd);
if(num == 0){
request.setAttribute("Msg", "用户名错误");
request.getRequestDispatcher("index.jsp").forward(request, response);
}else if(num == 10){
request.setAttribute("Msg", "密码错误");
request.getRequestDispatcher("index.jsp").forward(request, response);
} else if (num == 1) {
int id = t.LogId();
System.out.println("用户的ID是"+id);
request.getSession().setAttribute("userId",id);
request.getRequestDispatcher("JSP/Product.jsp").forward(request, response);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
前台获取ID
JSP页面获取参数
点击加入购物车按钮调用同一个方法 ,注意每件商品的ID是唯一的,方法传入的参数就是每件商品唯一的id
<%--
Created by IntelliJ IDEA.
User: JIAHANG
Date: 2023/1/30
Time: 16:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>购物页</title>
<link rel="stylesheet" href="/untitled01_war_exploded/css/product.css" />
<script type="text/javascript" src="/untitled01_war_exploded/js/product.js"></script>
<script type="text/javascript">
window.onload = function () {
var tableLine = document.getElementById("number");
for (var i = 0; i < tableLine.rows.length; i++) {
tableLine.rows[i].cells[0].innerHTML = (i + 1);
}
}
</script>
</head>
<body>
<%--<%@include file="/JSP/Navbar.jsp"%>--%>
<div class="foot" id="foot">
<%-- <label class="fl select-all"><input type="checkbox" class="check-all check"/> 全选</label>--%>
<%-- <a class="fl delete" id="deleeAll" href="././JSP/Shopping.jsp">购物车</a>--%>
<div class="fr closing" ><a class="fr closing" id="#" href="/untitled01_war_exploded/index.jsp">退出</a></div>
<div class="fr total"><span id="priceTotal"></span></div>
<div class="fr selected" id="selected">
<%-- <span id="selectedTotal">0</span>件--%>
<%-- <span class="arrow up">︽</span>--%>
<%-- <span class="arrow down">︾</span>--%>
<a class="fl delete" id="deleeAll" href="/untitled01_war_exploded/JSP/Shopping.jsp">购物车</a>
</div>
<div class="selected-view">
<div id="selectedViewList" class="clearfix">
<!--<div><img src="images/1.jpg"><span>取消选择</span></div>-->
</div>
<span class="arrow">◆<span>◆</span></span>
</div>
</div>
<table id="cartTable">
<thead>
<tr>
<th>序号</th>
<th>商品</th>
<th>单价</th>
<th>详情</th>
<th>操作</th>
</tr>
</thead>
<tbody id="number">
<tr>
<td class="checkbox"></td>
<td class="goods"><img src="/untitled01_war_exploded/images/1-1.jpg" alt="" id="img1"/><span id="name1">Casio/卡西欧 EX-TR350</span></td>
<td class="price" id="td1">5999.88</td>
<td class="count">
D:\TOMCAT\apache-tomcat-8.5.85\bin\catalina.bat run
[2023-01-31 03:58:08,262] Artifact untitled01: Waiting for server connection to start
Using CATALINA_BASE: "C:\Users\JIAHANG\AppData\Local\JetBrains\IntelliJIdea2022.3\
Using CATALINA_HOME: "D:\TOMCAT\apache-tomcat-8.5.85"
Using CATALINA_TMPDIR: "D:\TOMCAT\apache-tomcat-8.5.85\temp"
</td>
<td class="operation"><span class="delete" onclick="proAdd(img1,name1,td1)">加入购物车</span></td>
</tr>
<tr>
<td class="checkbox"></td>
<td class="goods"><img src="/untitled01_war_exploded/images/1-2.jpg" alt="" id="img2"/><span id="name2">Canon/佳能 PowerShot SX50 HS</span></td>
<td class="price" id="td2" >3888.50</td>
<td class="count">
D:\TOMCAT\apache-tomcat-8.5.85\bin\catalina.bat run
[2023-01-31 03:58:08,262] Artifact untitled01: Waiting for server connection to start
Using CATALINA_BASE: "C:\Users\JIAHANG\AppData\Local\JetBrains\IntelliJIdea2022.3\
Using CATALINA_HOME: "D:\TOMCAT\apache-tomcat-8.5.85"
Using CATALINA_TMPDIR: "D:\TOMCAT\apache-tomcat-8.5.85\temp"
</td>
<td class="operation"><span class="delete" onclick="proAdd(img2,name2,td2)">加入购物车</span></td>
</tr>
<tr>
<td class="checkbox"></td>
<td class="goods"><img src="/untitled01_war_exploded/images/1-3.jpg" alt="" id="img3"/><span id="name3">Sony/索尼 DSC-WX300</span></td>
<td class="price" id="td3">1428.50</td>
<td class="count">
D:\TOMCAT\apache-tomcat-8.5.85\bin\catalina.bat run
[2023-01-31 03:58:08,262] Artifact untitled01: Waiting for server connection to start
Using CATALINA_BASE: "C:\Users\JIAHANG\AppData\Local\JetBrains\IntelliJIdea2022.3\
Using CATALINA_HOME: "D:\TOMCAT\apache-tomcat-8.5.85"
Using CATALINA_TMPDIR: "D:\TOMCAT\apache-tomcat-8.5.85\temp"
</td>
<td class="operation"><span class="delete" onclick="proAdd(img3,name3,td3)">加入购物车</span></td>
</tr>
<tr>
<td class="checkbox"></td>
<td class="goods"><img src="/untitled01_war_exploded/images/1-4.jpg" alt="" id="img4"/><span id="name4">Fujifilm/富士 instax mini 25</span></td>
<td class="price" id="td4">640.60</td>
<td class="count">
D:\TOMCAT\apache-tomcat-8.5.85\bin\catalina.bat run
[2023-01-31 03:58:08,262] Artifact untitled01: Waiting for server connection to start
Using CATALINA_BASE: "C:\Users\JIAHANG\AppData\Local\JetBrains\IntelliJIdea2022.3\
Using CATALINA_HOME: "D:\TOMCAT\apache-tomcat-8.5.85"
Using CATALINA_TMPDIR: "D:\TOMCAT\apache-tomcat-8.5.85\temp"
</td>
<td class="operation"><span class="delete" onclick="proAdd(img4,name4,td4)">加入购物车</span></td>
</tr>
</tbody>
</table>
<%--<a href="././JSP/Shopping.jsp">购物车</a>--%>
<%
int userid = (int) session.getAttribute("userId");
%>
<script type="text/javascript">
const userid = "<%=userid%>";
function proAdd(src,name,price1){
//const img1 = document.getElementById(src);
const src1 = src.getAttribute("src")
//const imgname = document.getElementById(name);
const nametext = name.innerText;
//const pric = document.getElementById(price1);
const price = price1.innerText;
//alert(src1+nametext+price);
};
</script>
</body>
</html>