⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e1043. getting a request header in a servlet.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
This example demonstrates how to get the value of a request header in either a GET or POST request. 
    // See also e1035 The Quintessential Servlet
    
    // This method is called by the servlet container to process a GET request.
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGetOrPost(req, resp);
    }
    
    // This method is called by the servlet container to process a POST request.
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGetOrPost(req, resp);
    }
    
    // This method handles both GET and POST requests.
    private void doGetOrPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/plain");
    
        // Get the value of a request header; the name is case-insensitive
        String name = "user-agent";
        String value = req.getHeader(name);
        if (value == null) {
            // The request header was not present
        }
    
        // Get all request headers
        Enumeration enum = req.getHeaderNames();
        for (; enum.hasMoreElements(); ) {
            // Get the name of the request header
            name = (String)enum.nextElement();
            out.println(name);
    
            // Get a value of the request header
            value = req.getHeader(name);
    
            // If the request header can appear more than once, get all values
            Enumeration valuesEnum = req.getHeaders(name);
            for (; valuesEnum.hasMoreElements(); ) {
                // Get a value of the request header
                value = (String)valuesEnum.nextElement();
    
                out.println("    "+value);
            }
        }
        out.close();
    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -