数据库信息:
Goods类:
package com.nai.bean;
public class Goods {
private Integer gid;
private String gname;
private Double price;
private String mark;
public Goods(Integer gid, String gname, Double price, String mark) {
this.gid = gid;
this.gname = gname;
this.price = price;
this.mark = mark;
}
public Goods() {
}
public Integer getGid() {
return gid;
}
@Override
public String toString() {
return "Goods{" +
"gid=" + gid +
", gname='" + gname + '\'' +
", price='" + price + '\'' +
", mark='" + mark + '\'' +
'}';
}
public void setGid(Integer gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
}
GoodsDaoImpl类:
package com.nai.Dao.impl;
import com.nai.bean.Goods;
import com.nai.bean.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class GoodsDaoImpl {
private static String jdbcdirver = "com.mysql.cj.jdbc.Driver";
private static String jdbcurl = "jdbc:mysql://localhost:3306/myweb?useSSL=false&serverTimezone=UTC";
private static String jdbcuser = "root";
private static String jdbcpassword = "root";
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
User Fi = null;
int row = 0;
public List selectAll() {
List goodsList = new ArrayList<>();
try {
Class.forName(jdbcdirver);
con = DriverManager.getConnection(jdbcurl, jdbcuser, jdbcpassword);
String sql = "select * from t_goods";
pstm = con.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
//从结果集中获取数据,封装到Goods对象中
Goods goods = new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setPrice(rs.getDouble("price"));
goods.setMark(rs.getString("mark"));
//把当前行对应的对象存储到集合中
goodsList.add(goods);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return goodsList;
}
}
Login(登录)类:
package com.nai.servlet;
import com.nai.Dao.UserDao;
import com.nai.Dao.impl.UserDaoImpl;
import com.nai.bean.User;
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;
@WebServlet("/Login")
public class Login extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Login-get...");
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求和响应的编码格式,以及响应的格式
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charest=UTF-8");
//获取请求的参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//执行业务处理---jdbc的操作
UserDaoImpl userDao= new UserDaoImpl();
User Fi=userDao.login(username,password);
//判断登录的用户信息是否为空
if(Fi!=null){
req.getRequestDispatcher("selectAllGoods").forward(req,resp);
}else {
resp.sendRedirect("user.jsp");
}
}
}
1.后端在作用域对象中存储数据;
2.在页面中使用EL表达式取出数据;3.可以使用JSTL标签进行集合元素的遍历
selectAllGoods类:
package com.nai.servlet;
import com.nai.Dao.impl.GoodsDaoImpl;
import com.nai.bean.Goods;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
@WebServlet("/selectAllGoods")
public class servletAllGoods extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//查询所有商品的信息
GoodsDaoImpl goodsDao=new GoodsDaoImpl();
List goodsList=goodsDao.selectAll();
System.out.println(goodsList);
//把数据传递到前端页面
//通过request获取session对象,该对象可以向前端传输数据的容器
HttpSession session =request.getSession();
//向session中存入商品信息集合
session.setAttribute("goodsList",goodsList);
//登录成功,跳转到上面
response.sendRedirect("goods.jsp");
}
}
******
JDBCUntil类:
package com.nai.until;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class JDBCUtil {
private static String jdbcdirver="com.mysql.cj.jdbc.Driver";
private static String jdbcurl="jdbc:mysql://localhost:3306/myweb?useSSL=false&serverTimezone=UTC";
private static String jdbcuser="root";
private static String jdbcpassword="root";
private static Connection connection=null;
public static Connection getCon(){
try {
Class.forName(jdbcdirver);
connection= DriverManager.getConnection(jdbcurl,jdbcuser,jdbcpassword);
}catch (Exception e){
e.printStackTrace();
}
return connection;
}
public static void close( PreparedStatement pstm, Connection con){
try{
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
public static Connection getCo(){
try {
Class.forName(jdbcdirver);
connection= DriverManager.getConnection(jdbcurl,jdbcuser,jdbcpassword);
}catch (Exception e){
e.printStackTrace();
}
return connection;
}
public static void login(ResultSet rs, PreparedStatement pstm, Connection con){
try{
if(rs!=null){
rs.close();
}
if(pstm!=null){
pstm.close();
}
if(con!=null){
con.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
index.jsp:
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/2/14
Time: 10:52
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
欢迎访问web商店
欢迎访问web商店
去登录
去注册
goods.jsp:
<%@ page import="java.util.List" %>
<%@ page import="com.nai.bean.Goods" %><%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/2/16
Time: 11:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登陆成功
登录成功
欢迎来到javaweb商店
商品编号
商品名称
商品价格
商品说明
<%
//获取session
HttpSession session1=request.getSession();
//从session中取出数据
List goodsList = (List) session1.getAttribute("goodsList");
//遍历集合元素
for(Goods goods : goodsList){
%>
<%=goods.getGid()%>
<%=goods.getGname()%>
<%=goods.getPrice()%>
<%=goods.getMark()%>
<%
}
%>
login.jsp:
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2023/2/14
Time: 11:00
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登录
*****
最终效果图: