Servlet作业小练习

一.题目

利用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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户信息录入</title>
</head>
<body>
<%--
设置 input 标签的 type 为checkbox 或者 radio 时,
必须同时设置 input 标签的 value 属性,此时 value 的值就是提交给服务器的值
--%>
<form action=${pageContext.request.contextPath}/show method="post">
    用户名:<input type="text" name="name"><br>
    性别:<input type="radio" name="sex" value="男">男
        <input type="radio" name="sex" value="女">女<br>
    爱好:<input type="checkbox" name="hobbies" value="吃饭">吃饭
            <input type="checkbox"name="hobbies" value="睡大觉">睡大觉
            <input type="checkbox" name="hobbies" value="CRUD">CRUD<br>
    <input type="submit" value="提交">
</form>
</body>
</html>

3.showInfo.jsp

<%@ page import="com.hjj.pojo.User" %><%--
  Created by IntelliJ IDEA.
  User: ALASIJIA
  Date: 2023/11/11
  Time: 17:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
%>
<%
    // 从 ServletContext 中获取 JavaBean
    User user = (User) application.getAttribute("user");
%>

<h1>用户信息</h1>
<p>姓名:<%=user.getName()%>
</p>
<p>性别:<%=user.getSex()%>
</p>
<p>爱好:<% for (String hobby : user.getHobbies()) { %>
    <%= hobby %>
    <% } %>
</p>

</body>
</html>

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 {
    @Override
    protected 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");
    }
}
相关推荐
weixin_5375904521 小时前
《Java编程入门官方教程》第八章练习答案
java·开发语言·servlet
Ttang232 天前
Tomcat原理(4)——尝试手动Servlet的实现
java·开发语言·servlet·java-ee·tomcat·intellij-idea
loop lee3 天前
计算机网络 - HTTP 协议和万维网
java·网络协议·servlet·tomcat
不修×蝙蝠3 天前
搭建Tomcat(四)---Servlet容器
java·服务器·servlet·tomcat·搭建resquest
像污秽一样3 天前
简易记事本开发-(SSM+Vue)
java·vue.js·spring boot·spring·servlet·maven·mybatis
计算机学无涯5 天前
Servlet学习中遇到的一些问题及解决
servlet
测试工程师成长之路5 天前
解锁 Jenkins 搭建全攻略
运维·servlet·jenkins
进击的编程浪人5 天前
jsp中的四个域对象(Spring MVC)
servlet
怒放de生命20106 天前
jenkins 出现 Jenkins: 403 No valid crumb was included in the request
java·servlet·jenkins
爱小黄6 天前
从Servlet到Spring MVC,从Spring MVC到Spring BootC
spring·servlet·mvc