JAX RS – 18 – @Context

Merhaba Arkadaslar
Bu bolumde @Context annotation’ini inceleyecegiz. @Context annotation’ini asagidaki tiplere ait objeleri inject etmek icin kullanabiliriz. Onceki orneklerimizde HttpHeaders ve UriInfo icin @Context annotation’ini kullandik.

  • javax.ws.rs.core.HttpHeaders
  • javax.ws.rs.core.UriInfo
  • javax.ws.rs.core.Request
  • javax.servlet.http.HttpServletRequest
  • javax.servlet.http.HttpServletResponse
  • javax.servlet.ServletConfig
  • javax.servlet.ServletContext
  • javax.ws.rs.core.SecurityContext

Maven Dependency

Oncelikle pom.xml dosyamiza servlet icin dependency tanimini ekleyelim.

pom.xml

<properties>
.....
<servlet.version>3.1.0</servlet.version>
</properties>


.....
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>${servlet.version}</version>
</dependency>
....
		 

ContextResource.java
@Context annotation’ini kullanarak ;

  • ServletContext
  • ServletConfig
  • HttpServletRequest
  • HttpServletResponse objelerini inject edelim.
package _12.context.service;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

@Path("/context")
public class ContextResource {

	@GET
	@Path("/servletcontext")
	public String injectServletContext(@Context ServletContext context) {
		String message = "injectServletContext is called.";
		String contextPath = context.getContextPath();
		return message + "<br/>" + contextPath;
	}

	@GET
	@Path("/servletconfig")
	public String injectServletConfig(@Context ServletConfig config) {
		String message = "injectServletConfig is called.";
		return message + "<br/> " + config.getServletName();
	}

	@GET
	@Path("/request")
	public String injectHttpServletRequest(@Context HttpServletRequest request) {
		String message = "injectHttpServletRequest is called.";
		return message + "<br/> " + request.getPathInfo();
	}

	@GET
	@Path("/response")
	public String injectHttpServletResponse(@Context HttpServletResponse response) {
		String message = "injectHttpServletResponse is called.";
		return message + "<br/> " + response.getCharacterEncoding();
	}

}

http://localhost:8080/injavawetrust.jersey.tutorial/context/servletcontext
http://localhost:8080/injavawetrust.resteasy.tutorial/context/servletcontext
http://localhost:8080/injavawetrust.jersey.tutorial/context/servletconfig
http://localhost:8080/injavawetrust.resteasy.tutorial/context/servletconfig
http://localhost:8080/injavawetrust.jersey.tutorial/context/request
http://localhost:8080/injavawetrust.resteasy.tutorial/context/request
http://localhost:8080/injavawetrust.jersey.tutorial/context/response
http://localhost:8080/injavawetrust.resteasy.tutorial/context/response

Github kaynak kodlar / source folder
injavawetrust.resteasy
injavawetrust.jersey

Yazimi burada sonlandiriyorum.
Herkese Bol Javali Gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.