Package com.nhaycungbibi.controller

Source Code of com.nhaycungbibi.controller.CategoryController

package com.nhaycungbibi.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubeilike.entity.Category;
import com.tubeilike.entity.Tube;
import com.tubeilike.model.CategoryModel;
import com.tubeilike.model.TubeModel;

public class CategoryController extends HttpServlet {
  private static final long serialVersionUID = 1L;

  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    // search by input key.
    try {
      String path = ((HttpServletRequest) request).getRequestURI();
      String[] splittedURI = path.split("/");
      int page = 1;
      String cateAlias = "";
      if (splittedURI.length == 3) {
        cateAlias = splittedURI[splittedURI.length - 1];

      } else if (splittedURI.length == 4) {
        cateAlias = splittedURI[splittedURI.length - 2];
        try {
          page = Integer
              .parseInt(splittedURI[splittedURI.length - 1]);
        } catch (Exception e) {
          page = 1;
        }
      } else {
        response.sendRedirect("/home");
      }
      cateAlias = URLDecoder.decode(cateAlias, "UTF8");
      System.out.println("Cate to search : " + cateAlias);
      Category cate = CategoryModel.getByCateAlias(cateAlias);
      if (cate != null) {
        List<Tube> result = TubeModel
            .getByCategory(cateAlias, page, 12);
        request.setAttribute("title", cate.getTitle().getValue() + " - NhayCungBiBi");
        request.setAttribute("description", "Chuyên trang clip nhảy cùng bibi.");
        request.setAttribute("keyword", "nhảy cùng bibi, nhaycungbibi.com, nhaycungbibi, video clip, youtube, mix clip, tubeonfire.com, tubeonfire, your clip,  sharing, camera phone, video phone, free...");
        request.setAttribute("url", request.getRequestURL());
        request.setAttribute("result", result);
        request.setAttribute("currentPage", page);
        request.setAttribute("totalResult", cate.getCount());
        request.setAttribute("cate", cateAlias);
        request.setAttribute("cateString",
            cateAlias.replaceAll("\\+", " "));
        request.getRequestDispatcher("/category_show.jsp").forward(
            request, response);
      } else {
        System.out.println("Error path.");
        response.sendRedirect("/home");
      }
    } catch (Exception e) {
      System.out.println("Exception.");
      e.printStackTrace();
      response.sendRedirect("/home");
    }
  }
}
TOP

Related Classes of com.nhaycungbibi.controller.CategoryController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.