无标题文章

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"%>




Insert title here




 加入购物车,购物车有 本,总价为

Java
加入购物车


Oracle 加入购物车

Struts2 加入购物车



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 userNames = Arrays.asList("userName");
     String userName = request.getParameter("userName");
     String result = null;
     if(userNames.contains(userName)){
         result = "used";
     } else {
         result = "unused";
     }
     
     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"%>




Insert title here




UserName:


你可能感兴趣的:(无标题文章)