Javaweb Ajax实验—使用ajax实现登陆验证及购物车功能

创建phoneChoose.jsp

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

Insert title here

<%

//session.removeAttribute(“shoppingCar”);

//session.invalidate();

%>

<%

out.print(“

手机商城



”);

%>

华为P30   

华为mate30

iphone11 

iphone12 

小米10pro  

三星S20   

iqoo3    

vivo NEX3  

创建UserBean.java

package com.bean;

public class UserBean {

private String userName;

private String userPassword;

private String errornews = “”;

public UserBean(String name, String password) {

userName = name;

userPassword = password;

}

public boolean isLoginSuccess() {

boolean success = true;

if (userName == null || !userName.equals(“qqq”) || userPassword == null || !userPassword.equals(“qqq123”)) {

// 如果“用户名为空”、“用户名不为qqq”、“密码为空”、“密码不为qqq123”这四种情况出现任意一种,即视为登陆失败,则进入下面的错误信息提示

errornews += “登陆失败,错误信息:”;

success = false; // 登陆失败

if (userName != null && !userName.equals(“qqq”)) {

// 如果用户名不为空,但用户名不为qqq,视为用户名不存在

errornews += “用户名不存在”;

}

if (userName.equals(“qqq”) && userPassword.length() > 6 && !userPassword.equals(“qqq123”)) {

// 如果用户名为qqq,密码满足6位,但密码不为qqq123,则视为密码错误

errornews += “密码错误”;

}

if (“”.equals(userName)) {

errornews += “用户名不能为空”;

}

if (“”.equals(userPassword)) {

errornews += “密码不能为空”;

} else if (userPassword.length() < 6) {

// 密码小于6位

errornews += “密码不能小于6位”;

}

}

return success;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getUserPassword() {

return userPassword;

}

public void setUserPassword(String userPassword) {

this.userPassword = userPassword;

}

public String geterrornews() {

return errornews;

}

public void seterrornews(String errornews) {

this.errornews = errornews;

}

}

创建loginServlet.java

package com.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.bean.UserBean;

/**

  • Servlet implementation class loginServlet

*/

@WebServlet(“/loginServlet”)

public class loginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

  • @see HttpServlet#HttpServlet()

*/

public loginServlet() {

super();

// TODO Auto-generated constructor stub

}

/**

  • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse

  •  response)
    

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// 传参,传用户名、密码

String userName = request.getParameter(“userName”);

String userPassword = request.getParameter(“userPassword”);

UserBean user = new UserBean(userName, userPassword);

if (user.isLoginSuccess()) {

// 如果UserBean判断登陆成功,则进入phoneChoose.jsp页面

request.getSession().setAttribute(“user”, user);

response.sendRedirect(“phoneChoose.jsp”);

} else {

// 否则,返回错误信息

request.setAttribut

必看视频!获取2024年最新Java开发全套学习资料 备注Java

e(“errornews”, user.geterrornews());

request.getRequestDispatcher(“login.jsp”).forward(request, response);

}

}

/**

  • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse

  •  response)
    

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

}

创建ShoppingCarServlet.java

package com.servlet;

import java.io.IOException;

import java.util.HashMap;

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 javax.servlet.http.HttpSession;

/**

  • Servlet implementation class ShoppingCarServlet

*/

@WebServlet(“/ShoppingCarServlet”)

public class ShoppingCarServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

/**

  • @see HttpServlet#HttpServlet()

*/

public ShoppingCarServlet() {

super();

// TODO Auto-generated constructor stub

}

/**

  • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse

  •  response)
    

*/

@SuppressWarnings(“unchecked”)

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// 设置编码格式

request.setCharacterEncoding(“utf-8”);

HttpSession session = request.getSession();

Map<String, Integer> car = (Map<String, Integer>) session.getAttribute(“shoppingCar”);

if (car == null) {

car = new HashMap<>();

}

// 遍历信息

String[] phones = request.getParameterValues(“phone”);

if (phones != null && phones.length > 0) {

for (String phone : phones) {

// 判断购物车中是否有某手机的购买信息

if (car.get(phone) != null) {

int num = car.get(phone);

car.put(phone, num + 1);

} else {

// 第一次购买手机

car.put(phone, 1);

}

}

}

session.setAttribute(“shoppingCar”, car);

response.sendRedirect(“ShoppingListServlet”);

}

/**

  • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse

  •  response)
    

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// TODO Auto-generated method stub

doGet(request, response);

}

}

创建ShoppingListServlet.java

package com.servlet;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.Map;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

最后

面试题文档来啦,内容很多,485页!

由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。

1111道Java工程师必问面试题

MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:

Elasticsearch 24 题 +Memcached + Redis 40题:

Spring 26 题+ 微服务 27题+ Linux 45题:

Java面试题合集:

ort javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

最后

面试题文档来啦,内容很多,485页!

由于笔记的内容太多,没办法全部展示出来,下面只截取部分内容展示。

1111道Java工程师必问面试题

[外链图片转存中…(img-nSc689lJ-1716450831112)]

MyBatis 27题 + ZooKeeper 25题 + Dubbo 30题:

[外链图片转存中…(img-TIF00fiw-1716450831113)]

Elasticsearch 24 题 +Memcached + Redis 40题:

[外链图片转存中…(img-fWFE7sFp-1716450831114)]

Spring 26 题+ 微服务 27题+ Linux 45题:

[外链图片转存中…(img-YrlpAtXx-1716450831114)]

Java面试题合集:

[外链图片转存中…(img-fHcddr71-1716450831115)]

package shoppingcart.biz; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * 购物车 */ public class ShoppingCart { private Map cartMap = null; //保存Product的Map /** * 购物车构造函数 */ public ShoppingCart(){ cartMap = new HashMap(); } /** * 取得存放产品的cartMap */ public Map getCartMap(){ return this.cartMap; } /** * 向购物车中添加产品 */ public boolean addProduct(String productId){ if(existProduct(productId)){ // 产品已存在则增加数量 Product product = cartMap.get(productId); product.setProductNum(product.getProductNum() + 1); return true; } else{ // 否则新加入该产品 Product product = new Product(productId); if(product.getProductId()==null){ return false; // 数据库中无该产品 }else{ cartMap.put(productId, product); return false; } } } /** * 检查购物车里是否已存在该产品 */ public boolean existProduct(String productId){ Iterator hmkey = cartMap.keySet().iterator(); while(hmkey.hasNext()){ String pid = hmkey.next(); if(pid.equals(productId)){ return true; } } return false; } /** * 从购物车中移除指定产品 */ public void delProduct(String productId){ cartMap.remove(productId); } /** * 获取购物车产品数量 */ public int getProductNum(){ int productNum = 0; Iterator hmEntry = cartMap.values().iterator(); while(hmEntry.hasNext()){ productNum += hmEntry.next().getProductNum(); } return productNum; } /** * 统计购物车商品总价 */ public double getTotalprice(){ double totalPrice = 0.0; Iterator hmkey = cartMap.keySet().iterator(); Product product = null; while(hmkey.hasNext()){ product = cartMap.get(hmkey.next()); totalPrice += product.getProductPirce() * product.getProductNum(); } return totalPrice; } /** * 清空购物车 */ public void clearCart(){ cartMap.clear(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值