【javaweb:jsp】从服务器获取动态商品信息并展示在jsp中
2018-01-28 21:05
429 查看
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.jsp
request.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"><span
aria-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>
<!-- 分页结束 -->
<!--商品浏览记录-->
<div
style="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;">
<li
style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
</ul>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</body>
</html>
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.jsp
request.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"><span
aria-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>
<!-- 分页结束 -->
<!--商品浏览记录-->
<div
style="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;">
<li
style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
</ul>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</body>
</html>
相关文章推荐
- Java从服务器上获取时间,动态在jsp页面显示
- Java从服务器上获取时间动态显示在jsp页面实现思路
- JavaWeb -- Jsp , Jsp中动态获取项目路径
- Java从服务器上获取时间,动态在jsp页面显示
- Java从服务器上获取时间动态显示在jsp页面实现思路
- Java JSP获取访问连接来源及服务器获取访问者信息
- Java,jsp 获取jdk、系统、服务器等信息
- Java从服务器上获取时间,动态在jsp页面显示
- Tomcat服务器的配置(java web、jsp)
- 获取Web服务器所处局域网的网上邻居信息
- jsp页面获取服务器信息
- Java 获取系统信息和服务器信息
- java获取服务器、工程路径等相关信息System.getProperty("user.dir")
- 用定制的PHP应用程序来获取Web服务器的状态信息
- java web 动态服务器
- 基于Web过程模拟的动态Web信息获取
- java获取服务器一些信息的方法(服务器地址/相对路径/端口/项目名字)
- Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之15.Servlet响应头信息 推荐
- 基于Web过程模拟的动态Web信息获取
- 用定制的PHP应用程序来获取Web服务器的状态信息