【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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

第10章 中断和动态时钟显示

第10章 中断和动态时钟显示 从本章开始,按照书籍的划分,第10章开始就进入保护模式(Protected Mode)部分了,感觉从这里开始难度突然就增加了。 书中介绍了为什么有中断(Interrupt)的设计,中断的几种方式:外部硬件中断、内部中断和软中断。通过中断做了一个会走的时钟和屏幕上输入字符的程序。 我自己理解中断的一些作用: 为了更好的利用处理器的性能。协同快速和慢速设备一起工作