【javaweb:jsp】从服务器获取动态商品信息并展示在jsp中

2024-04-18 12:48

本文主要是介绍【javaweb:jsp】从服务器获取动态商品信息并展示在jsp中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、servlet代码(用于从数据库中获取商品信息list)

package indi.product;import java.io.IOException;
import java.sql.SQLException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;import indi.domain.Product;
import indi.utils.DataSourceUtils;
/*** 从服务器中获取动态商品信息,并输出到jsp中展示,便于商品信息的变动和更新。* @author Administrator**/
public class ProductListServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//1.从数据库中获取商品信息QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());String sql="select * from product";List<Product> productList =null;try {productList = qr.query(sql, new BeanListHandler<Product>(Product.class));} catch (SQLException e) {e.printStackTrace();}//2.利用request域存储商品信息,采用请求转发的方法转发给product_list.jsprequest.setAttribute("productInfo", productList);request.getRequestDispatcher("/product_list.jsp").forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}
}
2、jsp代码(用于展示服务器中的商品信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<%--下面是从项目中导入的java包--%>
<%@ page import="java.util.*" %>
<%@ page import="indi.domain.*" %>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" /><style>
body {margin-top: 20px;margin: 0 auto;width: 100%;
}.carousel-inner .item img {width: 100%;height: 300px;
}
</style>
</head><body><!-- 引入header.jsp --><jsp:include page="/header.jsp"></jsp:include><div class="row" style="width: 1210px; margin: 0 auto;"><div class="col-md-12"><ol class="breadcrumb"><li><a href="#">首页</a></li></ol></div><%//获取request域中传递的参数List<Product> productList=(List<Product>)request.getAttribute("productInfo");//将获取的商品信息打印在网页上for(Product product:productList){out.write("<div class='col-md-2'style='height:250px'>");out.write("<a href='product_info.htm'> <img src='"+product.getPimage()+"'");out.write("width='170' height='170' style='display: inline-block;'>");out.write("</a>");out.write("<p>");out.write("<a href='product_info.html' style='color: green'>"+product.getPname()+"</a>");out.write("</p>");out.write("<p>");out.write("<font color='#FF0000'>商城价:¥"+product.getShop_price()+"</font>");out.write("</div>");}%></div><!--分页 --><div style="width: 380px; margin: 0 auto; margin-top: 50px;"><ul class="pagination" style="text-align: center; margin-top: 10px;"><li class="disabled"><a href="#" aria-label="Previous"><spanaria-hidden="true">«</span></a></li><li class="active"><a href="#">1</a></li><li><a href="#">2</a></li><li><a href="#">3</a></li><li><a href="#">4</a></li><li><a href="#">5</a></li><li><a href="#">6</a></li><li><a href="#">7</a></li><li><a href="#">8</a></li><li><a href="#">9</a></li><li><a href="#" aria-label="Next"> <span aria-hidden="true">»</span></a></li></ul></div><!-- 分页结束 --><!--商品浏览记录--><divstyle="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;"><h4 style="width: 50%; float: left; font: 14px/30px 微软雅黑">浏览记录</h4><div style="width: 50%; float: right; text-align: right;"><a href="">more</a></div><div style="clear: both;"></div><div style="overflow: hidden;"><ul style="list-style: none;"><listyle="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><imgsrc="products/1/cs10001.jpg" width="130px" height="130px" /></li></ul></div></div><!-- 引入footer.jsp --><jsp:include page="/footer.jsp"></jsp:include></body></html>



这篇关于【javaweb:jsp】从服务器获取动态商品信息并展示在jsp中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/914834

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

线上Java OOM问题定位与解决方案超详细解析

《线上JavaOOM问题定位与解决方案超详细解析》OOM是JVM抛出的错误,表示内存分配失败,:本文主要介绍线上JavaOOM问题定位与解决方案的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一、OOM问题核心认知1.1 OOM定义与技术定位1.2 OOM常见类型及技术特征二、OOM问题定位工具

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏