How to set jstl in Maven Jetty plugin - java

I made a simple web project. For ease of use, I used a maven jetty plugin and I made several attempts to use the jstl library. In principle, the plugin has a built-in jstl library, and indeed, although I have not explicitly included jstl-1.2.jar, my pages are compiled. The problem occurs when I try to use the functionality of jstl.
Example jsp:
<%# page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<html>
<body>
<h2>Hello World!</h2>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<c:out value = "${salary}"/>
</body>
</html>
Preview:
I tried this approaches:
1)to include the library as a dependancy in my pom.xml -> the same result
2)to include the library in WEB-INF.lib -> the same result
3)to include jstl-1.2.jar as external jar in dependencies -> the same result
I am using NetBeans 8.2

There is no need to include jstl library when you use maven jetty plugin. The problem was in Servlet descriptor which can be configured from web.xml.
This web.xml solve my problem:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>

Related

JSTL configuration

I don't know what I've done incorrectly, but I can't include JSTL. I have jstl-1.2.zip, taglibs-standard-jstlel-1.2.5.zip in my WEB-INF/lib but unfortunately I get exception:
jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
I included also the taglib : <%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> in my JSP file. Please help me I couldn't find a solution for this problem, I just want to show hello world with the jstl technology so i can go and learn more about it.
MyservletMyJSPFile

JSP Tomcat7 static css and js files not loading

Learning JSP and have an issue with the css/js content not loading. I have a jsp page, where I have bootstrap css and js referenced using standard html link and script tags:
<link rel="stylesheet" type="text/css" link="/bootstrap/css/bootstrap.css" />
and
<script src="/WebIntro/bootstrap/js/bootstrap.js"></script>
neither of them work and Chrome is giving me the following on the console:
Resource interpreted as Stylesheet but transferred with MIME type text/plain:
If I use an include directive, it works for the css but pulls all the content into the file:
<%#include file="/bootstrap/css/bootstrap.css"%>
The jsp has the following #page and meta tags:
<%#page contentType="text/html" %>
and
<meta http-equiv = "Content-Language" content = "en"/>
<meta http-equiv = "Content-Type" content="text/html; charset=utf-8">
I tried googling and found the mime-mapping element for the web.xml but the following seems to have no affect:
<mime-mapping>
<extension>css</extension>
<mime-type>text/css</mime-type>
</mime-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>application/javascript</mime-type>
</mime-mapping>
If there is some standard Tomcat config that needs to happen I am unaware as I am new to Tomcat; using Tomcat 7, and eclipse and this is in a maven project.
The solution for me was very strange. The changes to the web.xml file that was present since I built the project in eclipse/maven, weren't reflected in the web.xml within tomcat webapp application directories. Everything else in the package updates fine (as far as I can tell), except for the web.xml?
So I found the web.xml file within eclipse inside target >> project-SNAPSHOT >> WEB-INF >> web.xml (which is supposed to be derived). Forced changes there and it worked, changes were picked up and my original issue gone.
I am guessing I had inadvertently broken some type of dependency link or something along the way somehow, but once I added the servlet, mappings, etc back in, everything worked fine.
Weird.
Removing this line in jsp resolved the error for us.
We were facing the issue when using Tomcat 8.5.59

How to know which servlet and JSP version am I using?

Can you tell me how to know which servlet and JSP version am I using ?
I use NetBeans IDE 7.1.2 for creating Servlets and JSP.
You can easily check the JSP,SERVER and SERVLET version. Add the following code in your jsp page after that run using any IDE Tools.
Server Version: <%= application.getServerInfo() %><br>
Servlet Version: <%= application.getMajorVersion() %>.<%= application.getMinorVersion() %>
JSP Version: <%= JspFactory.getDefaultFactory().getEngineInfo().getSpecificationVersion() %> <br>
You can get the details programatically using ServletContext #getMajorVersion() and #getMinorVersion().
For knowing the JSP version corresponding to the Servlet, you can get details from this Tomcat page.
Below is a brief summary (check Tomcat's corresponding version at the link above):
Servlet 4.0 uses JSP 2.3
Servlet 3.1 uses JSP 2.3
Servlet 2.5 uses JSP 2.1
Servlet 2.4 uses JSP 2.0
Servlet 2.3 uses JSP 1.2
Servlet 2.2 uses JSP 1.1
Servlet 2.1 uses JSP 1.0
The version is declared in the web.xml file using the attribute version.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
...
</web-app>
Read more

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);

pre-compile jsp files happend exceptions

I pre-compile some jsp files through ant task of jspc,but it built failed.
errers:
info.jsp(35,2) The attribute prefix fn does not correspond to any imported tag library
info.jsp line 35 :
<c:if test="${fn:length(requestScope.checkDetailInfoList) gt 1}">
ant task xml:
<jasper validateXml="false" uriroot="${basedir}/WebRoot"
webXmlFragment="${dir.WEB-INF}/generated_web.xml"
outputDir="${dir.WEB-INF}/src" />
How should I correct?
You need to make sure the jsp file imports the fn namespace of the JSTL. You'll need a line that looks something like this in your jsp file:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Also, you'll need to make sure the JSTL jars (jstl.jar and standard.jar) are in your classpath when jasper tries to compile.
Asaph's comments are spot on.
There's one other bit to check: The <fn> tag set was a later addition to the JSTL libraries. Maybe you have an older version of jstl.jar and standard.jar that needs to be updated.

Categories