JAVA WEB 中间件为SERVLET(六)
生活随笔
收集整理的這篇文章主要介紹了
JAVA WEB 中间件为SERVLET(六)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
開始從JSP頁(yè)面請(qǐng)求servlet,并帶回參數(shù),后臺(tái)將參數(shù)存入到數(shù)據(jù)庫(kù)
1.后臺(tái)給前端返回用戶ID。將來(lái)用戶ID、商品名稱、商品圖片路徑、商品單價(jià)、商品數(shù)量,保存在數(shù)據(jù)庫(kù)的購(gòu)物車表里面。
工具類修改一下
?
?
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";//數(shù)據(jù)庫(kù)名稱static final String password = "******";//訪問數(shù)據(jù)庫(kù)密碼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");// 加載數(shù)據(jù)庫(kù)驅(qū)動(dòng),注冊(cè)到驅(qū)動(dòng)管理器Class.forName("com.mysql.jdbc.Driver");conn = DriverManager.getConnection(url,username,password);// 創(chuàng)建Connection連接stmt = conn.createStatement();// 獲取Statement(獲得發(fā)送sql的對(duì)象)String sql = "select * from com_user where userId = '"+ userId+"'";rs = stmt.executeQuery(sql); // 執(zhí)行查詢boolean bool = rs.first();if(bool == false){rs.close(); // 關(guān)閉ResultSetstmt.close(); // 關(guān)閉Statementconn.close();// 關(guān)閉Connectionreturn 0;}String uname = rs.getString("userName");System.out.println("用戶密碼"+ uname);if(pwd.equals(uname)){id = rs.getInt(1);rs.close(); // 關(guān)閉ResultSetstmt.close(); // 關(guān)閉Statementconn.close();// 關(guān)閉Connectionreturn 1;}else {rs.close(); // 關(guān)閉ResultSetstmt.close(); // 關(guān)閉Statementconn.close();// 關(guān)閉Connectionreturn 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;} }?我這里練習(xí)了一下繼承,子類繼承了父類的方法和屬性
紅框里面就是將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 {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 設(shè)置響應(yīng)內(nèi)容類型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", "用戶名錯(cuò)誤");request.getRequestDispatcher("index.jsp").forward(request, response);}else if(num == 10){request.setAttribute("Msg", "密碼錯(cuò)誤");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);}}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }?前臺(tái)獲取ID
?JSP頁(yè)面獲取參數(shù)
?
點(diǎn)擊加入購(gòu)物車按鈕調(diào)用同一個(gè)方法?,注意每件商品的ID是唯一的,方法傳入的參數(shù)就是每件商品唯一的id
<%--Created by IntelliJ IDEA.User: JIAHANGDate: 2023/1/30Time: 16:34To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>購(gòu)物頁(yè)</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">購(gòu)物車</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">購(gòu)物車</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>序號(hào)</th><th>商品</th><th>單價(jià)</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 startUsing 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)">加入購(gòu)物車</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 startUsing 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)">加入購(gòu)物車</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 startUsing 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)">加入購(gòu)物車</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 startUsing 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)">加入購(gòu)物車</span></td></tr></tbody> </table> <%--<a href="././JSP/Shopping.jsp">購(gòu)物車</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>總結(jié)
以上是生活随笔為你收集整理的JAVA WEB 中间件为SERVLET(六)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 录屏软件录制内部声音,图文教程,快速解决
- 下一篇: FreeMarker模板语言开发(整理版