• 吴裕雄--天生自然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>");
        }
    
    }
  • 相关阅读:
    日期格式不合法
    DataGridView的Scrollbar通過編程不能自動下滾
    Sourcesafe shadown backup
    共享目錄突然不工作 
    VS2005編譯后版本如何控制
    WebBrowser用作RichTextBox
    怎樣設定MS Reporting自動橫向列印
    VSS 2005不需登錄,直接開啟的方法
    subreport cannot be shown
    An Algorithm Summary of Programming Collective Intelligence (5)
  • 原文地址:https://www.cnblogs.com/tszr/p/12683813.html
Copyright © 2020-2023  润新知