I'm new to JSP, so here's the problem. I want to create a block of HTML code with dynamism in it. This block of code needs to be repeated/reused multipl开发者_如何学Goe times in multiple places on my site. It's ugly to create a method with lots of
responseOut.println("<html> text with escaped characters")
So I'm wondering if JSP can be used to create reusable (callable or addressable by Class.methodname) methods.
It's easy to do this in PHP within the PHP framework.
I guess it all depends to the extent that JSP is s precompilation method? or run dynamically in the webserver...
I'm working in Eclipse (with GAE), so any comments and hints in this framework would also be appreciated.
Thanx Dan
You can encapsulate parts of Java (or JSP code) in tags that can be reused in other contexts. Have a look at this tutorial for a first introduction.
At its simplest, you can put it in a JSP fragment file and use <jsp:include>
to include it. The "dynamism" can just be achieved using taglibs/EL. How exactly to do it depends on the sole functional requirement which is yet unclear. At least, HTML code does definitely not belong in a Servlet class.
However the statement "<html> text with escaped characters"
makes me think that all you really need is the JSTL <c:out>
tag. It will by default escape HTML entities like <
, >
, etc to prevent HTML code injection (and XSS holes).
精彩评论