Results 1 to 4 of 4

Thread: Can't establish servlet-mapping.

  1. #1
    Join Date
    Jul 2008
    Posts
    182

    Default Can't establish servlet-mapping.

    Hi with spring mvc 3.1 and jboss 7.1, I am trying to map a path /category/image/* into a servlet CategoryImageServlet.java class, but it doesn't establish the mapping.

    Here is web.xml file:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <!-- The master configuration file for this Spring web application -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/config/web-application-config.xml
    </param-value>
    </context-param>

    <!-- Loads the Spring web application context -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>

    <!-- Enables use of HTTP methods PUT and DELETE -->
    <filter>
    <filter-name>httpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMet hodFilter</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>httpMethodFilter</filter-name>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    </filter-mapping>

    <!-- Enables Spring Security -->
    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFil terProxy</filter-class>
    </filter>

    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    </filter-mapping>

    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all *.spring requests to the DispatcherServlet for handling -->
    <servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- category image servlet -->

    <servlet>
    <servlet-name>categoryImageServlet</servlet-name>
    <servlet-class>ix.houseware.category.servlet.CategoryImageS ervlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>categoryImageServlet</servlet-name>
    <url-pattern>/category/image/*</url-pattern>
    </servlet-mapping>

    </web-app>
    CategoryImageServlet.java:
    public class CategoryImageServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(CategoryImageServlet.class);

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    logger.info("calling CategoryImageServlet.....");
    }
    list.jsp:
    <td><img src="/category/image/${category.categoryId}" /></td>
    Any help would be very appreciated.
    Thanks
    Sam

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    And why should it... You have a servlet mapped to / (all incoming requests)... So indeed your image servlet is never going to be called. Swap the servlet-mapping elements or create a Controller instead of a servlet and let that handle your images.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2008
    Posts
    182

    Default

    Got that resolved, thanks a lot.
    Now I encountered another issue that I don't know how to auto-inject a service object in the servlet class, since I want to use a service object to get an image byte from database.

    Here is my servlet class:
    public class CategoryImageServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = Logger.getLogger(CategoryImageServlet.class);
    @Autowired
    private CategoryImageService categoryImageService;

    // @Autowired
    // public CategoryImageServlet(CategoryImageService categoryImageService) {
    // this.categoryImageService = categoryImageService;
    // }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
    // Get ID from request.
    String categoryId = request.getParameter("id");
    System.out.println("CategoryImageServlet.......... : "+categoryId);
    // Check if ID is supplied to the request.
    if (categoryId == null) {
    // Do your thing if the ID is not supplied to the request.
    // Throw an exception, or send 404, or show default/warning image, or just ignore it.
    response.sendError(HttpServletResponse.SC_NOT_FOUN D); // 404.
    return;
    }

    List<CategoryImages> ciList = (List<CategoryImages>) categoryImageService.findImagesByCategoryId(Intege r.parseInt(categoryId), 0, categoryImageService.count()) ;
    // Lookup Image by ImageId in database.
    // Do your "SELECT * FROM Image WHERE ImageID" thing.
    // Image image = imageDAO.find(imageId);
    Thanks
    Sam

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    You cannot. The servlet isn't spring managed and as such isn't going to be dependency injected. You would have to do a lookup instead of dependency injection.

    As I mentioned earlier I suggest you write a controller instead of a servlet and let the DispatcherServlet handle and dispatch the request.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •