Problems with importing org.apache.commons in JSP - java

I've recently tried to use part of a code of a JSP file, found online, which starts by importing:
<%# page import = "org.apache.commons.fileupload.*" %>
<%# page import = "org.apache.commons.fileupload.disk.*" %>
<%# page import = "org.apache.commons.fileupload.servlet.*" %>
<%# page import = "org.apache.commons.io.output.*" %>
but Eclipse warns me with the error: " The import org.apache cannot be resolved. "
I tried to copy paste in the path
"C:\Users\MyPc\Downloads\apache-tomcat-7.0.96-windows-x64\apache-tomcat-7.0.96\webapps\SL2\WEB-INF\lib
the folder "commons-fileupload-1.4" once extracted, downloaded by the link: http://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi
and yet the error keep remaining. What do I mistake?

You should be putting the jar files from that zip, and only the jar files, into the project's WEB-INF/lib folder. The server has no use for the rest, and expects nothing more complicated in the layout there.

Related

JSP 2 tag files “/WEB-INF/tags” not found

Yes, this question is very similar to this one but with the huge difference that I am working with a TLD file rather than a taglib-to-folder declaration.
Okay, my setup is the following: I am migrating a pure-Java tag library to almost-JSP (some tags will still survive their Java form). I had a TLD defined under my classpath:META-INF folder. Some tags are defined as classes, while others are now defined as JSP 2 tag files under /WEB-INF/tags. Example given:
<tag-file>
<name>navbar</name>
<path>/WEB-INF/tags/navbar.tag</path>
<display-name>Navbar</display-name>
</tag-file>
This all works great under Eclipse, though I had to manually set up Web Deployment Assembly from src/META-INF to WEB-INF, otherwise the TLD file won't be recognized by Tomcat.
After happily debugging the code I have tried to create a WAR version of my application to be deployed to SIT. Despite the presence of the tags directory under WEB-INF along with the JSP 2 tag files, I am still unable to run the application.
And the error iiiiiiiiiiiiis:
org.apache.jasper.JasperException: /login.jsp (line: 3, column: 0) File [/WEB-INF/tags/navbar.tag] not found
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:275)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:91)
org.apache.jasper.compiler.Parser.processIncludeDirective(Parser.java:335)
org.apache.jasper.compiler.Parser.parseIncludeDirective(Parser.java:368)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:471)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1435)
org.apache.jasper.compiler.Parser.parse(Parser.java:139)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)
org.apache.jasper.compiler.Compiler.__compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet._serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
The TLD is declared as usual
<%# taglib prefix="example" uri="http://www.example.org/example.tld"%>
How can I fix this?
Ok, I found the partial solution (I will update this answer if and when I find the complete one).
It looks like that if I deploy the TLD file under classpath, then I should deploy the tags accordingly.
I had to replace <path>/WEB-INF/tags/navbar.tag</path> with <path>/META-INF/tags/navbar.tag</path> and move all the tags under classpath:META-INF/tags.
This way, the WAR package is working.
Unfortunately, I broke my Eclipse setup, so the workaround was to manually setup a Deployment assembly entry from src/META-INF/tags to META-INF/tags because the tag file now references META-INF folder. Pretty ugly, but suitable for development at the moment.
I've had a similar experience. My project uses tags to keep navigation menus in a single place and use them on many jsp pages. I am using Tomcat 8. Some of the things I have learned along the way are:
The tags directory contains *.tld and *.tag files you are creating.
My configuration is Tomcat 8,Java 7,JEE 6,Servlet 3.1 and JSP 2.2. I am using a plain eclipse jsp project. (no maven) Here is my tld file:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE xml>
<taglib version="2.0" xmlns="http://java.sun.com/xml/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<tlib-version>1.2.5</tlib-version>
<jsp-version>2.2</jsp-version>
<short-name>taglibs</short-name>
<description>jsp tag library for the navigation code</description>
<tag>
<name>navMenu</name>
<tag-class>ksServer.navTag</tag-class>
<body-content>tagdependent</body-content>
</tag>
</taglib>
The beginning lines of a typical webapp jsp file are
<%# page language="java" contentType="text/html" pageEncoding="UTF-8" %>
<%# page import="ksClasses.KsvrmSqlAgent" %>
<%# page import="org.slf4j.Logger" %>
<%# page import="org.slf4j.LoggerFactory" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="kr" tagdir="/WEB-INF/tags" %>

which jar to use for JSTL

(server Tomcat7v)
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
First Name:<c:out value="${param.fname}"></c:out><br/>
Last Name:<c:out value="${param.lname}"></c:out>
in the previous JSP and I am getting
Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
this error.
But when I try to find out the jars there were lots of version of that I am confused with which one should I use??
You need to put jstl jar in your classpath. See details at https://stackoverflow.com/tags/jstl/info
I were using Tomcat 7v which uses servlet 3.0 so I had to use jstl1.2.1 jars

JSTL Taglib -error

i have the following pice of code:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
If i want to run the programm i get the following error message:
HTTP Status 500 - The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
My JAR Files are in the WEB-INF/lib/ folder
--->javax.servlet.jsp.jstl-1.2.1-javadoc.jar (IMP)
--->javax.servlet.jsp.jstl-api-1.2.1-javadoc.jar (API)
I use the WEB.xml Version 3.0
So please can someone help me?!
Thanks
It seems you have added the javadoc of the JSTL instead of the code. Try downloading this one and add it to your lib directory:
http://mvnrepository.com/artifact/javax.servlet/jstl/1.2

error while importing .java file in jsp page

I have a .java file containing a public class. It is located under the 'web pages' folder (not under web-inf folder).
On my jsp page I have imported it as: <%# page import="packagename.javafilename;" %>
When I run jsp file, I get error as:
Unable to compile class for jsp.
Only a type can be imported. Packagename.filename resolves to a package
As mentioned in the comments there seems to be a missing % towards the end. Here is the way to import classes in jsp
// To import one class
<%# page import="com.xyz.MyClass" %>
OR
// To import multiple classes from the com.xyz package
<%# page import="com.xyz.*" %>
From your question things are not very clear.
First of all jsp cannot access .java but a .class file i.e. you have to keep your code compiled.
Secondly the class should be in the WEB-INF/classes folder or a jar in WEB-INF/lib folder.
Use this syntax:
<%# page import="package.filename" %>
In the last import, you might need to add a semicolon after the end of the package.filename.
So, if you have like 3 imports, then in the 3rd import line make sure to put a semicolon as:
<%# page import="package.filename;" %>
One more thing, make sure that the class files are in WEB-INF/classes folder.

Folder access in Java EE

I need to access a page allCommonPageLink.jsp from another page named add_edit.jsp
I have done <%# include file="/plan/common/allCommonPageLink.jsp" %>
Error /WEB-INF/ViewPages/plan/add_edit.jsp (line: 8, column: 42) File "/plan/common/allCommonPageLink.jsp" not found
NEW QUESTION::
how can I access an image form images folder from tray.jsp and add_edit.jsp
you should use
<%# include file="../../common/allCommonPageLink.jsp" %>
Using ../ takes you back to the previous directory from where you are now.
If your page is web-inf/viewpages/plan/page.html that means that your working dir is plan. Invoking ../ takes you to view pages. Invoking another ../ takes you to web-inf. And from web-inf you have access to the file by accessing common/anotherpage.jsp
You can read https://www.owasp.org/index.php/Path_Traversal for more info
make it
<%# include file="WEB-INF/common/allCommonPageLink.jsp" %>
the file allCommonPageLink.jsp is not under the plan folder, fix he path and try again, hope this would fix your issue.
This will work
<%# include file="common/allCommonPageLink.jsp" %>
OR
You have to go back up to two folders plan and viewPages in order to access this folder in that file so this will work for you
<%# include file="../../common/allCommonPageLink.jsp" %>
You can use
<%# include file="../../common/allCommonPageLink.jsp" %>
When you add ../ it it will refer its parent folder or directory.

Categories

Resources