一.题目

利用JavaBean实现用户类,包含姓名、性别、爱好,爱好需要用多选框

实现表单1进行获取数据,表单2显示获取结果。

利用Servlet实现逻辑代码

二.实现效果

三.具体实现

1.User实体类

package com.hjj.pojo;/** * @author:嘉佳 Date:2023/11/6 0:10 **/public class User {private String name;private String sex;private String[] hobbies;public User() {}public User(String name, String sex, String[] hobbies) {this.name = name;this.sex = sex;this.hobbies = hobbies;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public String[] getHobbies() {return hobbies;}public void setHobbies(String[] hobbies) {this.hobbies = hobbies;}}

2.dataEntry.jsp

用户信息录入用户名:
性别:男女
爱好:吃饭睡大觉CRUD

3.showInfo.jsp

用户信息

用户信息

姓名:

性别:

爱好:

4.sevlet文件

package com.hjj.sevletgk.hw9;import com.hjj.pojo.User;import javax.servlet.ServletContext;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 java.io.IOException;/** * @author:嘉佳 Date:2023/11/11 16:20 **/@WebServlet("/show")public class showInfo extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");String name = req.getParameter("name");String sex = req.getParameter("sex");String[] hobbies = req.getParameterValues("hobbies");User user = new User(name, sex, hobbies);ServletContext servletContext = getServletContext();servletContext.setAttribute("user",user);resp.setCharacterEncoding("UTF-8");resp.sendRedirect("hw9/showUser.jsp");}}