Cómo configurar ContextPath para un enlace de imagen

i am trying to put context path for an image in HTML.<img src="/MyWeb/images/pageSetup.gif">

Here /MyWeb is the ContextPath which is hardcoded. How can i get dynamically.

i am using as <img src=contextPath+"/images/pageSetup.gif">but image is not displaying. Is there any option.

preguntado el 08 de noviembre de 11 a las 12:11

If you're indeed using JSP to generate HTML, you should really add that tag to the question. -

3 Respuestas

First of all, "Context path" is a term which is typically used in JSP/Servlet web applications, but you didn't mention anything about it. Your historial de preguntas however confirms that you're using JSP/Servlet. In the future, you should be telling and tagging what server side language you're using, because "plain HTML" doesn't have a concept of "variables" and "dynamic generation" at all. It are server side languages like JSP which have the capability of maintaining and accessing variables and dyamically generating HTML. JavaScript can be used, but it has its limitations as it runs in webbrowser, not in webserver.

The question as you initially have will only confuse answerers and yield completly unexpected answers. With question tags you reach a specific target group. If you use alone the [html] tag, you will get answers which assume that you're using pure/plain HTML without any server side language.


Back to your question: you can use ${pageContext.request.contextPath} para esto.

<img src="${pageContext.request.contextPath}/images/pageSetup.gif">

Ver también:

contestado el 23 de mayo de 17 a las 15:05

yes. i will send my entire code to you. I am putting my entire program here.<html> <head> <title>Document Printing Help</title> <meta http-equiv="cache-control" content="no-store"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="Expires" content="-1"> <link href="theme/Master.css" rel="stylesheet" type="text/css"> </head> <body> <div id="helpBody" style="padding-left:10px"> <img src="/dnweb/images/pageSetup.gif"> <div style="text-align:center"> <a href="javascript:self.close()">Close this Window</a> </div> </div> </body> </html> - Mdhar9e

I think you didn't understand me. You seem to be using JSP. You should tell that in your question. I've already posted an answer how to achieve what you want in JSP. - BalusC

You can't write JavaScript in the src attribute. To do what you want, try some code like this:

var img = new Image();
img.src = contextPath + "/images/pageSetup.gif";
document.getElementById('display').appendChild(img);

Here the target; the place where you want to display the image, is a div or span, with the id display.

Demo

With HTML, you'll have to take some extra traffic of producing an error, so you can replace the image, or you can send some traffic Google's way. Please do not use this:

<img src='notAnImage' onerror='this.src= contextPath + "/images/pageSetup.gif" '>

Demo

No utilices esto.

respondido 08 nov., 11:16

In HTML can i mention like above . - Mdhar9e

You can have inline JavaScript in HTML, but it isn't recommended and maintaining it will be difficult. But if you still want to do it anyway, I'll edit my answer and add it there. - Algún chico

Okay, I'm sorry, I lied about the HTML. You can't do what you want with inline JavaScript, unless you're fine with having to load another image first and replacing it. What is not working? - Algún chico

It is working in javascript but i am using only html with out js code. - Mdhar9e

If you weren't using JavaScript, you shouldn't have tagged it with JavaScript. You can do what you want with server-side code - Algún chico

You must use JavaScript for this.

First, have all images point to some dummy empty image on your domain while putting the real path as custom attribute:

<img src="empty.gif" real_src="/images/pageSetup.gif" />

Now have such JavaScript code in place to iterate over all the images and change their source to use the context path:

var contextPath = "/MyRealWeb";
window.onload = function() {
    var images = document.getElementByTagName("img");
    for (var i = 0; i < images.length; i++) {
        var image = images[i];
        var realSource = image.getAttribute("real_src") || "";
        if (realSource.length > 0)
            image.src = contextPath + realSource;
    }
};

respondido 08 nov., 11:17

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.