zoukankan      html  css  js  c++  java
  • springmvc中使用servletAPI中的httprequest和response

    1  发布一个接口   在方法参数中传入servlet相关的HttpServletRequest ...就可以在控制器中使用request对象。 这只是简单的测试helloword级别的不包含任何业务逻辑

    @Controller
    @RequestMapping("/ServiceAPIService")
    public class ServiceAPIService {
        
        public final static String SUCCEEDD="show";
        
        
        @RequestMapping(value="/testServlet",method=RequestMethod.GET)
        public String testServlet(HttpServletRequest request,HttpServletResponse response){
            
            System.out.println("testServlet...........request="+request+" ,response="+response);
            String url=request.getRequestURL().toString();
            System.out.println(url);
        
            return SUCCEEDD;
        }
        
    }

    2   测试  访问http://localhost:8080/springmvc/ServiceAPIService/testServlet

      可以看到request对象已经获取到正确的url

    使用 Writer响应给客户端一个消息 hello word

        @RequestMapping(value="/testServletWrite",method=RequestMethod.GET)
        public void testServletWrite(HttpServletRequest request,HttpServletResponse response,Writer out) throws IOException{
            
            System.out.println("testServlet...........request="+request+" ,response="+response+"Writer="+out);
            String url=request.getRequestURL().toString();
            System.out.println(url);
    //响应给客户端的消息 out.write(
    "hello word"); }

    测试Writer

    浏览器测试

    页面上打印出 helloword

  • 相关阅读:
    java+opencv实现图像灰度化
    java实现高斯平滑
    hdu 3415 单调队列
    POJ 3368 Frequent values 线段树区间合并
    UVA 11795 Mega Man's Mission 状态DP
    UVA 11552 Fewest Flops DP
    UVA 10534 Wavio Sequence DP LIS
    UVA 1424 uvalive 4256 Salesmen 简单DP
    UVA 1099 uvalive 4794 Sharing Chocolate 状态DP
    UVA 1169uvalive 3983 Robotruck 单调队列优化DP
  • 原文地址:https://www.cnblogs.com/sunjiqiang/p/5595839.html
Copyright © 2011-2022 走看看