File inclusion not working - java

The below code throws an error in Netbeans saying the include statement is not closed. I've tried a few things like #%> and I'm not able to figure out what's up. what am I doing wrong here?
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%=request.getAttribute("title")%></title>
</head>
<body>
<%#include file=request.getAttribute("template")%>
</body>
</html>

Try using <jsp:directive.include/>:
<jsp:directive.include file="<%= request.getAttribute("template") %>"/>
Depending on the contents of your template, <jsp:include/> may also work:
<jsp:include path="<%= request.getAttribute("template") %>"/>

The include directive works at compile time (translation time), the path listed in file should be relative to the web application.
The include directive is processed when the JSP page is translated
into a servlet class. The effect of the directive is to insert the
text contained in another file--either static content or another JSP
page--in the including JSP page
if you want to use something more flexible when used to include files jstl tag < c:import >
Using the jstl we can also include those contents or files
which are not a part of the current web application but lying
somewhere outside the web application

Related

CSS not loading in spring boot application

I have been searching all day, moved the style.css everywhere and still not managed to get it loaded. The images wont load as well.
My structure:
But if i click the firefox button, it loads:
This is how the style.css is imported in the head of the index:
(tried all kind of combinations)
When i check the developer tools, it says 404 for GET request (style.css and the pictures)
Spring Boot knows where the static directory is, so you don't need to use the ../ technique to get to the files within. The other part is that you should be using an annotation to get there. It may depend on what view template you are using, but for thymeleaf this is how you would achieve pulling in the css file:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Your Title</title>
<link rel="stylesheet" th:href="#{/css/style.css}" />
</head>
Notice the annotation in th:href="#{/css/style.css}"
Make sure you include the namespace of your view template, th in the example.
Images work the same way:
<img th:src="#{/img/stanev2.png}" />

HTML embed not working for Java Applet

I've been researching on which to use between <applet>,<object>, or <embed>, but none seem to work.
When I tried to load JApplet through HTML I am got RuntimeException error java.lang.NoClassDefFoundError: com/sforce/ws/ConnectionException.
When I tried to run number1.class with the number1.class being in myfile.jar it needs the other 3 jar files for the library and that is what the error is. The files look like this:
tomcat-->webapps-->applet-->newhtml.html
applet-->lib-->(wsc-23,enterprise,partner)
applet-->applet_class-->(number1.class,myfile.jar)
Any help would be appreciated.
I've also looked through majority of stackoverflow questions as well as other places, but still no luck!
<!DOCTYPE html>
<html>
<body>
<html type="application/x-java-applet;version=1.6"
width="512" height="512"
code="applet_class.number1.class"
src="myfile.jar,applet/lib/wsc-23.jar,
applet/lib/enterprise.jar,
applet/lib/partner.jar"/></html>
</body>
</html>
The best way to deploy a JWS app. or applet is to use the Deployment Toolkit Script.
But looking at that element..
<html type="application/x-java-applet;version=1.6"
width="512" height="512"
code="applet_class.number1.class"
src="myfile.jar,applet/lib/wsc-23.jar,
applet/lib/enterprise.jar,
applet/lib/partner.jar"/></html>
The most basic form of the applet element (deprecated in HTML 4.01 is):
<applet
width="512" height="512"
code="applet_class.number1"
archive="myfile.jar,applet/lib/wsc-23.jar,applet/lib/enterprise.jar,applet/lib/partner.jar"/>
</applet>
Change html to applet.
Remove the type attribute.
Remove the .class from the end of the code attribute.
Change src to archive, and have all the archives in one line.

Java (IntelliJ Idea) - import class fail

I have a jsp page that imports a class from another module and package.
<%# page language="java" import="login.UserLogin" %>
<%# page contentType="text/html; charset=windows-1251" %>
<html>
<head>
<title>
Page title
</title>
</head>
<body>
<%-- Form to initiate POST --%>
<FORM name="test_form" method="post">
<INPUT type="submit" name="submit" value="Make Post"><BR>
</FORM>
</body>
</html>
I've corrected oll the dependances in IDE, but still get an error log:
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
The imported class is empty, so there are no further dependances.
Your JSP's are internally compiled to a servlets called [your_jsp]_jsp.java and your IDE is saying that it cannot find that compiled class.
Your problem is that your project is not compiled / not deployed in the server. Or maybe you have not well configured the output directory... I don't know, I don't use intelliJ...
Found! Error in Tomcat deployment config - it referenced to source folder instead of artifact

Error when including jsp files on my jsp page

I am trying to include 2 JSP files in my JSP page. My main page is called temp.jsp - this is in a subfolder in my web project called tempFolder.
I am trying to include a file in the main project folder (called invalidcqs.jsp) and a file (called env_status_report.jsp) in a sub folder (envmon) of the main project folder.
the code in my temp.jsp file is:
<html>
<head>
<title>Screen1 using includes</title>
<meta http-equiv="refresh" content="10"/>
</head>
<body style="background-color:#E6E6FA">
<%# include file="../envmon/env_status_report.jsp" %>
<br><hr><br>
<%# include file="../invalidcqs.jsp" %>
</body>
</html
The second include <%# include file="../invalidcqs.jsp" %> works fine but the first one <%# include file="/../envmon/env_status_report.jsp" %> shows an error in Eclipse.
The text of the error is:
Multiple annotations found at this line:
- Syntax error on token "else", delete this token
- Syntax error, insert "Finally" to complete
TryStatement
- Syntax error on token "else", delete this token
Does anyone know why Eclipse doesn't like this?
Usually I don't care much about Eclipse reporting errors on jsp pages, specially when using the <%# include> directive. For instance, if you declare a scriptlet variable in your main page and use it in the included page, Eclipse will complain about it not being declared while in the included page, but it will work all right at runtime.
This error is possibly coming out of the included jsp, so I'd start looking for this error inside it.
You could also try to include pages the EL way:
<jsp:include page="/WEB-INF/pages/received.shtml" />
Maybe that will help

Jetty 7 hightide distribution, JSP and JSTL support

I've been struggling with Jetty 7 and its support for JSP and JSTL.
My JSP file:
<%# page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
<title>blah</title>
</head>
<body>
<table id="data">
<tr class="columns">
<td>Hour</td>
<c:forEach var="campaign" items="${campaigns}">
<td>${campaign}</td>
</c:forEach>
</tr>
<c:forEach var="hour" items="${results}">
<tr>
<td class="hour">${hour.key}</td>
<c:forEach var="campaign" items="${campaigns}">
<td>${hour[campaign]}</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</body>
</html>
The JSP portions above work as expected. JSTL, however, does not. The campaigns and results variables are request attributes set by a servlet.
I get the following errors:
WARN: ... compiler.TagLibraryInfoImpl: Unknown element (deferred-value) in attribute
WARN: ... compiler.TagLibraryInfoImpl: Unknown element (deferred-value) in attribute
WARN: ... compiler.TagLibraryInfoImpl: Unknown element (deferred-value) in attribute
ERROR: ... javax.servlet.ServletException: java.lang.AbstractMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
I am not bundling any jar files into my .war file deployed to jetty.
The version of jetty I'm using is: jetty-hightide-7.0.1.v20091125
The classpath:
/usr/local/jetty/lib/jetty-xml-7.0.1.v20091125.jar:/usr/local/jetty/lib/servlet-api-2.5.jar:/usr/local/jetty/lib/jetty-http-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-continuation-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-server-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-security-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-servlet-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-webapp-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-deploy-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-servlets-7.0.1.v20091125.jar:/usr/local/jetty/lib/jsp/ant-1.6.5.jar:/usr/local/jetty/lib/jsp/core-3.1.1.jar:/usr/local/jetty/lib/jsp/jetty-jsp-2.1-7.0.1.v20091125.jar:/usr/local/jetty/lib/jsp/jsp-2.1-glassfish-9.1.1.B60.25.p2.jar:/usr/local/jetty/lib/jsp/jsp-api-2.1-glassfish-9.1.1.B60.25.p2.jar:/usr/local/jetty/resources:/usr/local/jetty/lib/jetty-util-7.0.1.v20091125.jar:/usr/local/jetty/lib/jetty-io-7.0.1.v20091125.jar
Any help would be greatly appreciated.
Thanks in advance,
Lior.
With Jetty 8, the situation is a bit different, in case this helps anyone.
For JSTL 1.2, rather surprisingly, the taglib has to be:
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
with JSTL 1.2 from (mavenishly):
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
I can't really explain why the URL lacks 'jsp', but it works this way.
tsk... I don;t have privilege to comment. I am using Jetty 7.1.6 and answer provided by bmargulies works.
Basically, changing URI from
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
to
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
makes taglibs to work in Jetty 7.
-Nishant
The reason why http://java.sun.com/jsp/jstl/core doesn't work is because the code in the Jasper Jsp parser used by Jetty (org.apache.jasper.glassfish:jar:2.2.2.xxx) assumes that that uri is a systemuri (see TldScanner.java) and it will not put any taglibs with this uri in its tablib location cache. I don't know why this assumption is in the code but it is. Seems to be a bug to me.
java.lang.AbstractMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
This exception basically means that the mentioned method cannot be found in the runtime classpath, while it was available in the compiletime classpath of either the class or one of its dependencies.
This method is introduced in JSP 2.1 which gets hand in hand with Servlet 2.5. Since Jetty 7 is supposed to support Servlet 2.5 and thus isn't the suspect here, the only cause can be that the web.xml is declared as Servlet 2.4 or lower instead of Servlet 2.5. So, to fix this particular problem, you need to declare your web.xml as at least Servlet 2.5. The <web-app> tag should look like this:
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="YourWebAppID"
version="2.5">
If that doesn't solve the problem, then the other cause is that the /WEB-INF/lib or even worse the /JRE/lib or /JRE/lib/ext is cluttered with appserver-specific libraries containing an older Servlet API version. E.g. servlet-api.jar from Tomcat or j2ee.jar or javaee.jar from Glassfish, etcetera. You'll need to clean up those classpath folders from any libraries which doesn't belong there, because they get precedence in classloading and will override the appserver's own libraries. Appserver-specific libraries belongs to the appserver in question, not to the webapp or JRE.
That said and apart from the actual problem, the #page attributes language="java" contentType="text/html; charset=utf-8" are all superfluous. The language already defaults to Java and the contentType already defaults to text/html and the charset will already be set to UTF-8 if you set pageEncoding="UTF-8". So the following is already sufficient:
<%#page pageEncoding="UTF-8" %>
Thx for the tip Steve! The bug seems still there, here's a workaround to run at Jetty initialisation. It did the trick for me.
import org.apache.jasper.runtime.TldScanner;
import java.util.Set;
Field field = TldScanner.class.getDeclaredField("systemUris");
field.setAccessible(true);
((Set<?>)field.get(null)).clear();
field.setAccessible(false);

Categories