• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I use JSTL within a Servlet?  RSS feed

 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We often use jstl c:import within JSPs for includes. However, we have a legacy application that has no JSPs. All of the html output is generated through servlets. Can I still use JSTL somehow in a servlet to get the c:import in the same manner? I would think you could because a JSP ends up being compiled into a servlet.

Thanks
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Charles,

unfortunately I don't know if there's an easy or better say reasonable way to use JSTL features inside Servlets. But one way to find out would be to have a look at the generated code a Servlet container produces for JSPs using JSTL features. Tomcat for example can be configured to (or does by default) place the generated Java source files inside its "work" directory. So you obviously have to create a sample JSP and see what code Tomcat's JSP engine generates for it. I'm not sure if this works for other container though.

Marco
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot.

Are you wanting to include a resource from within the same web application? If so, a request dispatcher can be used for that (which is the same mechanism that <c:import> uses under the covers when the resource is local).
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it's a resource outside the web application.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, you'll want to check out java.net.URLConnection, or projects like HttpClient.
 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say it's an inside resource. How would I use the getDispatcher method like a <c:import>?

 
Charles Owen
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Through some research and looking through some Java docs, I was able to find what I was looking for. You're actually able to access another context within a Servlet. This is a legacy, very legacy, application that I am maintaining. There are no JSPs in this project. Believe it or not, just Servlets. Do they call this the Model T instead of the Model 1? lol

Here's the code:



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Model T".... I love it!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!