Java Cannot include jsp page - java

i am trying to directive include a .jsp page in Java but the link is not recognized by Netbeans 8.2 and is showed as plain text.
<%# include file="/WEB-INF/templates/header.html" %>
How can i solve this?

You cannot read from the WEB-INF directory. Have a look at this document to see how a WAR file should be organised.

Related

linking dynamic .html form to .jsp

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.

Custom JSP Tag which include another 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.

Jstl not adding correct to Dynamic Web Project in Eclipse (Juno)

I have seen several Tutorials and post where they show you how to add the Jstl.rar to be able to use it with JSP Files. But I'm not havving any luck
This is what I did
Create Dynamic Web Project
Download the Jstl 1.2. from Apache came with:
taglibs-standad-impl-1.2.5jar,taglibs-standard-spec-1.2.5.jar,taglibs-standard-jstlel-1.2.5.jar,taglibs-standard-compat-1.2.5.jar
Add jar files under /WEB-INF/lib
Create jsp file and add <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
It gives me an error it cannot find the tag description <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
I look at these articles but they tell you different ways to fix it but they are not working. Any help would be appreciated. I'm trying to learn this but I'm struggling because of this
About Jstl
Getting jstl to run with tomcat and eclipse
I also tried going to properties ->Java Build Path-> Libraries. And add a new user library and under that user library add all the jar files but didnt work either. Am i missing some confugiration on the web.xml file. Any help please ?

JSP include directive, jsp:include action, relative vs. absolute paths

I am doing some basic templating in my JSP-based webapp. For example, I want to have a standard header and footer (basic HTML) that I pull into each of my JSPs.
My content JSP is at /WEB-INF/jsp/home.jsp, and I have template JSPs at /WEB-INF/jsp/template/, such as /WEB-INF/jsp/template/Body-Footer.jsp.
So now, within home.jsp, I want to pull in my template files. First, I try the jsp:include action:
<jsp:include page="template/Body-Footer.jsp"></jsp:include>
It generates the error javax.servlet.ServletException: File "/template/Body-Footer.jsp" not found
Strange to me, considering that Eclipse says that the path is valid.
Okay, so then I switch to the include directive:
<%# include file="template/Body-Footer.jsp" %>
This works just fine, pulls in my footer HTML.
But why does the jsp:include not work? After some experimentation, I find that putting in the absolute path does get it to work:
<jsp:include page="/WEB-INF/jsp/template/Body-Footer.jsp"></jsp:include>
Now it works fine, no errors.
So here's my question: why? Why do I (apparently) need to use an absolute path with the jsp:include action, but not with the include directive?
/WEB-INF/jsp/template/Body-Footer.jsp is not an absolute path. Its also a relative path. The problem is that template/Body-Footer.jsp is an incomplete relative path, whereas the other is complete. That is, the paths are relative to your app path. Since /WEB-INF/ is under your app path, you have to include it. Absolute path means like C:/program files/tomcat/webapps/yourapp/WEB-INF/jsp/template/Body-Footer.jsp
Answer to WHY - The jsp:include is a runtime directive unlike the <%# include ... %> directive which happens to be a compile time directive (translation time, actually).
See more on: JSP Performance using jsp:include
Bottom line - directives are run against different folders as a base.
Btw. JSP pages should be outside of WEB-INF folder, if you want to follow official recommendation:
The Java EE 6 Tutorial - Web Module Structure
I read JSP 2.0 spec and here:
Relative URL Specifications
* A context-relative path is a path that starts with a slash (/).
It is to be interpreted as relative to the application to which
the JSP page or tag file belongs. That is, its ServletContext
object provides the base context URL.
* A page relative path is a path that does not start with a
slash (/). It is to be in- terpreted as relative to the current
JSP page, or the current JSP file or tag file, depending on where
the path is being used.
For now javax.servlet.ServletContext.getRealPath("/WEB-INF/test/test.jsp") is null for security reason.
Assume that context-relative path is path from your WAR root.

Class not found exception Applet.jar in html file

I am trying to run an .jar file with a class in it that extends Applet in an html file. The .jar is called DocScrabble.jar, html is DocScrabble.html, and .class file is ScrabbleSolver.class. ScrabbleSolver.class also references a file called EnglishWords.txt, and I included that in the default package in which I placed ScrabbleSolver.class when I exported the file to a .jar. DocScrabble.jar and DocScrabble.html are located in the same directory. The applet works fine in eclipse, so I am assuming that my html file is the problem. THere is the html code.
<!DOCTYPE html>
<html>
<head>
<title>Doc Scrabble></title>
</head>
<body>
<APPLET CODE="ScrabbleSolver.class"
ARCIHVE="DocScrabble.jar"
WIDTH="400"
HEIGHT="200"
</Applet>
</body>
</html>
When i try to run the html, it gives me an error that says ClassNotFoundException ScrabbleSolver.class. Could someone please tell me what's wrong? I'm relatively new to programming.
It is highly advisable to check HTML using a validation service or DTD.
I typically rely on the W3C HTML validation service to check HTML.
Note that the applet element was last valid in HTML 3.2. It was deprecated in HTML 4.01. Without declaring any version, that mark-up would be presumed to be HTML 5.
The best way to deploy an applet is to use the Deployment Toolkit Script. On the other hand mistakes in spelling the attribute names in the script would not be picked up by an HTML validation service, since it concentrates on the HTML, rather than JavaScript embedded in the HTML.
For that reason it is a good idea to see the applet work when loaded using 'pure HTML' first.
Is your ScrabbleSolver class in a package? if so then it should be packagename.ScrabbleSolver.class
See Also: http://download.java.net/jdk8/docs/technotes/guides/jweb/applet/using_tags.html

Categories