rest入门实践之四:(转载)
转载处:http://blog.csdn.net/exsuns
jax-rs提供了一个注解注入的方式来取得客户端的信息
当jax-rs服务基于servlet发布的时候 ,还可以通过@Context注入servlet中的ServletConfig , ServletContext ,HttpServletRequest , HttpServletResponse
然后REST就可以通过sessionid来保持住用户状态
使用方法:
建个web工程,并加入jax-rs(jsr311)的JAR包
新建一个类
- @Path("UserContext")
- public class UserContext {
- @Context UriInfo uriInfo;
- @Context HttpHeaders httpHeaders;
- @Context SecurityContext sc;
- @Context Request req;
- @Context Response resp;
- @Context HttpServletResponse response;
- @Context HttpServletRequest request;
- @GET
- public String hi(@QueryParam("name") String yourName ){
- if(yourName!=null)
- request.getSession().setAttribute("name", yourName);
- String username = (String) request.getSession().getAttribute("name");
- if(username!=null){
- System.out.println(request.getSession().getId() + ":" + username);
- }
- else{
- System.out.println(request.getSession().getId() + "没有用户");
- }
- return null;
- }
- }
- <!--在web.xml加入-->
- <servlet>
- <display-name>JAX-RS REST Servlet</display-name>
- <servlet-name>JAX-RS REST Servlet</servlet-name>
- <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>JAX-RS REST Servlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
部署部运行
http://localhost:8080/rest/services/UserContext
后台会提示:
- A46756539D2E39CC2CFFCB3FE1C99E70没有用户
然后运行
http://localhost:8080/rest/services/UserContext?name=hello
后台会出现
- A46756539D2E39CC2CFFCB3FE1C99E70:hello