package com.MarissaMan.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 javax.servlet.http.HttpSession;
import com.MarissaMan.beans.ShoppingCart;
/**
-
Servlet implementation class AddToCartServlet
*/
@WebServlet("/addToCart")
public class AddToCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookName= request.getParameter("id"); int price = Integer.parseInt(request.getParameter("price")); HttpSession session = request.getSession(); ShoppingCart sCart = (ShoppingCart)session.getAttribute("sCart"); if(sCart == null ){ sCart = new ShoppingCart(); session.setAttribute("sCart", sCart); } sCart.addToCart(bookName, price); StringBuilder result = new StringBuilder(); result.append("{") .append("\"bookName\":\"" + bookName + "\"") .append(",") .append("\"totalBookNumber\":" + sCart.getTotalBookNumber()) .append(",") .append("\"totalMoney\":" + sCart.getTotalMoney()) .append("}"); response.setContentType("text/javascript; charset=UTF-8"); response.getWriter().print(result.toString());
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.1.1.js"></script>
<script type="text/javascript">
$(function(){
$("a").click(function(){
var url = "${pageContext.request.contextPath }/"+this.href;
var args = {"time":new Date()};
console.log(url);
$.getJson(url,args,function(data){
$("#bookName").text(data.bookName);
$("#totalBookNumber").text(data.totalBookNumber);
$("#totalMoney").text(data.totalMoney);
});
return false;
});
})
</script>
</head>
<body>
<span id="bookName"></span> 加入购物车,购物车有<span id="totalBookNumber"></span> 本,总价为
<span id="totalMoney"></span>
Java
<a href="addToCart">加入购物车</a>
<br><br>
Oracle
<a href="addToCart?id=Oracle&price=100">加入购物车</a>
<br><br>
Struts2
<a href="addToCart?id=Struts2&price=100">加入购物车</a>
<br><br>
</body>
</html>
package com.MarissaMan.Servlet;
import java.io.IOException;
import java.util.Arrays;
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;
/**
-
Servlet implementation class ValiateUserNameServlet
*/
@WebServlet("/valiateUserName")
public class ValiateUserNameServlet extends HttpServlet {
private static final long serialVersionUID = 1L;protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<String> userNames = Arrays.asList("userName"); String userName = request.getParameter("userName"); String result = null; if(userNames.contains(userName)){ result = "<font color='red'>used</font>"; } else { result = "<font color='green'>unused</font>"; } response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.getWriter().print(result);
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-3.1.1.js"></script>
<script type="text/javascript">
$(function() {
$(":input[name='username']")
.change(
function() {
var val = $(this).val();
val = $.trim(val);
if (val != "") {
var url = "${pageContext.request.contextPath }/valiateUserName";
var args = {
"username" : val,
"time" : new Date()
};
$.post(url, args, function(data) {
$("#message").html(data);
});
}
});
})
</script>
</head>
<body>
<form action="" method="post">
UserName: <input type="text" name="username" /> <br>
<div id="message"></div>
<br> <input type="submit" value="Submit" />
</form>
</body>
</html>