• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How do you run a servlet in a jsp page?

crystal

Platinum Member
Like the title say,

I have a jsp page and a servlet and I want to call out this servlet in the jsp page.

thx.
 
<jsp:forward page="....." /> Where .... is the url at which the servlet can normally be accessed. If you can't already access the servlet by url then you have to make a mapping in web.xml

Just curious, why do you want to do this?
 
Basically, the servlet queries the db and returns the image. The jsp is where I display the image. I tried something like this <img src="/servlet/myservlet?param=blah&amp;param2=moreblah" > in the jsp but that didn't work. Also something like this,
<img src="<jsp:include /servlet/myservlet?param=blah&amp;param2=moreblah>" >
but no go, heh. Just looking for some simple example that point me to the right direction.
 
Servlets don't return values. What you should be doing is starting from the servlet (not the jsp), doing your db work and putting whatever info needs to be transferred to the jsp into the request. Then use a RequestDispatcher to send things over to the jsp which retrieves your img src from the request and goes about it's business.

If you absolutely must retrieve this info in the middle of your jsp processing then don't do it with a servlet. Do it with a pojo (plain old java object) and have the img src be the return value of whatever method you call to start this lookup. If you need the request, session and/or application variables handy for your lookup you might think about writing a custom tag. You can do that in regular java and I believe these variables are conveniently available in tags.
 
Oh, but thinking more about your example there, you could get it to work the way you have it set up:

<img src="<jsp:include page="/servlet/myservlet?param=blah&amp;param2=moreblah" />" >

I highlighted syntax mistakes that you made above. Now in the servlet get your output stream (getPrintStream() method of your HttpRequest object, I think) and just print out the url of the image that you want. That will then be flushed to the output and it will come out right where you put the <jsp:include /> tag in the jsp. It's really not how you're supposed to use servlets but it would work.
 
Back
Top