• JavaWeb 综合案例(包含注册与登录页面,包含对数据库的增删改查页面)新增加session与cookie与验证码


    目录

    1.案例结构(会依次对代码进行展示和解答) 

    2.BrandMapper接口类

    3.UserMapper接口类

    4.Brand实体类

    5.User实体类

    6.BrandService(集成所有的方法)

    7.UserService(结成所有的方法)

    8.CheckCodeUtil工具类(做验证码)

    9.SqlSessionFactoryUtils工具类

    10.AddServlet(实现添加)

    11.CheckCodeServlet(实现验证码的添加)

    12.DeleteServlet(实现删除)

    13.LoginServlet(实现登录功能)

    14.RegisterServlet(实现注册功能)

    15.SelectAllServlet(实现查找)

    16.SelectByIdServlet(实现回显--update中的模块)

    17.UpdateServlet(实现更新)

    18.BrandMapper.xml(Brand映射文件)

    19.UserMapper.xml(User映射文件,没有用到)

    20.Mybatis-config.xml(Mybatis实现连接数据库)

    21.CSS文件(登录注册页面的CSS文件)

    1.login.css

    2.register.css

    22.IMG文件(登陆注册的IMG文件)

    1.a.jpg

    2.Desert1.jpg

    3.reg_bg_min.jpg

    23.addBrand.jsp(显示添加页面)

    24.brand.jsp(实现查找的页面)

    25.delete.jsp(实现删除的页面)

    26.index.html(主页面,显示brand.jsp的链接)

    27.login.jsp(实现登录的页面)

    28.register.jsp(实现注册的页面)

    29.update.jsp(显示更新的界面)

    30.pom文件(存储的关于Java的一些依赖)

    31.展示页面

    (算了,不想展示了,如果有哪位朋友愿意来看的话,给我留言我私信给你)


    1.案例结构(会依次对代码进行展示和解答) 

    2.BrandMapper接口类

    1. package com.itheima.mapper;
    2. import com.itheima.pojo.Brand;
    3. import org.apache.ibatis.annotations.*;
    4. import java.util.List;
    5. public interface BrandMapper {
    6. /**
    7. * dao层查询所有的操作
    8. * @return
    9. */
    10. @Select("select * from tb_brand")
    11. @ResultMap("brandResultMap")
    12. List selectAll();
    13. /**
    14. * dao层添加数据
    15. * @param brand
    16. */
    17. @Insert("insert into tb_brand values (null,#{brandName},#{companyName},#{ordered},#{description},#{status})")
    18. void add(Brand brand);
    19. /**
    20. * 更改数据前的回显数据,图像化根据id查询
    21. * @param id
    22. * @return
    23. */
    24. @Select("select * from tb_brand where id = #{id}")
    25. @ResultMap("brandResultMap")
    26. Brand selectById(int id);
    27. /**
    28. * 修改语句
    29. * @param brand
    30. */
    31. @Update("update tb_brand set brand_name=#{brandName},company_name=#{companyName},ordered=#{ordered},description=#{description},status=#{status} where id=#{id}")
    32. void update(Brand brand);
    33. @Delete("delete from tb_brand where id = #{id}")
    34. void delete(int id);
    35. }

    3.UserMapper接口类

    1. package com.itheima.mapper;
    2. import com.itheima.pojo.User;
    3. import org.apache.ibatis.annotations.Insert;
    4. import org.apache.ibatis.annotations.Param;
    5. import org.apache.ibatis.annotations.Select;
    6. public interface UserMapper {
    7. /**
    8. * 根据用户名和密码查询用户对象
    9. * @param username
    10. * @param password
    11. * @return
    12. */
    13. @Select("select * from tb_user where username = #{username} and password = #{password}")
    14. User select(@Param("username") String username,@Param("password") String password);
    15. /**
    16. * 根据用户名查询用户对象
    17. * @param username
    18. * @return
    19. */
    20. @Select("select * from tb_user where username = #{username}")
    21. User selectByUsername(String username);
    22. /**
    23. * 添加用户
    24. * @param user
    25. */
    26. @Insert("insert into tb_user values(null,#{username},#{password})")
    27. void add(User user);
    28. }

    4.Brand实体类

    1. package com.itheima.pojo;
    2. /*
    3. 在实体类中,基本数据类型,建议使用其对应的包装类
    4. */
    5. public class Brand {
    6. //id主键
    7. private Integer id;
    8. //品牌名称
    9. private String brandName;
    10. //企业名称
    11. private String companyName;
    12. //排序字段
    13. private Integer ordered;
    14. //描述信息
    15. private String description;
    16. //状态 0:禁用 1:启用
    17. private Integer status;
    18. public Brand(Integer id, String brandName, String companyName, Integer ordered, String description,
    19. Integer status) {
    20. super();
    21. this.id = id;
    22. this.brandName = brandName;
    23. this.companyName = companyName;
    24. this.ordered = ordered;
    25. this.description = description;
    26. this.status = status;
    27. }
    28. public Brand() {
    29. }
    30. @Override
    31. public String toString() {
    32. return "Brand [id=" + id + ", brandName=" + brandName + ", companyName=" + companyName + ", ordered=" + ordered
    33. + ", description=" + description + ", status=" + status + "]";
    34. }
    35. public Integer getId() {
    36. return id;
    37. }
    38. public void setId(Integer id) {
    39. this.id = id;
    40. }
    41. public String getBrandName() {
    42. return brandName;
    43. }
    44. public void setBrandName(String brandName) {
    45. this.brandName = brandName;
    46. }
    47. public String getCompanyName() {
    48. return companyName;
    49. }
    50. public void setCompanyName(String companyName) {
    51. this.companyName = companyName;
    52. }
    53. public Integer getOrdered() {
    54. return ordered;
    55. }
    56. public void setOrdered(Integer ordered) {
    57. this.ordered = ordered;
    58. }
    59. public String getDescription() {
    60. return description;
    61. }
    62. public void setDescription(String description) {
    63. this.description = description;
    64. }
    65. public Integer getStatus() {
    66. return status;
    67. }
    68. public void setStatus(Integer status) {
    69. this.status = status;
    70. }
    71. }

    5.User实体类

    1. package com.itheima.pojo;
    2. public class User {
    3. private Integer id;
    4. private String username;
    5. private String password;
    6. public Integer getId() {
    7. return id;
    8. }
    9. public void setId(Integer id) {
    10. this.id = id;
    11. }
    12. public String getUsername() {
    13. return username;
    14. }
    15. public void setUsername(String username) {
    16. this.username = username;
    17. }
    18. public String getPassword() {
    19. return password;
    20. }
    21. public void setPassword(String password) {
    22. this.password = password;
    23. }
    24. @Override
    25. public String toString() {
    26. return "User{" +
    27. "id=" + id +
    28. ", username='" + username + '\'' +
    29. ", password='" + password + '\'' +
    30. '}';
    31. }
    32. }

    6.BrandService(集成所有的方法)

    1. package com.itheima.service;
    2. import com.itheima.Util.SqlSessionFactoryUtils;
    3. import com.itheima.mapper.BrandMapper;
    4. import com.itheima.pojo.Brand;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. import java.util.List;
    8. public class BrandService {
    9. SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
    10. /**
    11. * service查询所有的功能
    12. * @return
    13. */
    14. public List selectAll() {
    15. //调用BrandMapper.selectAll()方法
    16. //获取SqlSession
    17. SqlSession sqlSession = factory.openSession();
    18. //获取BrandMapper
    19. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    20. //调用方法
    21. List brands = mapper.selectAll();
    22. //关闭sqlSession
    23. sqlSession.close();
    24. //返回brands
    25. return brands;
    26. }
    27. /**
    28. * 添加数据库
    29. * @return
    30. */
    31. public void add(Brand brand) {
    32. //调用BrandMapper.selectAll()方法
    33. //获取SqlSession
    34. SqlSession sqlSession = factory.openSession(true);
    35. //获取BrandMapper
    36. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    37. //调用方法
    38. mapper.add(brand);
    39. //关闭sqlSession
    40. sqlSession.close();
    41. }
    42. /**
    43. * 根据id查询
    44. * @return
    45. */
    46. public Brand selectById(int id) {
    47. //调用BrandMapper.selectAll()方法
    48. //获取SqlSession
    49. SqlSession sqlSession = factory.openSession();
    50. //获取BrandMapper
    51. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    52. //调用方法
    53. Brand brand = mapper.selectById(id);
    54. //关闭sqlSession
    55. sqlSession.close();
    56. //返回brand
    57. return brand;
    58. }
    59. public void update(Brand brand) {
    60. //调用BrandMapper.selectAll()方法
    61. //获取SqlSession
    62. SqlSession sqlSession = factory.openSession(true);
    63. //获取BrandMapper
    64. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    65. //调用方法
    66. mapper.update(brand);
    67. //关闭sqlSession
    68. sqlSession.close();
    69. }
    70. public void delete(int id) {
    71. //调用BrandMapper.selectAll()方法
    72. //获取SqlSession
    73. SqlSession sqlSession = factory.openSession(true);
    74. //获取BrandMapper
    75. BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
    76. //调用方法
    77. mapper.delete(id);
    78. //关闭sqlSession
    79. sqlSession.close();
    80. }
    81. }

    7.UserService(结成所有的方法)

    1. package com.itheima.service;
    2. import com.itheima.Util.SqlSessionFactoryUtils;
    3. import com.itheima.mapper.UserMapper;
    4. import com.itheima.pojo.User;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. public class UserService {
    8. //获取util工具包的SqlSessionFactory
    9. SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
    10. //调用UserMapper,构成方法,来让servlet进行下一步操作
    11. /**
    12. * 登录
    13. * @param username
    14. * @param password
    15. * @return
    16. */
    17. public User login(String username,String password){
    18. //获取SqlSession,查找不需要事务,所以选择不提交事务
    19. SqlSession sqlSession = factory.openSession();
    20. //获取UserMapper
    21. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    22. //调用方法
    23. User user = mapper.select(username, password);
    24. //关闭sqlSession
    25. sqlSession.close();
    26. //返回user
    27. return user;
    28. }
    29. public Boolean register(User user) {
    30. //获取SqlSession,查找不需要事务,所以选择不提交事务
    31. SqlSession sqlSession = factory.openSession();
    32. //获取UserMapper
    33. UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    34. //判断用户名是否存在
    35. User u = mapper.selectByUsername(user.getUsername());
    36. if ( u == null ){
    37. //用户名不存在
    38. mapper.add(user);
    39. //提交事务
    40. sqlSession.commit();
    41. }
    42. //关闭sqlSession,释放资源
    43. sqlSession.close();
    44. return u == null;
    45. }
    46. }

    8.CheckCodeUtil工具类(做验证码)

    1. package com.itheima.Util;
    2. import javax.imageio.ImageIO;
    3. import java.awt.*;
    4. import java.awt.geom.AffineTransform;
    5. import java.awt.image.BufferedImage;
    6. import java.io.*;
    7. import java.util.Arrays;
    8. import java.util.Random;
    9. /**
    10. * 生成验证码工具类
    11. */
    12. public class CheckCodeUtil {
    13. public static final String VERIFY_CODES = "123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    14. private static Random random = new Random();
    15. public static void main(String[] args) throws IOException {
    16. FileOutputStream fos = new FileOutputStream("D://152//a.jpg");
    17. String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, fos, 4);
    18. System.out.println(checkCode);
    19. }
    20. /**
    21. * 输出随机验证码图片流,并返回验证码值(一般传入输出流,响应response页面端,Web项目用的较多)
    22. *
    23. * @param width 图片宽度
    24. * @param height 图片高度
    25. * @param os 输出流
    26. * @param verifySize 数据长度
    27. * @return 返回验证码数据
    28. * @throws IOException
    29. */
    30. public static String outputVerifyImage(int width, int height, OutputStream os, int verifySize) throws IOException {
    31. String verifyCode = generateVerifyCode(verifySize);
    32. outputImage(width, height, os, verifyCode);
    33. return verifyCode;
    34. }
    35. /**
    36. * 使用系统默认字符源生成验证码
    37. *
    38. * @param verifySize 验证码长度
    39. * @return
    40. */
    41. public static String generateVerifyCode(int verifySize) {
    42. return generateVerifyCode(verifySize, VERIFY_CODES);
    43. }
    44. /**
    45. * 使用指定源生成验证码
    46. *
    47. * @param verifySize 验证码长度
    48. * @param sources 验证码字符源
    49. * @return
    50. */
    51. public static String generateVerifyCode(int verifySize, String sources) {
    52. // 未设定展示源的字码,赋默认值大写字母+数字
    53. if (sources == null || sources.length() == 0) {
    54. sources = VERIFY_CODES;
    55. }
    56. int codesLen = sources.length();
    57. Random rand = new Random(System.currentTimeMillis());
    58. StringBuilder verifyCode = new StringBuilder(verifySize);
    59. for (int i = 0; i < verifySize; i++) {
    60. verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
    61. }
    62. return verifyCode.toString();
    63. }
    64. /**
    65. * 生成随机验证码文件,并返回验证码值 (生成图片形式,用的较少)
    66. *
    67. * @param w
    68. * @param h
    69. * @param outputFile
    70. * @param verifySize
    71. * @return
    72. * @throws IOException
    73. */
    74. public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException {
    75. String verifyCode = generateVerifyCode(verifySize);
    76. outputImage(w, h, outputFile, verifyCode);
    77. return verifyCode;
    78. }
    79. /**
    80. * 生成指定验证码图像文件
    81. *
    82. * @param w
    83. * @param h
    84. * @param outputFile
    85. * @param code
    86. * @throws IOException
    87. */
    88. public static void outputImage(int w, int h, File outputFile, String code) throws IOException {
    89. if (outputFile == null) {
    90. return;
    91. }
    92. File dir = outputFile.getParentFile();
    93. //文件不存在
    94. if (!dir.exists()) {
    95. //创建
    96. dir.mkdirs();
    97. }
    98. try {
    99. outputFile.createNewFile();
    100. FileOutputStream fos = new FileOutputStream(outputFile);
    101. outputImage(w, h, fos, code);
    102. fos.close();
    103. } catch (IOException e) {
    104. throw e;
    105. }
    106. }
    107. /**
    108. * 输出指定验证码图片流
    109. *
    110. * @param w
    111. * @param h
    112. * @param os
    113. * @param code
    114. * @throws IOException
    115. */
    116. public static void outputImage(int w, int h, OutputStream os, String code) throws IOException {
    117. int verifySize = code.length();
    118. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    119. Random rand = new Random();
    120. Graphics2D g2 = image.createGraphics();
    121. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    122. // 创建颜色集合,使用java.awt包下的类
    123. Color[] colors = new Color[5];
    124. Color[] colorSpaces = new Color[]{Color.WHITE, Color.CYAN,
    125. Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
    126. Color.PINK, Color.YELLOW};
    127. float[] fractions = new float[colors.length];
    128. for (int i = 0; i < colors.length; i++) {
    129. colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
    130. fractions[i] = rand.nextFloat();
    131. }
    132. Arrays.sort(fractions);
    133. // 设置边框色
    134. g2.setColor(Color.GRAY);
    135. g2.fillRect(0, 0, w, h);
    136. Color c = getRandColor(200, 250);
    137. // 设置背景色
    138. g2.setColor(c);
    139. g2.fillRect(0, 2, w, h - 4);
    140. // 绘制干扰线
    141. Random random = new Random();
    142. // 设置线条的颜色
    143. g2.setColor(getRandColor(160, 200));
    144. for (int i = 0; i < 20; i++) {
    145. int x = random.nextInt(w - 1);
    146. int y = random.nextInt(h - 1);
    147. int xl = random.nextInt(6) + 1;
    148. int yl = random.nextInt(12) + 1;
    149. g2.drawLine(x, y, x + xl + 40, y + yl + 20);
    150. }
    151. // 添加噪点
    152. // 噪声率
    153. float yawpRate = 0.05f;
    154. int area = (int) (yawpRate * w * h);
    155. for (int i = 0; i < area; i++) {
    156. int x = random.nextInt(w);
    157. int y = random.nextInt(h);
    158. // 获取随机颜色
    159. int rgb = getRandomIntColor();
    160. image.setRGB(x, y, rgb);
    161. }
    162. // 添加图片扭曲
    163. shear(g2, w, h, c);
    164. g2.setColor(getRandColor(100, 160));
    165. int fontSize = h - 4;
    166. Font font = new Font("Algerian", Font.ITALIC, fontSize);
    167. g2.setFont(font);
    168. char[] chars = code.toCharArray();
    169. for (int i = 0; i < verifySize; i++) {
    170. AffineTransform affine = new AffineTransform();
    171. affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize / 2, h / 2);
    172. g2.setTransform(affine);
    173. g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + fontSize / 2 - 10);
    174. }
    175. g2.dispose();
    176. ImageIO.write(image, "jpg", os);
    177. }
    178. /**
    179. * 随机颜色
    180. *
    181. * @param fc
    182. * @param bc
    183. * @return
    184. */
    185. private static Color getRandColor(int fc, int bc) {
    186. if (fc > 255) {
    187. fc = 255;
    188. }
    189. if (bc > 255) {
    190. bc = 255;
    191. }
    192. int r = fc + random.nextInt(bc - fc);
    193. int g = fc + random.nextInt(bc - fc);
    194. int b = fc + random.nextInt(bc - fc);
    195. return new Color(r, g, b);
    196. }
    197. private static int getRandomIntColor() {
    198. int[] rgb = getRandomRgb();
    199. int color = 0;
    200. for (int c : rgb) {
    201. color = color << 8;
    202. color = color | c;
    203. }
    204. return color;
    205. }
    206. private static int[] getRandomRgb() {
    207. int[] rgb = new int[3];
    208. for (int i = 0; i < 3; i++) {
    209. rgb[i] = random.nextInt(255);
    210. }
    211. return rgb;
    212. }
    213. private static void shear(Graphics g, int w1, int h1, Color color) {
    214. shearX(g, w1, h1, color);
    215. shearY(g, w1, h1, color);
    216. }
    217. private static void shearX(Graphics g, int w1, int h1, Color color) {
    218. int period = random.nextInt(2);
    219. boolean borderGap = true;
    220. int frames = 1;
    221. int phase = random.nextInt(2);
    222. for (int i = 0; i < h1; i++) {
    223. double d = (double) (period >> 1)
    224. * Math.sin((double) i / (double) period
    225. + (6.2831853071795862D * (double) phase)
    226. / (double) frames);
    227. g.copyArea(0, i, w1, 1, (int) d, 0);
    228. if (borderGap) {
    229. g.setColor(color);
    230. g.drawLine((int) d, i, 0, i);
    231. g.drawLine((int) d + w1, i, w1, i);
    232. }
    233. }
    234. }
    235. private static void shearY(Graphics g, int w1, int h1, Color color) {
    236. int period = random.nextInt(40) + 10; // 50;
    237. boolean borderGap = true;
    238. int frames = 20;
    239. int phase = 7;
    240. for (int i = 0; i < w1; i++) {
    241. double d = (double) (period >> 1)
    242. * Math.sin((double) i / (double) period
    243. + (6.2831853071795862D * (double) phase)
    244. / (double) frames);
    245. g.copyArea(i, 0, 1, h1, 0, (int) d);
    246. if (borderGap) {
    247. g.setColor(color);
    248. g.drawLine(i, (int) d, i, 0);
    249. g.drawLine(i, (int) d + h1, i, h1);
    250. }
    251. }
    252. }
    253. }

    9.SqlSessionFactoryUtils工具类

    1. package com.itheima.Util;
    2. import org.apache.ibatis.io.Resources;
    3. import org.apache.ibatis.session.SqlSessionFactory;
    4. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    5. import java.io.IOException;
    6. import java.io.InputStream;
    7. public class SqlSessionFactoryUtils {
    8. private static SqlSessionFactory sqlSessionFactory;
    9. //静态代码快会随着类的加载二自动执行,并且只执行一次
    10. static
    11. {
    12. String resource = "mybatis-config.xml";//现在在resource根目录下
    13. InputStream inputStream = null;
    14. try {
    15. inputStream = Resources.getResourceAsStream(resource);
    16. } catch (IOException e) {
    17. e.printStackTrace();
    18. }
    19. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    20. }
    21. public static SqlSessionFactory getSqlSessionFactory(){
    22. return sqlSessionFactory;
    23. }
    24. }

    10.AddServlet(实现添加)

    1. package com.itheima.web;
    2. import com.itheima.pojo.Brand;
    3. import com.itheima.service.BrandService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. @WebServlet("/addServlet")
    9. public class AddServlet extends HttpServlet {
    10. private BrandService service = new BrandService();
    11. @Override
    12. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    13. //解决乱码问题
    14. request.setCharacterEncoding("UTF-8");
    15. //1.接受表单提交的数据,封装成一个brand对象的数据
    16. String brandName = request.getParameter("brandName");
    17. String companyName = request.getParameter("companyName");
    18. String ordered = request.getParameter("ordered");
    19. String description = request.getParameter("description");
    20. String status = request.getParameter("status");
    21. Brand brand = new Brand(null,brandName,companyName,Integer.parseInt(ordered),description,Integer.parseInt(status));
    22. //2.调用service添加数据
    23. service.add(brand);
    24. //3.转发到查询所有的servlet
    25. request.getRequestDispatcher("/selectAllServlet").forward(request,response);
    26. }
    27. @Override
    28. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    29. this.doGet(request, response);
    30. }
    31. }

    11.CheckCodeServlet(实现验证码的添加)

    1. package com.itheima.web;
    2. import com.itheima.Util.CheckCodeUtil;
    3. import javax.servlet.*;
    4. import javax.servlet.http.*;
    5. import javax.servlet.annotation.*;
    6. import java.io.FileOutputStream;
    7. import java.io.IOException;
    8. @WebServlet("/checkCodeServlet")
    9. public class CheckCodeServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. //生成验证码
    13. ServletOutputStream outputStream = response.getOutputStream();
    14. String checkCode = CheckCodeUtil.outputVerifyImage(100, 50, outputStream, 4);
    15. //把生成的验证码存入到session对象
    16. HttpSession session = request.getSession();
    17. session.setAttribute("checkCodeGen",checkCode);
    18. }
    19. @Override
    20. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    21. this.doGet(request, response);
    22. }
    23. }

    12.DeleteServlet(实现删除)

    1. package com.itheima.web;
    2. import com.itheima.pojo.Brand;
    3. import com.itheima.service.BrandService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. @WebServlet("/deleteServlet")
    9. public class DeleteServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. //1.传入id
    13. String id = request.getParameter("id");
    14. //2.调用对象
    15. BrandService service = new BrandService();
    16. service.delete(Integer.parseInt(id));
    17. //3.把id传入到delete
    18. request.setAttribute("id",id);
    19. //3.强求转发到主界面
    20. request.getRequestDispatcher("/delete.jsp").forward(request,response);
    21. }
    22. @Override
    23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    24. this.doGet(request, response);
    25. }
    26. }

    13.LoginServlet(实现登录功能)

    1. package com.itheima.web;
    2. import com.itheima.pojo.User;
    3. import com.itheima.service.UserService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. @WebServlet("/loginServlet")
    9. public class LoginServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. //1.获取表单传递的参数(实际上和php很类似,接受的表单信息,其实就是input的name属性)
    13. String username = request.getParameter("username");
    14. String password = request.getParameter("password");
    15. //获取复选框数据
    16. String remember = request.getParameter("remember");
    17. //2.获取UserService的方法来去查询
    18. UserService service = new UserService();
    19. User user = service.login(username, password);
    20. //进行条件判断
    21. if(user != null ){
    22. //判断用户选中了remember,最好写成("1".equals(remember)),以免造成空指针异常
    23. if("1".equals(remember)){
    24. //确定数值等于1
    25. //1.创建cookie(可以把username当作一个cookie,把另外一个password当作另一个cookie)
    26. Cookie c_cookie = new Cookie("username",username);
    27. Cookie p_cookie = new Cookie("password",password);
    28. //1.1设置此cookie在浏览器存活的时间
    29. c_cookie.setMaxAge(60*60*24*7);
    30. p_cookie.setMaxAge(60*60*24*7);
    31. //2.发送cookie
    32. response.addCookie(c_cookie);
    33. response.addCookie(p_cookie);
    34. }
    35. //提前打开session
    36. HttpSession session = request.getSession();
    37. session.setAttribute("user",user);
    38. //登陆成功,跳转到查询所有的主页面(index.html),否则这个index.html将毫无意义(两个方法,请求转发(有参数),重定向)
    39. String contextPath = request.getContextPath();//动态获取地址
    40. response.sendRedirect(contextPath+"/index.html");
    41. }else{
    42. //登录失败,提供失败信息,跳转到login.jsp页面,引导客户进行注册
    43. //将错误信息存储到request里面,利用请求转发,来让register.jsp页面显示此错误信息
    44. request.setAttribute("login_msg","用户名或密码错误,请您重新登录");
    45. request.getRequestDispatcher("/login.jsp").forward(request,response);
    46. }
    47. }
    48. @Override
    49. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    50. this.doGet(request, response);
    51. }
    52. }

    14.RegisterServlet(实现注册功能)

    1. package com.itheima.web;
    2. import com.itheima.pojo.User;
    3. import com.itheima.service.UserService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. @WebServlet("/registerServlet")
    9. public class RegisterServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. //解决乱码问题
    13. request.setCharacterEncoding("UTF-8");
    14. //获取表单参数(与用户输入的验证码)
    15. String username = request.getParameter("username");
    16. String password = request.getParameter("password");
    17. String checkCode = request.getParameter("checkCode");
    18. //获取session,把从session获取的checkCodeGen这个值,强转为String,利于之后的比对
    19. HttpSession session = request.getSession();
    20. String checkCodeGen = (String)session.getAttribute("checkCodeGen");
    21. User user = new User();
    22. user.setUsername(username);
    23. user.setPassword(password);
    24. //验证码的比对(前提是这个checkCode(用户填写的不准为空))
    25. if(!checkCodeGen.equals(checkCode)){
    26. //不允许注册
    27. //如果验证码比对不正确,则就给出提示信息
    28. request.setAttribute("register_msg", "验证码错误");
    29. request.getRequestDispatcher("/register.jsp").forward(request, response);
    30. return;//如果执行到return后,就不会在向下继续执行到mysql了
    31. }
    32. //调取userService方法完成对数据的添加
    33. UserService service = new UserService();
    34. Boolean register = service.register(user);
    35. //判断注册成功与否
    36. if (username.length()!=0 && password .length()!=0) {
    37. if (register) {
    38. //register为true,则跳转到登录页面(因为还要显示注册成功,请登录的信息,所以使用请求转发比较稳妥)
    39. request.setAttribute("register_msg", "注册成功,请登录");
    40. request.getRequestDispatcher("/login.jsp").forward(request, response);
    41. } else {
    42. //register为false
    43. request.setAttribute("register_msg", "用户已经存在,请重新注册");
    44. request.getRequestDispatcher("/register.jsp").forward(request, response);
    45. }
    46. }else{
    47. //用户名和密码为空的情况下
    48. request.setAttribute("error_msg", "账号或密码为空,请重新注册");
    49. request.getRequestDispatcher("/register.jsp").forward(request, response);
    50. }
    51. }
    52. @Override
    53. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    54. this.doGet(request, response);
    55. }
    56. }

    15.SelectAllServlet(实现查找)

    1. package com.itheima.web;
    2. import com.itheima.pojo.Brand;
    3. import com.itheima.service.BrandService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. import java.util.List;
    9. @WebServlet("/selectAllServlet")
    10. public class SelectAllServlet extends HttpServlet {
    11. private BrandService service = new BrandService();
    12. @Override
    13. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    14. //调用对应的BrandService完成查询
    15. List brands = service.selectAll();
    16. //存入request域中
    17. //void setAttribute(String name,Object o)存储数据到request域中
    18. request.setAttribute("brands",brands);
    19. //请求转发
    20. request.getRequestDispatcher("/brand.jsp").forward(request,response);
    21. }
    22. @Override
    23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    24. this.doGet(request, response);
    25. }
    26. }

    16.SelectByIdServlet(实现回显--update中的模块)

    1. package com.itheima.web;
    2. import com.itheima.pojo.Brand;
    3. import com.itheima.service.BrandService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. @WebServlet("/selectByIdServlet")
    9. public class SelectByIdServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. //1,在brand.jsp里面传输了一个id,之前在php里面学过,就不在赘述了(接收id)
    13. String id = request.getParameter("id");
    14. //id有了之后,把这个id传入到SelectById的形参里面去(调用对应的service进行查询)
    15. BrandService service = new BrandService();
    16. Brand brand = service.selectById(Integer.parseInt(id));
    17. //储存到request域中
    18. request.setAttribute("brand",brand);
    19. //转发到update.jsp中
    20. request.getRequestDispatcher("/update.jsp").forward(request,response);
    21. }
    22. @Override
    23. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    24. this.doGet(request, response);
    25. }
    26. }

    17.UpdateServlet(实现更新)

    1. package com.itheima.web;
    2. import com.itheima.pojo.Brand;
    3. import com.itheima.service.BrandService;
    4. import javax.servlet.*;
    5. import javax.servlet.http.*;
    6. import javax.servlet.annotation.*;
    7. import java.io.IOException;
    8. @WebServlet("/updateServlet")
    9. public class UpdateServlet extends HttpServlet {
    10. @Override
    11. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. //解决乱码问题
    13. request.setCharacterEncoding("UTF-8");
    14. //1.接受表单提交的数据,封装成一个brand对象的数据
    15. String id = request.getParameter("id");
    16. String brandName = request.getParameter("brandName");
    17. String companyName = request.getParameter("companyName");
    18. String ordered = request.getParameter("ordered");
    19. String description = request.getParameter("description");
    20. String status = request.getParameter("status");
    21. Brand brand = new Brand(Integer.parseInt(id),brandName,companyName,Integer.parseInt(ordered),description,Integer.parseInt(status));
    22. //调用service更改数据
    23. BrandService service = new BrandService();
    24. service.update(brand);
    25. //3.转发到查询所有的servlet
    26. request.getRequestDispatcher("/selectAllServlet").forward(request,response);
    27. }
    28. @Override
    29. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    30. this.doGet(request, response);
    31. }
    32. }

    18.BrandMapper.xml(Brand映射文件)

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.itheima.mapper.BrandMapper">
    6. <resultMap id="brandResultMap" type="brand">
    7. <result column="brand_name" property="brandName">result>
    8. <result column="company_name" property="companyName">result>
    9. resultMap>
    10. mapper>

    19.UserMapper.xml(User映射文件,没有用到

    20.Mybatis-config.xml(Mybatis实现连接数据库)

    1. "1.0" encoding="UTF-8" ?>
    2. configuration
    3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    5. <configuration>
    6. <typeAliases>
    7. <package name="com.itheima.pojo"/>
    8. typeAliases>
    9. <environments default="development">
    10. <environment id="development">
    11. <transactionManager type="JDBC"/>
    12. <dataSource type="POOLED">
    13. <property name="driver" value="com.mysql.jdbc.Driver"/>
    14. <property name="url" value="jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true"/>
    15. <property name="username" value="root"/>
    16. <property name="password" value="root"/>
    17. dataSource>
    18. environment>
    19. environments>
    20. <mappers>
    21. <package name="com.itheima.mapper"/>
    22. mappers>
    23. configuration>

    21.CSS文件(登录注册页面的CSS文件)

    1.login.css

    1. * {
    2. margin: 0;
    3. padding: 0;
    4. }
    5. html {
    6. height: 100%;
    7. width: 100%;
    8. overflow: hidden;
    9. margin: 0;
    10. padding: 0;
    11. background: url(../imgs/Desert1.jpg) no-repeat 0px 0px;
    12. background-repeat: no-repeat;
    13. background-size: 100% 100%;
    14. -moz-background-size: 100% 100%;
    15. }
    16. body {
    17. display: flex;
    18. align-items: center;
    19. justify-content: center;
    20. height: 100%;
    21. }
    22. #loginDiv {
    23. width: 37%;
    24. display: flex;
    25. justify-content: center;
    26. align-items: center;
    27. height: 380px;
    28. background-color: rgba(75, 81, 95, 0.3);
    29. box-shadow: 7px 7px 17px rgba(52, 56, 66, 0.5);
    30. border-radius: 5px;
    31. }
    32. #name_trip {
    33. margin-left: 50px;
    34. color: red;
    35. }
    36. p {
    37. margin-top: 30px;
    38. margin-left: 20px;
    39. color: azure;
    40. }
    41. #remember{
    42. margin-left: 15px;
    43. border-radius: 5px;
    44. border-style: hidden;
    45. background-color: rgba(216, 191, 216, 0.5);
    46. outline: none;
    47. padding-left: 10px;
    48. height: 20px;
    49. width: 20px;
    50. }
    51. #username{
    52. width: 200px;
    53. margin-left: 15px;
    54. border-radius: 5px;
    55. border-style: hidden;
    56. height: 30px;
    57. background-color: rgba(216, 191, 216, 0.5);
    58. outline: none;
    59. color: #f0edf3;
    60. padding-left: 10px;
    61. }
    62. #password{
    63. width: 202px;
    64. margin-left: 15px;
    65. border-radius: 5px;
    66. border-style: hidden;
    67. height: 30px;
    68. background-color: rgba(216, 191, 216, 0.5);
    69. outline: none;
    70. color: #f0edf3;
    71. padding-left: 10px;
    72. }
    73. .button {
    74. border-color: cornsilk;
    75. background-color: rgba(100, 149, 237, .7);
    76. color: aliceblue;
    77. border-style: hidden;
    78. border-radius: 5px;
    79. width: 100px;
    80. height: 31px;
    81. font-size: 16px;
    82. }
    83. #subDiv {
    84. text-align: center;
    85. margin-top: 30px;
    86. }
    87. #loginMsg{
    88. text-align: center;
    89. color: aliceblue;
    90. }
    91. #errorMsg{
    92. text-align: center;
    93. color:red;
    94. }

    2.register.css

    1. * {
    2. margin: 0;
    3. padding: 0;
    4. list-style-type: none;
    5. }
    6. .reg-content{
    7. padding: 30px;
    8. margin: 3px;
    9. }
    10. a, img {
    11. border: 0;
    12. }
    13. body {
    14. background-image: url("../imgs/reg_bg_min.jpg") ;
    15. text-align: center;
    16. }
    17. table {
    18. border-collapse: collapse;
    19. border-spacing: 0;
    20. }
    21. td, th {
    22. padding: 0;
    23. height: 90px;
    24. }
    25. .inputs{
    26. vertical-align: top;
    27. }
    28. .clear {
    29. clear: both;
    30. }
    31. .clear:before, .clear:after {
    32. content: "";
    33. display: table;
    34. }
    35. .clear:after {
    36. clear: both;
    37. }
    38. .form-div {
    39. background-color: rgba(255, 255, 255, 0.27);
    40. border-radius: 10px;
    41. border: 1px solid #aaa;
    42. width: 424px;
    43. margin-top: 150px;
    44. margin-left:1050px;
    45. padding: 30px 0 20px 0px;
    46. font-size: 16px;
    47. box-shadow: inset 0px 0px 10px rgba(255, 255, 255, 0.5), 0px 0px 15px rgba(75, 75, 75, 0.3);
    48. text-align: left;
    49. }
    50. .form-div input[type="text"], .form-div input[type="password"], .form-div input[type="email"] {
    51. width: 268px;
    52. margin: 10px;
    53. line-height: 20px;
    54. font-size: 16px;
    55. }
    56. .form-div input[type="checkbox"] {
    57. margin: 20px 0 20px 10px;
    58. }
    59. .form-div input[type="button"], .form-div input[type="submit"] {
    60. margin: 10px 20px 0 0;
    61. }
    62. .form-div table {
    63. margin: 0 auto;
    64. text-align: right;
    65. color: rgba(64, 64, 64, 1.00);
    66. }
    67. .form-div table img {
    68. vertical-align: middle;
    69. margin: 0 0 5px 0;
    70. }
    71. .footer {
    72. color: rgba(64, 64, 64, 1.00);
    73. font-size: 12px;
    74. margin-top: 30px;
    75. }
    76. .form-div .buttons {
    77. float: right;
    78. }
    79. input[type="text"], input[type="password"], input[type="email"] {
    80. border-radius: 8px;
    81. box-shadow: inset 0 2px 5px #eee;
    82. padding: 10px;
    83. border: 1px solid #D4D4D4;
    84. color: #333333;
    85. margin-top: 5px;
    86. }
    87. input[type="text"]:focus, input[type="password"]:focus, input[type="email"]:focus {
    88. border: 1px solid #50afeb;
    89. outline: none;
    90. }
    91. input[type="button"], input[type="submit"] {
    92. padding: 7px 15px;
    93. background-color: #3c6db0;
    94. text-align: center;
    95. border-radius: 5px;
    96. overflow: hidden;
    97. min-width: 80px;
    98. border: none;
    99. color: #FFF;
    100. box-shadow: 1px 1px 1px rgba(75, 75, 75, 0.3);
    101. }
    102. input[type="button"]:hover, input[type="submit"]:hover {
    103. background-color: #5a88c8;
    104. }
    105. input[type="button"]:active, input[type="submit"]:active {
    106. background-color: #5a88c8;
    107. }
    108. .err_msg{
    109. color: red;
    110. padding-right: 170px;
    111. }
    112. #password_err,#tel_err{
    113. padding-right: 195px;
    114. }
    115. #reg_btn{
    116. margin-right:50px; width: 285px; height: 45px; margin-top:20px;
    117. }
    118. #checkCode{
    119. width: 100px;
    120. }
    121. #changeImg{
    122. color: aqua;
    123. }

    22.IMG文件(登陆注册的IMG文件)

    1.a.jpg

    2.Desert1.jpg

    3.reg_bg_min.jpg

    23.addBrand.jsp(显示添加页面)

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>添加品牌title>
    6. head>
    7. <body>
    8. <h3>添加品牌h3>
    9. <form action="/brand_demo/addServlet" method="post">
    10. 品牌名称:<input type="text" name="brandName"><br>
    11. 企业名称:<input type="text" name="companyName"><br>
    12. 排序: <input type="text" name="ordered"><br>
    13. 描述信息:<textarea rows="5" cols="3" name="description">textarea><br>
    14. 状态:
    15. <input type="radio" name="status"value="0">禁用
    16. <input type="radio" name="status"value="1">启用<br>
    17. <input type="submit" value="提交">
    18. form>
    19. body>
    20. html>

    24.brand.jsp(实现查找的页面)

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. <html>
    4. <script>
    5. function add(){
    6. let flag = window.confirm("您确定要添加数据吗?");
    7. if (flag==true){
    8. window.location="/brand_demo/addBrand.jsp";
    9. }else{
    10. window.location="/brand_demo/brand.jsp";
    11. }
    12. }
    13. function del(){
    14. let flag = window.confirm("您确定要删除数据吗?");
    15. if (flag==true){
    16. window.location="/brand_demo/brand.jsp";
    17. }else{
    18. return 0;
    19. }
    20. }
    21. script>
    22. <head>
    23. <title>Titletitle>
    24. head>
    25. <body>
    26. <center>
    27. <h1>欢迎${user.username}访问阿里云数据库h1>
    28. <input type="button" value="添加数据" onclick="add()">
    29. <hr>
    30. <table border="1" cellspacing="0" width="800">
    31. <tr>
    32. <th>序号th>
    33. <th>品牌名称th>
    34. <th>企业名称th>
    35. <th>排序th>
    36. <th>品牌介绍th>
    37. <th>状态th>
    38. <th>操作th>
    39. tr>
    40. <c:forEach items="${brands}" var="brand" varStatus="status">
    41. <tr align="center">
    42. <%-- <td>${brand.id}td>--%>
    43. <td>${status.count}td>
    44. <td>${brand.brandName}td>
    45. <td>${brand.companyName}td>
    46. <td>${brand.ordered}td>
    47. <td>${brand.description}td>
    48. <c:if test="${brand.status == 0 }">
    49. <td>禁用td>
    50. c:if>
    51. <c:if test="${brand.status == 1 }">
    52. <td>启用td>
    53. c:if>
    54. <td><a href="/brand_demo/selectByIdServlet?id=${brand.id}">修改a>/
    55. <a href="/brand_demo/deleteServlet?id=${brand.id}" onclick="del()">删除a>td>
    56. tr>
    57. c:forEach>
    58. table>
    59. center>
    60. body>
    61. html>

    25.delete.jsp(实现删除的页面)

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: HP
    4. Date: 2022/8/21
    5. Time: 16:05
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <script>
    11. function del(){
    12. let flag = window.confirm("您确定要返回主页面吗?");
    13. if (flag=true){
    14. window.location="/brand_demo/selectAllServlet";
    15. }
    16. }
    17. script>
    18. <head>
    19. <title>Deletetitle>
    20. head>
    21. <body>
    22. <center>
    23. <h1>提醒您!!<br>
    24. 已经删除了id为${id}的数据h1>
    25. <input type="button"value="返回主页面"onclick="del()">
    26. center>
    27. body>
    28. html>

    26.index.html(主页面,显示brand.jsp的链接)

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <center>
    9. <h2>h2>
    10. <h1> 欢迎访问h1>
    11. <a href="/brand_demo/selectAllServlet" >点击查询所有数据库数据a>
    12. center>
    13. body>
    14. html>

    27.login.jsp(实现登录的页面)

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <title>logintitle>
    7. <link href="css/login.css" rel="stylesheet">
    8. head>
    9. <body>
    10. <div id="loginDiv" style="height: 350px">
    11. <%--action一般指向处理这个页面的servlet,当点击submit的时候,就会把表单里面面的变量,提交到对应的servlet里面去--%>
    12. <form action="/brand_demo/loginServlet" id="form">
    13. <h1 id="loginMsg">LOGIN INh1>
    14. <div id="errorMsg">${login_msg}${register_msg}div>
    15. <p>Username:<input id="username" name="username" type="text" value="${cookie.username.value}">p>
    16. <p>Password:<input id="password" name="password" type="password" value="${cookie.password.value}">p>
    17. <p>Remember:<input id="remember" value="1" name="remember" type="checkbox">p>
    18. <div id="subDiv">
    19. <input type="submit" class="button" value="login up">
    20. <input type="reset" class="button" value="reset">   
    21. <a href="register.jsp">没有账号?a>
    22. div>
    23. form>
    24. div>
    25. body>
    26. html>

    28.register.jsp(实现注册的页面)

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. html>
    3. <html lang="en">
    4. <head>
    5. <meta charset="UTF-8">
    6. <title>欢迎注册title>
    7. <link href="css/register.css" rel="stylesheet">
    8. head>
    9. <body>
    10. <div class="form-div">
    11. <div class="reg-content">
    12. <h1>欢迎注册h1>
    13. <span>已有帐号?span> <a href="login.html">登录a>
    14. div>
    15. <form id="reg-form" action="/brand_demo/registerServlet" method="post">
    16. <table>
    17. <tr>
    18. <td>用户名td>
    19. <td class="inputs">
    20. <input name="username" type="text" id="username">
    21. <br>
    22. <span id="username_err" class="err_msg" >${register_msg}${error_msg}span>
    23. td>
    24. tr>
    25. <tr>
    26. <td>密码td>
    27. <td class="inputs">
    28. <input name="password" type="password" id="password">
    29. <br>
    30. <span id="password_err" class="err_msg" style="display: none">密码格式有误span>
    31. td>
    32. tr>
    33. <tr>
    34. <td>验证码td>
    35. <td class="inputs">
    36. <input name="checkCode" type="text" id="checkCode">
    37. <img id="CheckCodeImage" src="/brand_demo/checkCodeServlet" onclick="check()">
    38. <a href="#" id="changeImg" onclick="check()">看不清?a>
    39. td>
    40. tr>
    41. table>
    42. <div class="buttons">
    43. <input value="注 册" type="submit" id="reg_btn">
    44. div>
    45. <br class="clear">
    46. form>
    47. div>
    48. <script>
    49. function check(){
    50. // 利用时间来当作参数来获取图片
    51. let milliseconds = new Date().getMilliseconds();//获取当前时间的毫米值
    52. document.getElementById("CheckCodeImage").src = "/brand_demo/checkCodeServlet?"+milliseconds;
    53. }
    54. script>
    55. body>
    56. html>

    29.update.jsp(显示更新的界面)

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    3. <html>
    4. <head>
    5. <title>修改数据title>
    6. head>
    7. <body>
    8. <h3>回显数据,修改数据h3>
    9. <h3>可以封装成一个brand的数据,然后利用el表达式来回显数据h3>
    10. <form action="/brand_demo/updateServlet" method="post">
    11. <%-- 隐藏域--%>
    12. <input type="hidden" name="id" value="${brand.id}">
    13. 品牌名称:<input type="text" name="brandName" value="${brand.brandName}"><br>
    14. 企业名称:<input type="text" name="companyName"value="${brand.companyName}"><br>
    15. 排序: <input type="text" name="ordered" value="${brand.ordered}"><br>
    16. 描述信息:<textarea rows="10" cols="3" name="description" >${brand.description}textarea><br>
    17. 状态:
    18. <c:if test="${brand.status == 0}">
    19. <input type="radio" name="status"value="0" checked >禁用
    20. <input type="radio" name="status"value="1">启用<br>
    21. c:if>
    22. <c:if test="${brand.status == 1}">
    23. <input type="radio" name="status"value="0" >禁用
    24. <input type="radio" name="status"value="1" checked>启用<br>
    25. c:if>
    26. <input type="submit" value="提交">
    27. form>
    28. body>
    29. html>

    30.pom文件(存储的关于Java的一些依赖)

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>org.examplegroupId>
    7. <artifactId>brand_demoartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <properties>
    10. <maven.compiler.source>18maven.compiler.source>
    11. <maven.compiler.target>18maven.compiler.target>
    12. properties>
    13. <packaging>warpackaging>
    14. <build>
    15. <plugins>
    16. <plugin>
    17. <groupId>org.apache.tomcat.mavengroupId>
    18. <artifactId>tomcat7-maven-pluginartifactId>
    19. <version>2.2version>
    20. plugin>
    21. <plugin>
    22. <groupId>org.apache.maven.pluginsgroupId>
    23. <artifactId>maven-compiler-pluginartifactId>
    24. <configuration>
    25. <source>17source>
    26. <target>17target>
    27. configuration>
    28. plugin>
    29. plugins>
    30. build>
    31. <dependencies>
    32. <dependency>
    33. <groupId>javax.servletgroupId>
    34. <artifactId>javax.servlet-apiartifactId>
    35. <version>3.1.0version>
    36. <scope>providedscope>
    37. dependency>
    38. <dependency>
    39. <groupId>commons-iogroupId>
    40. <artifactId>commons-ioartifactId>
    41. <version>2.6version>
    42. dependency>
    43. <dependency>
    44. <groupId>org.mybatisgroupId>
    45. <artifactId>mybatisartifactId>
    46. <version>3.5.5version>
    47. dependency>
    48. <dependency>
    49. <groupId>mysqlgroupId>
    50. <artifactId>mysql-connector-javaartifactId>
    51. <version>8.0.29version>
    52. dependency>
    53. <dependency>
    54. <groupId>junitgroupId>
    55. <artifactId>junitartifactId>
    56. <version>4.13.2version>
    57. <scope>Testscope>
    58. dependency>
    59. <dependency>
    60. <groupId>org.slf4jgroupId>
    61. <artifactId>slf4j-apiartifactId>
    62. <version>1.7.36version>
    63. dependency>
    64. <dependency>
    65. <groupId>ch.qos.logbackgroupId>
    66. <artifactId>logback-classicartifactId>
    67. <version>1.2.3version>
    68. dependency>
    69. <dependency>
    70. <groupId>ch.qos.logbackgroupId>
    71. <artifactId>logback-coreartifactId>
    72. <version>1.2.3version>
    73. dependency>
    74. <dependency>
    75. <groupId>jstlgroupId>
    76. <artifactId>jstlartifactId>
    77. <version>1.2version>
    78. dependency>
    79. <dependency>
    80. <groupId>taglibsgroupId>
    81. <artifactId>standardartifactId>
    82. <version>1.1.2version>
    83. dependency>
    84. dependencies>
    85. project>

    31.展示页面

    (算了,不想展示了,如果有哪位朋友愿意来看的话,给我留言我私信给你)

  • 相关阅读:
    ORB_SLAM3 判断关键帧与创建关键帧
    Vim实用技巧_7.模式匹配和查找
    【21天学习挑战赛—Java编程进阶之路】(6)
    JAVA_SSM+VUE校园二手物品交易平台(含论文)源码
    【自然语言处理】seq2seq模型—机器翻译
    cleanmymacX4.14免费版mac清除浏览器缓存软件
    向毕业妥协系列之机器学习笔记:监督学习-回归与分类(一)
    mysql与磁盘的关系
    MATLAB学习笔记(系统学习)
    使用GD32F207的高级定时器来产生PWM波出现的隐藏BUG
  • 原文地址:https://blog.csdn.net/qq_51272114/article/details/126527074