码迷,mamicode.com
首页 > 其他好文 > 详细

Request中的方法调用

时间:2015-09-23 10:26:54      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:

几种方法的调用:

package com.stono.servlet;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        System.out.println("do get method called!");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,
            IOException {
        String method = req.getMethod();
        System.out.println("method is " + method);
        Enumeration<String> headerNames = req.getHeaderNames();
        int i = 0;
        while (headerNames.hasMoreElements()) {
            String nextElement = headerNames.nextElement();
            String header = req.getHeader(nextElement);
            System.out.println(++i + ":" + nextElement + ":" + header);
        }
        System.out.println("===============COOKIE================");
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie);
            cookie.getName();
            cookie.getValue();
            cookie.getVersion();
        }
        System.out.println("================Remote===================");
        String remoteAddr = req.getRemoteAddr();
        String remoteHost = req.getRemoteHost();
        int remotePort = req.getRemotePort();
        String remoteUser = req.getRemoteUser();
        System.out.println(remoteAddr);
        System.out.println(remoteHost);
        System.out.println(remotePort);
        System.out.println(remoteUser);
        System.out.println("================Server===================");
        String serverName = req.getServerName();
        int serverPort = req.getServerPort();
        String servletPath = req.getServletPath();
        System.out.println(serverName);
        System.out.println(serverPort);
        System.out.println(servletPath);
        System.out.println("================Local===================");
        String localAddr = req.getLocalAddr();
        String localName = req.getLocalName();
        int localPort = req.getLocalPort();
        System.out.println(localAddr);
        System.out.println(localName);
        System.out.println(localPort);
        
        System.out.println("do post method called");
        String[] strings = req.getParameterValues("txt1");
        System.out.println(strings.length);
        for (String string : strings) {
            System.out.println(string);
        }
    }
}

 

Request中的方法调用

标签:

原文地址:http://www.cnblogs.com/stono/p/4831234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!