标签:
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
springmvc中使用servletAPI中的httprequest和response
标签:
原文地址:http://www.cnblogs.com/sunjiqiang/p/5595839.html