I have been learning JSP for a while now and I was curious in knowing how JSP files are converted into HTML. While learning about that process, I came into this concept that JSP files are generated to Java Servlet files and they are compiled into Servlet Class files. These class files are stored in JSP Container and when the corresponding JSP file is requested, the corresponding servlet class file is executed. My question is how JSP Container knows if the JSP file had changed from the old compiled one and in which manner it compares both the files?
Related
I have jsp and HTML files in my java EE project (I work with Eclipse)
My WEB-CONTENT file has been removed. I recovered it with Recuva.
I realized that JSTL and JDBC were damaged so I implemented them again, I do not have the same errors anymore.
But I do not understand why my code is illegible!
This jsp file is for authentification
i am new to advance java and trying to build a dynamic web application using eclipse. I have no idea how to link a ready-made/dynamic .html form with .jsp , although i have made a registration form in jsp but it is not looking nice.
One way to convert an HTML file to a JSP is to open a new JSP file ( in your IDE ) and copy - paste the contents of the HTML file into it. Another way is to embed the Java code into the HTML file then change the file's extension from a .html to a .jsp.
I want to create a custom JSP tag as follows.
<ng:template src="../js/Rule/templates/rule-list.jsp" />
Which will actually include the file "../js/Rule/templates/rule-list.jsp" inside a scripts tag and generate HTML as follows.
<script type="text/ng-template" id="../js/Rule/templates/rule-list.jsp">
Content of ../js/Rule/templates/rule-list.jsp file
</script>
So far I have creates following tagfile.
<%# attribute name="src" required="true" rtexprvalue="true" %>
<script type="text/ng-template" id="${src}">
<%# include file="${src}" %>
</script>
Which is giving this error
File "${src}" not found
Means its trying to include the ${src} instated of its value. Can any one suggest how to include file in tag file from specified attribute value?
Note: I am using angularjs. I want to load angularjs templates without ajax call. Because my browser is not able to load ng-template with AJAX call for cross domain call problem.
Got it. I need to use dynamic include as
<jsp:include page="${src}" />
This is working fine.
WEB-INF directory is a special directory that is not part of the public directory tree of your web (Servlet) application.
The Servlet Specification states (page 70 or so):
A special directory exists within the application hierarchy named
“WEB-INF”. This directory contains all things related to the
application that aren’t in the document root of the application. The
WEB-INF node is not part of the public document tree of the
application. No file contained in the WEB-INF directory may be served
directly to a client by the container. However, the contents of the
WEB-INF directory are visible to servlet code using the getResource and
getResourceAsStream method calls on the ServletContext, and may be
exposed using the RequestDispatcher calls.
AngularJS cannot see any folder inside your web application WEB-INF folder since it has no "connection" to it.
You will have to add those template files in a public folder, view-able by your Angular template files.
The following JUnit test is working in my application:
#Test
public void testMessageResources(){
final InputStream stream = getClass().
getResourceAsStream("/com/service/MessageResources.properties");
Assert.assertNotNull(stream);
}
But in my JSP, I can't read the file using this code:
<%
final InputStream resourceAsStream = application.
getResourceAsStream("/com/service/MessageResources.properties");
%>
resourceAsStream is always null. I'm using JSP 2.1.
The MessageResources.properties file is on the classpath, but not inside the JSP directory. Is that a problem?
The JSP uses ServletContext.getResourceAsStream(), which doesn't do the same thing as Class.getResourceAsStream(). It doesn't search for the resources in the classpath. If you want to use Class.getResourceAsStream(), then use Class.getResourceAsStream(). It will also work in a Java EE context. Just make sure to use a class loaded from the same classloader as the one you want to use to load your properties file.
Such a method call shouldn't be done in a JSP, though, but in a Servlet or action which forxards to the JSP. JSPs should be used to generate markup.
The getClass().getResourceAsStream() searches the file in the classpath, while application.getResourceAsStream looks for the resource in your web application directly. So the directory 'com' should be inside your web application and not in the classpath.
I have a text.class file that is in the same directory in my .jsp file, how can I include it in my jsp file? usually all of the classes should be in the WEB-INF,however I can't put it there.. Usually what I do is:
<%#Test.test" %>
where Test is a folder in the WEB-INF, so how can I do this now?
<%# page import="Test.test" %>
Provided that Test.test is in your classpath .The better place is to put it is:
WEB-INF/classes/Test/test
Not really an answer, but a warning you should check.
Putting your class files at your JSP folder can lead to security concerns.
The servlet container allows HTTP access for everything under the root web application dir (or inside the war file) but the content of the WEB-INF and META-INF folders. These folders are protected by default.
If you put a class at a different location, somebody could access an download it just writing the URL at his browser nav bar:
http://host:port/appContext/Test/test.class
I don't know if your app handles sensitive data, or your class contains code accessing main components of your application, which could be exposed if someone downloads and decompile your code: it is kind of a serious security risk.
Rethink your app structure, an keep your classes under the WEB-INF/classes dir. Or at least, configure your container or your web app to forbid access to *.class resources via HTTP requests.