• 吴裕雄--天生自然JAVA开发JSP-Servlet学习笔记:servlet获取表单请求参数并在客户端显示


    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import java.io.IOException;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     *
     * @author Administrator
     */
    @WebServlet(name = "firstServlet", urlPatterns = {"/FirstServlet"})
    public class FirstServlet extends HttpServlet {
    
        /**
         *
         * @param request
         * @param response
         * @throws ServletException
         * @throws IOException
         */
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            // 设置解码方式
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            // 获取name的请求参数值
            String name = request.getParameter("name");
            // 获取gender的请求参数值
            String gender = request.getParameter("gender");
            // 获取color的请求参数值
            String[] color = request.getParameterValues("color");
            // 获取country的请求参数值
            String national = request.getParameter("country");
            // 获取页面输出流
            PrintStream out = new PrintStream(response.getOutputStream());
            //输出HTML页面标签
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet测试</title>");
            out.println("</head>");
            out.println("<body>");
            // 输出请求参数的值:name
            out.println("您的名字:" + name + "<hr/>");
            // 输出请求参数的值:gender
            out.println("您的性别:" + gender + "<hr/>");
            // 输出请求参数的值:color
            out.println("您喜欢的颜色:");
            for (String c : color) {
                out.println(c + " ");
            }
            out.println("<hr/>");
            out.println("您喜欢的颜色:");
            // 输出请求参数的值:national
            out.println("您来自的国家:" + national + "<hr/>");
            out.println("</body>");
            out.println("</html>");
        }
    
    }
  • 相关阅读:
    Linux常用指令
    maven报错 java.lang.RuntimeException: com.google.inject.CreationException: Unable to create injector, see the following errors
    Idea 项目jdk环境配置
    Idea 设置maven配置文件settings.xml的位置
    IntelliJ IDEA常用快捷键总结
    Idea 一个窗口打开多个项目
    Git 下拉项目
    Git 删除本地保存的账号和密码
    mysql 查询奇偶数
    redis 短信验证码
  • 原文地址:https://www.cnblogs.com/tszr/p/12683813.html
Copyright © 2020-2023  润新知