Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

servlets - Loading JSP pages from custom sources

Would it be possible to execute a JSP page and capture its output outside of a web application? Mode specifically, in my case there still exists a usual web application, but it loads JSP pages not from its classpath, but from an arbitrary source. It seems like I cannot simply get RequestDispatcher and point it to a JSP file on disk.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you're better off with a templating engine like velocity. This provides a clean infrastructure for dynamic content that's clearly different from the jsp/servlet stuff that you are asking fore.

That said, I've seen applications that copy jsps into their deployed directory in order for the container to pick them up and translate them. Should you do this, please note that this limits your future options:

  • you rely upon your application to be "exploded" - e.g. it can't run directly out of a WAR archive (this might limit your deployment options)
  • making jsps editable at runtime might open up security holes if you don't disable scriptlets (also if you do disable, but it'll be somewhat harder...). Disabling scriptlets prohibits real Java code in the jsps, you're limited to tag libraries then.
  • You'll need a Java compiler available at runtime, which you might not want to have in production systems - e.g. you cannot precompile your jsps before deployment. Also you pay the usual jsp-translation-penalty at runtime in your productive system.

web.xml configuration for disabling scripting:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>

I hope this web.xml snippet went through, the preview didn't show it correctly...

Update: Tried to make xml-snippet display correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...