0

How to call a function written in a JSP from a HTML page? I have declared a function to load an image from server in JSP page. Now I want to show that image in another html page by calling that JSP function in HTML page.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Vinay Gayakwad
  • 526
  • 4
  • 10
  • 20
  • 6
    The question is not clear. It seems you have a function declared in a JSP page, but from where do you want to call the function? Please clarify your question. – Felix Kling Feb 11 '11 at 16:13
  • i have declared a function to load a image from server in jsp page. now i want to show that image another html page by calling that function in html page... – Vinay Gayakwad Feb 11 '11 at 16:21
  • 1
    Make a JSP page with no HTML and just your functions definition, then include it. (Can't use servlets ?) – POSIX_ME_HARDER Feb 11 '11 at 16:23

2 Answers2

2

i have declared a function to load a image from server in jsp page. now i want to show that image another html page by calling that function in html page.

That's not how it works. Webbrowser sends HTTP request to webserver. Webserver executes some Java/JSP/Servlet code based on the HTTP request (URL, parameters, pathinfo, etc). Java/JSP/Servlet code produces a bunch of HTML code (which can contain CSS/JS code as well). Webserver sends HTML code back to webbrowser as HTTP response. Webbrowser displays HTML. If you rightclick page in webbrowser and choose View Source, then you should not see any line of Java/JSP/Servlet code.

You just need to write your Java/JSP/Servlet code so that it produces exactly that HTML you want. Displaying images in HTML is to be done by <img> tag whose src attribute should point to the URL of the image.

<img src="foo.png" />

Just put that as-is in the JSP. With the above example, put the image file in same folder as the JSP as well.

If the images are however to be retrieved from an external resource as for example a database, then you need to create a Servlet which obtains an InputStream of the image from the external resource based on the parameters/pathinfo provided by the HTTP request and writes it to the OutputStream of the HTTP response along a set of correct response headers (content type, length, etc). Finally let the URL in the src attribute of the HTML <img> element point to the servlet instead.

<img src="imageservlet/foo.png" />

You can find a more detailed example of the servlet in this answer.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

Via an HTTP Request - i.e. submitting the page to the web container where the JSP executes. This is a very normal pattern.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242