Filter と Servlet は同じ URL マッピングを持つ必要があります。もちろん、フィルターが最初に呼び出されます。
フィルタでデータを前処理し、サーブレットでリクエスト オブジェクトにデータを追加して JSP にディスパッチします。以下の例を参照してください。
@SlingFilter(order=1)
@Properties({
@Property(name="service.pid", value="com.videojet.hiresite.filters.AddNewUserFilter",propertyPrivate=false),
@Property(name="service.description",value="Preproceesing Filter", propertyPrivate=false),
@Property(name="service.vendor",value="XXX Tech", propertyPrivate=false),
@Property(name="pattern",value="/services/videojet/v1/.*", propertyPrivate=false)
// The Property Above property Maps your Filter to URL
})
public class AddNewUserFilter implements javax.servlet.Filter{
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
log.info("AddNewUserFilter Invoked Invoked");
// Preprocessing here and just call chain.doFilter(xxx);
そしてサーブレット
@SlingServlet(
paths={"/services/videojet/v1/AddNewUserController/view","/services/videojet/v1/AddNewUserController/userExists"
,"/services/videojet/v1/AddNewUserController/addUser"}
)
@Properties({
@Property(name="service.pid", value="com.videojet.hiresite.controllers.AddNewUserController",propertyPrivate=false),
@Property(name="service.description",value="Adds new user", propertyPrivate=false),
@Property(name="service.vendor",value="XXX Tech", propertyPrivate=false)
})
public class AddNewUserController extends SlingAllMethodsServlet{
/**
* @author Oliver Kaunds
*/
///content/dam/videojetdocuments
private static final long serialVersionUID = 1L;
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Reference
protected AddNewUserService addNewUserService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
log.info(":: Do GET Called ");
String path =request.getRequestURI();
log.info("PATH :: "+ path);
try{
if(path.equals("/services/videojet/v1/AddNewUserController/view"))
{
/* Do the dispatching here One Servlet can have more than one Request
Mappings . My Each mapping serves a purpose. */
List<HRRepresentative> list =addNewUserService.getHRRepresentative();
request.setAttribute("hrList",list );
HttpSession session = request.getSession();
String userOperation =(String)session.getAttribute("userOp");
request.setAttribute("userOp", userOperation);
session.removeAttribute("userOp");
//throw new Exception("My Exception");
RequestDispatcher dispatcher =request.getRequestDispatcher("/content/videojet/en/addnewuser.html"); // Dispatch to JSP here
dispatcher.forward(request, response);
}
これは試行済みのコードです