include指令:<%@ include file="URL"%>
<body>
<h1>include动作</h1>
<hr/>
<jsp:include page="cookie.jsp" flush="false"/>
</body>
<h1>include指令</h1>
<jsp:include file="cookie.jsp">
<body>
<h1>include动作</h1>
<hr/>
<jsp:include page="cookie.jsp" flush="false"/>
</body>
<h1>include指令</h1>
<jsp:include file="cookie.jsp">
include指令与include动作比较
param动作<jsp:param name="参数名" value="参数值"/>
<form name="loginForm" action="dologin.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="<%=username%>"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" value="<%=password%>"/></td>
</tr>
<tr>
<td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"value="取消">十天内记住我的登录状态</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="登录"/></td>
<td colspan="2" align="center"><input type="reset" value="取消"/></td>
</tr>
</table>
</form>
</body>
dologin.jsp
<jsp:forward page="include.jsp">
<jsp:param value="admin@123.net" name="email"/>
<!-- 可以添加新的参数,也可以修改原有的参数 -->
</jsp:forward>
</body>
include.jsp
<%
request.setCharacterEncoding("utf-8");
String username="";
String password="";
String email="";
if(request.getParameter("username")!=null){
username=request.getParameter("username");
}
if(request.getParameter("password")!=null){
password=request.getParameter("password");
}
if(request.getParameter("email")!=null){
email=request.getParameter("email");
}
%>
用户名:<%=username %><br/>
密码:<%=password %><br/>
电子邮箱:<%=email %>
</body>
detail.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.ItemsDAO" %>
<%@ page import="com.Items" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
<tr>
<%
ItemsDAO itemDao=new ItemsDAO();
Items item=itemDao.getItemsById(Integer.parseInt(request.getParameter("id")));
if(item!=null){
%>
<td width="70%" valign="top">
<table>
<tr>
<td rowspan="4"><img src="<%=item.getPicture() %>" width="200" height="160"></td>
</tr>
<tr>
<td><b><%=item.getName() %></b></td>
</tr>
<tr>
<td>产地:<%=item.getCity() %></td>
</tr>
<tr>
<td>价格:<%=item.getPrice() %></td>
</tr>
</table>
</td>
<%
}
%>
<%
String list="";
//从客户端获得cookie集合
Cookie[] cookies=request.getCookies();
//遍历集合
for(Cookie c:cookies){
if(c.getName().equals("ListViewCookie")){
list=c.getValue();
}
}
//如果浏览记录超过1000条清零
list+=request.getParameter("id")+",";
String[] arr=list.split(",");
if(arr!=null&&arr.length>0){
if(arr.length>=1000){
list="";
}
}
Cookie cookie=new Cookie("ListViewCookie",list);
%>
</table>
</center>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*" %>
<%@ page import="com.ItemsDAO" %>
<%@ page import="com.Items" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<H1>商品展示</H1>
<hr/>
<center>
<table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<%
ItemsDAO itemsDao=new ItemsDAO();
ArrayList<Items> list=itemsDao.getAllItems();
if(list!=null&&list.size()>0){
for(int i=0;i<list.size();i++){
Items item=list.get(i);
%>
<div>
<dl>
<dt>
<a href="details.jsp?id=<%=item.getId()%>"><img src="<%=item.getPicture() %>" width="120" height="120"></a>
</dt>
<dd class="dd_name"><%=item.getName() %></dd>
<dd class="dd_city">产地:<%=item.getCity() %> 价格:<%=item.getPrice() %></dd>
</dl>
</div>
<%
}
}
%>
</td>
</tr>
</table>
</center>
</body>
</html>
DBHelper.java
public class DBHelper {
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/shopping";
private static final String username="root";
private static final String password="";
private static Connection conn=null;
static{
try{
Class.forName(driver);
}catch(Exception e){
}
}
public static Connection getConnection() throws SQLException{
//单例模式,返回数据库链接对象
if(conn==null){
conn=DriverManager.getConnection(url,username,password);
return conn;
}
return conn;
}
public static void main(String[] args) throws SQLException{
Connection conn=DBHelper.getConnection();
if(conn!=null){
System.out.println("数据库链接成功");
}else{
System.out.println("数据库链接失败");
}
}
}
Items.java
public class Items {
private int id;
private String name;
private String city;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
private int price;
private int number;
private String picture;
}
ItemsDAO.java
public class ItemsDAO {
//获得所有商品信息
public ArrayList<Items> getAllItems(){
java.sql.Connection conn=null;
java.sql.PreparedStatement stmt=null;
ResultSet rs=null;
ArrayList<Items>list=new ArrayList<Items>();
try{
conn=DBHelper.getConnection();
String sql="select *from items";
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
while(rs.next()){
Items item=new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setNumber(rs.getInt("number"));
item.setPrice(rs.getInt("price"));
item.setPicture(rs.getString("picture"));
list.add(item);
}
return list;
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
}
}
//根据商品编号获得商品资料
public Items getItemsById(int id){
java.sql.Connection conn=null;
java.sql.PreparedStatement stmt=null;
ResultSet rs=null;
//ArrayList<Items> list=new ArrayList<Items>();
try{
conn=DBHelper.getConnection();
String sql="select *from items where id=?;";
stmt=conn.prepareStatement(sql);
stmt.setInt(1, id);
rs=stmt.executeQuery();
if(rs.next()){
Items item=new Items();
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setNumber(rs.getInt("number"));
item.setPrice(rs.getInt("price"));
item.setPicture(rs.getString("picture"));
return item;
}
else{
return null;
}
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
}
}
//获取最近浏览的5条信息
public ArrayList<Items> getViewList(String list){
ArrayList<Items>itemlist=new ArrayList<Items>();
int iCount=5;
if(list!=null&&list.length()>0){
String[] arr=list.split(",");
for(String s:arr){
int id=Integer.parseInt(s);
itemlist.add(getItemsById(id));
}
return itemlist;
}
else{
return null;
}
}}