Image Src for calling a Servlet

后端 未结 3 1767
既然无缘
既然无缘 2021-01-29 06:15

I want to call a Servlet from img src. I have defined a Servlet class with name ImageProducerServlet and registered it in web.xml:


   

        
相关标签:
3条回答
  • 2021-01-29 06:41

    You need to prepend the context path. You preferably won't hardcode it as the context path is a server-controlled setting. You can obtain it from the HTTP request as follows:

    <img src="#{request.contextPath}/imageProducerServlet" id="id"/>
    

    An alternative is to use the HTML <base> tag and set it to the URL of the context root. This way every URL which doesn't start with / will be relative to it.

    See also:

    • How get the base URL?
    0 讨论(0)
  • 2021-01-29 06:56

    Just say

    <img src="imageProducerServlet" id="id"/>
    

    or

    <img src="/mycompany/imageProducerServlet" id="id"/>
    
    0 讨论(0)
  • 2021-01-29 07:00

    If this is working:

    http://localhost:7101/mycompany/imageproducerservlet
    

    then you need your img tag to look like that:

    <img src="/mycompany/imageProducerServlet" id="id"/>
    
    0 讨论(0)
提交回复
热议问题