创建phoneChoose.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%
//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)]