开发者

How to call a function written in JSP from a html page?

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 i开发者_Go百科n another html page by calling that JSP function in HTML page.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜