1.功能效果展示
2.user.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
user
<%@include file="/foreground/common/head.jsp"%>
欢迎来到XX商城
订单号
收件人
订单时间
订单状态
操作
${order.id }
${order.receiverName }
${order.ordertime }
未支付
已支付
查看
删除
3.user.js
function layout(){
window.location.href = getRootPath()+"/LayoutServlet";
}
/**
* js获取应用程序根路径
* @return {}
*/
function getRootPath() {
//获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath = window.document.location.href;
//获取主机地址之后的目录,如: /uimcardprj/share/meun.jsp
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPaht = curWwwPath.substring(0, pos);
//获取带"/"的项目名,如:/uimcardprj
var projectName = pathName
.substring(0, pathName.substr(1).indexOf('/') + 1);
return projectName;
}
/**
* 每次页面载入的时候都执行checkIsLogoIn方法,判断用户是否已经登录
*/
function checkIsLogin(){
var url = "/mybookstore/AjaxCheckIsLoginServlet";
var sendData = null;
$(function(){
$.post(url,sendData,function(backData,textStatus,xmlHttpRequest){
if(backData == "no"){
$("#myModal2").modal({backdrop: false,keyboard: false});
$("#myModal2").modal('show');
}
});
});
}
checkIsLogin();
/**
* 使用ajax提交修改用户信息表单
*/
function modifyUser(){
//1.得到表单数据
var sendData = $("form").serialize();
//2.得到请求的url信息
var sendUrl = contextPath + "/AjaxModifyUserinfoServlet?time=" + new Date().getTime();
//3.发送ajax请求
$.ajax({
type:"post",
url:sendUrl,
data:sendData,
async:true,
success:function(backData,textStatus,xmlHttpRequest){
if(backData == "success"){
alert("修改成功!");
}else{
alert(backData);
}
}
});
}
/**
* 显示订单详情
* 发送ajax请求到服务器,服务器返回详情数据,本地js拼接数据显示页面
*/
function showOrderDetail(id){
//1.得到请求的url信息
var sendUrl = contextPath + "/AjaxShowOrderDetailServlet?id=" + id + "&time=" +new Date().getTime();
//2.发送ajax请求
$.ajax({
type:"post",
url:sendUrl,
async:true,
//3.请求成功的回调
success:function(backData,textStatus,xmlHttpRequest){
//给showorderdetail动态添加模态框
$("#showorderdetail").html(backData);
//弹出模态框
$("#myModal").modal();
}
});
}
/**
* 删除订单
* 发送ajax请求到服务器,服务器删除订单,跳转页面
*/
function deleteOrder(id,caller){
//1.得到请求的url信息
var sendUrl = contextPath + "/AjaxDeleteOrderServlet?id="+id+"&time="+new Date().getTime();
//2.发送ajax请求
$.ajax({
type:"post",
url:sendUrl,
async:true,
//3.请求成功的回调
success:function(backData,textSatus,xmlHttpRequest){
if(backData == "success"){
//删除成功
alert("删除成功");
$(caller).parent().remove();
}else{
alert(backData);
}
}
});
}
4.LayoutServlet.java,实现用户退出
package com.chinasoft.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author jf
* 功能:实现用户退出
*/
@WebServlet("/LayoutServlet")
public class LayoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LayoutServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//清空session信息
request.getSession().removeAttribute("user");
request.getSession().invalidate();
//退出登录时清空cookie信息
Cookie[] cookies = request.getCookies();
for(Cookie c:cookies){
if("autologin".equals(c.getName())){
c.setMaxAge(0);
response.addCookie(c);
}
}
//跳转页面
request.getRequestDispatcher("foreground/login/login.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
5.检测用户是否登录
package com.chinasoft.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chinasoft.entity.User;
/**
* @author jf
* 功能:检测用户是否登录
*/
@WebServlet("/AjaxCheckIsLoginServlet")
public class AjaxCheckIsLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AjaxCheckIsLoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//获取浏览器中存储的session对象
User user = (User) request.getSession().getAttribute("user");
//user不为空,说明用户已经登录
if(user!=null){
response.getWriter().write("yes");
}else{
response.getWriter().write("no");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
6.处理修改用户信息
package com.chinasoft.servlet;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.chinasoft.dao.impl.ImplUserDao;
import com.chinasoft.entity.User;
/**
* @author jf
* 处理修改用户基本信息的表单
*/
@WebServlet("/AjaxModifyUserinfoServlet")
public class AjaxModifyUserinfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AjaxModifyUserinfoServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取请求参数
Map map = request.getParameterMap();
//2.使用beanutils的方法实现对象属性的拷贝
User user = new User();
try {
BeanUtils.populate(user, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
//3.修改用户信息
ImplUserDao implUserDao = new ImplUserDao();
try {
User user2 = implUserDao.modifyUser(user);
//把新的修改后的用户保存到session会话中
request.getSession().setAttribute("user", user2);
response.getWriter().write("success");
} catch (SQLException e) {
response.getWriter().write(e.getMessage());
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
7.显示订单详情
package com.chinasoft.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chinasoft.entity.Order;
import com.chinasoft.entity.OrderItem;
/**
* @author jf
* 点击查看,显示订单详情
*/
@WebServlet("/AjaxShowOrderDetailServlet")
public class AjaxShowOrderDetailServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AjaxShowOrderDetailServlet() {
super();
}
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.得到请求参数
String id = request.getParameter("id");
//2.获取session中保存的订单信息
List orders = (List) request.getSession().getAttribute("orders");
Order o = null;
for(Order order : orders){
if(id.equals(order.getId())){
o = order;
break;
}
}
//3.拼接返回给客户端的字符串
String html = "";
html+="";
html+= "
";
html+= "
";
html+= "
";
html+= "";
html+= "
";
html+= "
";
html+= "
购物车列表 ";
html+= "
";
html+= "
";
html+= "";
html+= "序号 ";
html+= "商品名称 ";
html+= "价格 ";
html+= "购买数量 ";
html+= "库存 ";
html+= "小计 ";
html+= " ";
for(int i=0 ; i";
html+= ""+i+" ";
html+= ""+item.getProduct().getName()+" ";
html+= ""+item.getProduct().getPrice()+" ";
html+= ""+item.getBuynum()+" ";
html+= ""+item.getProduct().getNum()+" ";
html+= ""+item.getBuynum()*item.getProduct().getPrice()+" ";
html+= "";
}
html+= "
";
html+= "
";
html+= "
";
html+= "";
html+= "
";
html+= "
";
html+= "
";
html+= "
";
html+="
";
//4.给客户端返回字符串
response.getWriter().write(html);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
8.处理删除订单
package com.chinasoft.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chinasoft.dao.impl.ImplOrderDao;
import com.chinasoft.entity.Order;
/**
* @author jf
* 处理删除订单的servlet
*/
@WebServlet("/AjaxDeleteOrderServlet")
public class AjaxDeleteOrderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public AjaxDeleteOrderServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.得到请求参数
String id = request.getParameter("id");
//2.获取session中保存的订单信息
@SuppressWarnings("unchecked")
List orders = (List) request.getSession().getAttribute("orders");
Order o = null;
for(Order order:orders){
if(id.equals(order.getId())){
o = order;
break;
}
}
//3.删除订单
ImplOrderDao implOrderDao = new ImplOrderDao();
try {
//数据库删除订单
implOrderDao.deleteOrder(o);
//域对象删除订单
orders.remove(o);
request.getSession().setAttribute("orders", orders);
//给客户端返回值
response.getWriter().write("success");
} catch (SQLException e) {
response.getWriter().write(e.getMessage());
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}