电子商务系统的一些代码
(一)、经常用的过滤器
1
package
com.ascent.util;
2
3 import java.io.IOException;
4 import javax.servlet.Filter;
5 import javax.servlet.FilterChain;
6 import javax.servlet.FilterConfig;
7 import javax.servlet.ServletException;
8 import javax.servlet.ServletRequest;
9 import javax.servlet.ServletResponse;
10 import javax.servlet.UnavailableException;
11
12 /** */ /**
13 * Example filter that sets the character encoding to be used in parsing the
14 * incoming request
15 */
16 public class SetCharacterEncodingFilter implements Filter {
17
18 /** *//**
19 * Take this filter out of service.
20 */
21 public void destroy() {
22 }
23 /** *//**
24 * Select and set (if specified) the character encoding to be used to
25 * interpret request parameters for this request.
26 */
27 public void doFilter(ServletRequest request, ServletResponse response,
28 FilterChain chain)throws IOException, ServletException {
29
30 request.setCharacterEncoding("gb2312");
31
32 // 传递控制到下一个过滤器
33 chain.doFilter(request, response);
34 }
35
36 public void init(FilterConfig filterConfig) throws ServletException {
37 }
38}
39
2
3 import java.io.IOException;
4 import javax.servlet.Filter;
5 import javax.servlet.FilterChain;
6 import javax.servlet.FilterConfig;
7 import javax.servlet.ServletException;
8 import javax.servlet.ServletRequest;
9 import javax.servlet.ServletResponse;
10 import javax.servlet.UnavailableException;
11
12 /** */ /**
13 * Example filter that sets the character encoding to be used in parsing the
14 * incoming request
15 */
16 public class SetCharacterEncodingFilter implements Filter {
17
18 /** *//**
19 * Take this filter out of service.
20 */
21 public void destroy() {
22 }
23 /** *//**
24 * Select and set (if specified) the character encoding to be used to
25 * interpret request parameters for this request.
26 */
27 public void doFilter(ServletRequest request, ServletResponse response,
28 FilterChain chain)throws IOException, ServletException {
29
30 request.setCharacterEncoding("gb2312");
31
32 // 传递控制到下一个过滤器
33 chain.doFilter(request, response);
34 }
35
36 public void init(FilterConfig filterConfig) throws ServletException {
37 }
38}
39
(二)、购物车类代码
package
com.ascent.util;
import java.util.HashMap;
import com.ascent.po.Product;
public class ShopCart {
private HashMap<String, Product> hashMap;
public HashMap<String, Product> getHashMap() {
return hashMap;
}
public void setHashMap(HashMap<String, Product> hashMap) {
this.hashMap = hashMap;
}
@SuppressWarnings("unchecked")
public ShopCart(){
hashMap = new HashMap();
}
/** *//**
* 检查hashmap中是否有了该pid对应的对象方法
* @param pid
* @return true:有 false:无
*/
public boolean checkPid(String pid){
if(hashMap.containsKey(pid)){
return true;
}else{
return false;
}
}
/** *//**
* 在上面方法返回false情况下添加product
* @param pid
* @param product
*/
public void addProduct(String pid,Product product){
hashMap.put(pid, product);
}
/** *//**
* 根据id删除hashmap中的product
* @param pid
*/
public void delProduct(String pid){
hashMap.remove(pid);
}
/** *//**
* 修改hashmap中pid对应product的质量quantity
* @param pid
* @param quantity
*/
public void updateQuantity(String pid,String quantity){
hashMap.get(pid).setQuantity(quantity);
}
/** *//**
* 清除购物车
*/
public void emptyCart(){
this.getHashMap().clear();
}
}
import java.util.HashMap;
import com.ascent.po.Product;
public class ShopCart {
private HashMap<String, Product> hashMap;
public HashMap<String, Product> getHashMap() {
return hashMap;
}
public void setHashMap(HashMap<String, Product> hashMap) {
this.hashMap = hashMap;
}
@SuppressWarnings("unchecked")
public ShopCart(){
hashMap = new HashMap();
}
/** *//**
* 检查hashmap中是否有了该pid对应的对象方法
* @param pid
* @return true:有 false:无
*/
public boolean checkPid(String pid){
if(hashMap.containsKey(pid)){
return true;
}else{
return false;
}
}
/** *//**
* 在上面方法返回false情况下添加product
* @param pid
* @param product
*/
public void addProduct(String pid,Product product){
hashMap.put(pid, product);
}
/** *//**
* 根据id删除hashmap中的product
* @param pid
*/
public void delProduct(String pid){
hashMap.remove(pid);
}
/** *//**
* 修改hashmap中pid对应product的质量quantity
* @param pid
* @param quantity
*/
public void updateQuantity(String pid,String quantity){
hashMap.get(pid).setQuantity(quantity);
}
/** *//**
* 清除购物车
*/
public void emptyCart(){
this.getHashMap().clear();
}
}
(三)、分页算法
//
分页类
package com.ascent.util;
import java.util. * ;
/** */ /**
* @author Administrator
* @version 负责页面控制的 JavaBean
*/
public class PageBean {
public int currentPage; // 当前页数
public int totalPage; // 总页数
public int totalRows; // 总行数
public int rowsPage = 5; // 每页显示多少行
public ArrayList data; // 封装页面显示的数据
public PageBean(){}
public void countTotalPage(){ // 计算总页数
if(totalRows%rowsPage==0){
this.totalPage = totalRows/rowsPage;
}
else{
this.totalPage = totalRows/rowsPage + 1;
}
}
public ArrayList getData(){
return data;
}
public PageBean(int totalRows){
this.totalRows = totalRows;
this.countTotalPage();
}
}
// 分页算法
public String guestPageShow() throws Exception {
this.pageReturn();
return "guestproductsshow";
}
@SuppressWarnings( " unchecked " )
private void pageReturn() {
String jump_page = this.getJumpPage();
if (jump_page == null) {
jump_page = "1";
}
PageBean page = this.listData(jump_page);
ActionContext.getContext().getSession().put("product_page_list", page);
this.setDataList(page.getData());
}
public PageBean listData(String number) {
PageBean page = new PageBean(productService.getTotalRows());
int num = Integer.parseInt(number);
String sql = "from Product p where delFlag=0 order by p.pid";
page.data = productService.getData(sql, page.rowsPage * (num - 1),
page.rowsPage);
page.currentPage = num;
return page;
}
// 分页算法
// 实现
PageBean page = this .listData( " 1 " );
ActionContext.getContext().getSession().put( " productuser_page_list " , page);
this .setDataList(page.getData());
// jsp方面
<%
PageBean pBean = (PageBean)session.getAttribute( " productuser_page_list " );
%>
<%
if (pBean.totalPage != 1 ) {
%>
<form name="pageForm" action="pageProductuserManagerAction.action" method="post">
<%@ include file="page.jsp" %>
</form>
<%} %>
// page.jsp
<% @ page language = " java " import = " java.util.* " pageEncoding = " gb2312 " %>
< script language = " JavaScript " >
function Jumping() {
document.pageForm.submit();
}
function gotoPage(pagenum) {
//alert(pagenum);
//alert(document.pageForm.jumpPage.value);
document.pageForm.jumpPage.value = pagenum;
document.pageForm.submit();
}
</ script >
< table >
< tr >
< td > 每页 <%= pBean.rowsPage %> 行 </ td >
< td > 共 <%= pBean.totalRows %> 行 </ td >
< td > 第 <%= pBean.currentPage %> 页 </ td >< td > 共 <%= pBean.totalPage %> 页 </ td >
< td >
<% if (pBean.currentPage == 1 ) {out.print("首页 上一页");} else {%>
<a href="javascript:gotoPage(1)">首页</a>
<a href="javascript:gotoPage(<%=pBean.currentPage-1%>)">上一页</a>
<%
}
%>
<% if (pBean.currentPage == pBean.totalPage) {out.print("下一页 尾页");} else {%>
<a href="javascript:gotoPage(<%=pBean.currentPage+1%>)">下一页</a>
<a href="javascript:gotoPage(<%=pBean.totalPage%>)">尾页</a>
<%} %>
</ td >
< td >
转到第 < select name = " jumpPage " onchange = " Jumping() " >
<% for ( int i = 1 ;i <= pBean.totalPage;i ++ ) {
if(i==pBean.currentPage){
%>
<option selected value=<%=i%>><%=i%></option>
<%
}
else
{
%>
<option value=<%=i%>><%=i%></option>
<%
}
}
%>
</ select > 页
</ td >
</ tr >
</ table >
package com.ascent.util;
import java.util. * ;
/** */ /**
* @author Administrator
* @version 负责页面控制的 JavaBean
*/
public class PageBean {
public int currentPage; // 当前页数
public int totalPage; // 总页数
public int totalRows; // 总行数
public int rowsPage = 5; // 每页显示多少行
public ArrayList data; // 封装页面显示的数据
public PageBean(){}
public void countTotalPage(){ // 计算总页数
if(totalRows%rowsPage==0){
this.totalPage = totalRows/rowsPage;
}
else{
this.totalPage = totalRows/rowsPage + 1;
}
}
public ArrayList getData(){
return data;
}
public PageBean(int totalRows){
this.totalRows = totalRows;
this.countTotalPage();
}
}
// 分页算法
public String guestPageShow() throws Exception {
this.pageReturn();
return "guestproductsshow";
}
@SuppressWarnings( " unchecked " )
private void pageReturn() {
String jump_page = this.getJumpPage();
if (jump_page == null) {
jump_page = "1";
}
PageBean page = this.listData(jump_page);
ActionContext.getContext().getSession().put("product_page_list", page);
this.setDataList(page.getData());
}
public PageBean listData(String number) {
PageBean page = new PageBean(productService.getTotalRows());
int num = Integer.parseInt(number);
String sql = "from Product p where delFlag=0 order by p.pid";
page.data = productService.getData(sql, page.rowsPage * (num - 1),
page.rowsPage);
page.currentPage = num;
return page;
}
// 分页算法
// 实现
PageBean page = this .listData( " 1 " );
ActionContext.getContext().getSession().put( " productuser_page_list " , page);
this .setDataList(page.getData());
// jsp方面
<%
PageBean pBean = (PageBean)session.getAttribute( " productuser_page_list " );
%>
<%
if (pBean.totalPage != 1 ) {
%>
<form name="pageForm" action="pageProductuserManagerAction.action" method="post">
<%@ include file="page.jsp" %>
</form>
<%} %>
// page.jsp
<% @ page language = " java " import = " java.util.* " pageEncoding = " gb2312 " %>
< script language = " JavaScript " >
function Jumping() {
document.pageForm.submit();
}
function gotoPage(pagenum) {
//alert(pagenum);
//alert(document.pageForm.jumpPage.value);
document.pageForm.jumpPage.value = pagenum;
document.pageForm.submit();
}
</ script >
< table >
< tr >
< td > 每页 <%= pBean.rowsPage %> 行 </ td >
< td > 共 <%= pBean.totalRows %> 行 </ td >
< td > 第 <%= pBean.currentPage %> 页 </ td >< td > 共 <%= pBean.totalPage %> 页 </ td >
< td >
<% if (pBean.currentPage == 1 ) {out.print("首页 上一页");} else {%>
<a href="javascript:gotoPage(1)">首页</a>
<a href="javascript:gotoPage(<%=pBean.currentPage-1%>)">上一页</a>
<%
}
%>
<% if (pBean.currentPage == pBean.totalPage) {out.print("下一页 尾页");} else {%>
<a href="javascript:gotoPage(<%=pBean.currentPage+1%>)">下一页</a>
<a href="javascript:gotoPage(<%=pBean.totalPage%>)">尾页</a>
<%} %>
</ td >
< td >
转到第 < select name = " jumpPage " onchange = " Jumping() " >
<% for ( int i = 1 ;i <= pBean.totalPage;i ++ ) {
if(i==pBean.currentPage){
%>
<option selected value=<%=i%>><%=i%></option>
<%
}
else
{
%>
<option value=<%=i%>><%=i%></option>
<%
}
}
%>
</ select > 页
</ td >
</ tr >
</ table >
这个ssh项目的配置文件