第二篇文章:浏览器向服务器发送请求,服务器向浏览器响应数据

springmvc讲的就是两件事:浏览器向服务器发送请求,服务器向浏览器响应数据


浏览器向服务器提交请求

1.当你不指定请求方式是get还是post的时候,默认就是采用Get方式提交请求

 你在浏览器地址栏中输入alpha/http就可以访问这个方法http

可以看出:请求默认是get请求(而不是post请求)

@RequestMapping("/alpha")
public class Controller
{
    @RequestMapping("/http")
    public void http(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        System.out.println(request.getMethod());//请求方式  GET
        System.out.println(request.getServletPath());//请求路径  /alpha/http

        Enumeration<String> enumeration=request.getHeaderNames();//把请求的消息头打印出来host: localhost:8080....
        while(enumeration.hasMoreElements())//把请求头的每一行name:value取出来
        {
            String name=enumeration.nextElement();
            String value=request.getHeader(name);

            System.out.println(name+": "+value);
        }

        System.out.println(request.getParameter("code"));
        //请求传入的参数,比如你输alpha/http?code=123,表明传入的参数就是123,那code就等于123,没有参数就是nul

    }
}

2.指定以get方式提交请求

    @RequestMapping("/students",method= RequestMethod.GET)
    @ResponseBody //表示返回的是一个字符串
    //第几页,1页展示几行数据
    public  String getStudents(int current, int limit)
    {
        System.o
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值