I am trying to call a Java class from a JSP page. I have created the project using JDeveloper.
I am getting an error that says "The import cannot be resolved". I have added the Class file in WEB-INF, root folder, and tried compiling, but it still shows the same error.
Below is the code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
</head>
<body>
<p>
<%# page import="java.util.*"%>
<%# page import="Class1"%>
<%
Class1 tc=new Class1("test");
out.print(tc.str);
%>
</p>
</body>
</html>
you should give fully qualified name for your class. (packagename.classname)
like:
<%# page import="pkgname.Class1"%>
Page directives are normally placed at the top of a JSP. Also I assume Class1 is in the default package since it does not possess a fully qualified name. If Class1 is in a package you need to prefix the name in the import with the package name.
<%# page import="java.util.*" %>
<%# page import="Class1" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
</head>
<body>
<p>
<%
Class1 tc=new Class1("test");
out.print(tc.str);
%>
</p>
</body>
First of all, /WEB-INF/src is the wrong place to keep your java sources (since WEB-INF folder contents are deployed to your server); you should want to move them out of /WEB-INF (into /src in project root, for example)
Either way, you need to tell Eclipse where your sources are and where you want classes built to. It's done in project properties dialog:
Right-click on your project in Eclipse, select Properties
Click on Java Build path on the left
Click source tab on the right
Click Add Folder button and add your source folder (/WEB-INF/src or wherever you moved it to)
Ensure Allow output folders for source folders is checked below
Under newly added source path select output folder and point it to /WEB-INF/classes or other location of your choice
You shouldn't repeat the directive tags like <%# page import ......%>. But you can repeat the values in the import attribute value itself like
<%# page import="java.util.*, java.io.*"%>
and make sure to put the fully qualified name to import
Related
This question already has answers here:
How to install JSTL? The absolute uri: http://java.sun.com/jstl/core cannot be resolved
(20 answers)
Closed 1 year ago.
I am learning JSTL and I have downloaded JSTL 2.0.0 from here because jstl 1.2.2 was using javax and I am using jakarta.
display.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:out value="Jai Swaminarayan"/>
<c:forEach items="${students}" var="s">
${s} <br/><br/>
</c:forEach>
</body>
</html>
This code keeps throwing the following error:
Error on Eclipse
A problem was encountered using TagExtraInfo class
org.apache.taglibs.standard.tei.ForEachTEI for 'c:forEach'
Error on Webpage
HTTP Status 500 – Internal Server Error
Type Exception Report
Message java.lang.NoClassDefFoundError: jakarta/servlet/jsp/jstl/core/LoopTag
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
I have even added .jar file to Build Path and WEB-INF Folder. I do Not think this issue is with .jar file but I could be wrong.
Here, <c:out> is working perfectly but it only throws error in <c:forEach> tag. I checked the files in jakarta.servlet.jsp.jstl-2.0.0.jar and file named forEachTag.class Does Exists in org.apache.taglibs.standard.tei.ForEachTEI
Software Data:
Java Version: 15.0.2 | Server: Tomcat-10 | IDE: Eclipse
What I tried:
I cannot find the source but some answer asked to remove whitespace between <%# and taglib
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Trying since long time. Any help is appreciable :)
I had the same problem as you, I did like the comment said and looked at dependencies, downloaded and put the one below in the webapp/Web-inf/lib folder and it worked after that.
I have a main jsp file which makes use of java class in the boxers package. But when I try to run jsp, the following error occures:
HTTP Status 500 - Unable to compile class for JSP:in the jsp file: /web/date_info.jsp boxers.B cannot be resolved to a type.
date_info.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<p><%= boxers.B.voice()%></p>
</body>
</html>
B class:
package boxers;
public class B {
public static String voice()
{
return "HELLO";
}
}
I've read the conflict between versions can cause this; my Java version is 8, Tomcat 8.5..
I've looked into webapps/my_app/build/web/WEB-INF/classes/boxers folder and there is a B.class file...
EDIT: I wonder if those who downvote at least know the answer to the question.
Figured it out. The application wasn't deployed correctly. The deployment process, described here helped me out. In particular - copying web application archive file (.war) and copying unpacked web application directory. My main mistake was that initially I applied the second method in the wrong way - copied all the folders in the app directory, created by Netbeans (build, src, web etc), while only NetbeansProjects/app_name/build/web's content should have been copied into tomcat/app_name/.
Or just copy the war-file of NebeansProjects/app_name/dist/ into tomcat/webapps - the tomcat will create the appropriate folder with the files himself seconds later.
tl;dr: wrong deploment, copypaste war or web's content.
This question already has answers here:
JSP in /WEB-INF returns "HTTP Status 404 The requested resource is not available"
(3 answers)
Closed 6 years ago.
I'm following this example, I created a index.jsp file in the project WebApplication1 with the following code:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>First JSP Page</title>
</head>
<body>
<h1>Today's date</h1>
Date and Time: <%= (new java.util.Date().toString())%>
</body>
</html>
The file is located in WEB-INF folder. Following the tutorial instructions, I built the project (success), then I ran it (also no exceptions). But when I go to localhost:8080/WebApplication1' it shows 404 mistake. I also tried the localhost:8080/WebApplication1/index with the same result..
EDIT1: I also tried to copy the WebApplication1 folder from where Netbeans places it and pasting it into tomcat/webapps and running the simpliest index.html with the same (404) result..
EDIT2: Figured out the solution - the localhost:8080/WebApplication1/web/index.jsp works fine!
Move your index.jsp outside the WEB-INF folder. According to the Tutorial, the index.jsp is not IN the WEB-INF folder but actually it is IN the web folder.
web/WEB-INF/index.jsp should be web/index.jsp
I created a jsp page as follows:
<%# page contentType="text/css" %>
<html>
<head>
<title>Login page</title>
<link href="/css/loginstyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1> India welfare</h1>
<p> welcome </p>
</body>
</html>
and named it as login.jsp
and i also created a css file called loginstyle.css and the code of the .css file is as follows:
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
the directory structure for css and jsp's are as follows:
webcontent/welfare_web/css for .css files and
webcontent/welfare_web/login for jsp files
the programming editor is eclipse and the server i am using is tomcat 7.0. when i am trying to run the login.jsp file using tomcat server. The css file is not showing any effect. i mean the output is normal text and is not as per the CSS file.
please help me how to make the .css file to effect the jsp file.
css href link is incorrect. Use relative path instead:
<link href="../css/loginstyle.css" rel="stylesheet" type="text/css">
You can write like that. This is for whenever you change context path you don't need to modify your jsp file.
<link rel="stylesheet" href="${pageContext.request.contextPath}/css/styles.css" />
I use this version
<style><%#include file="/WEB-INF/css/style.css"%></style>
I had the same problem too. Then i realized that in the MainPageServlet the urlPatterns parameter in #WebServlet annotation contained "/", because i wanted to forward to the MainPage if the user entered the section www.site.com/ . When i tried to open the css file from the browser, the url was www.site.com/css/desktop.css, but the page content was THE PAGE MainPage.jsp. So, i removed the "/" urlPattern and now i can use CSS files in my jsp file using one of the most common solutions (${pageContext.request.contextPath}/css/desktop.css).
Make sure your servlet doesn't contain the "/" urlPattern.
I hope this worked for u too,
- Axel Montini
if everything seems correct, and despite it still does not work I invite you to load the statics files in the web.xml like this
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/includes/*</url-pattern>
</servlet-mapping>
after
<!-- bootstrap css -->
<link rel="stylesheet" type="text/css" href="/includes/asserts/css/bootstrap.min.css"/>
use this it worked for me
<style><%#include file="/WEB-INF/view/style/style.css"%></style>
For CSS :
<link href="css/bootstrap.css" rel="stylesheet" type="text/css">
For JS :
<script type="text/javascript" src="js/bootstrap.js"></script>
Files location:
└───src
└───main
├───java
└───webapp
├───css/bootstrap.css
├───js/bootstrap.js
├───META-INF
└───WEB-INF/index.jsp
└───lib
I'm receiving this error on my jsp file - 'Can not find the tag library descriptor for "/struts-tags"'
Strange thing is my app still seems to work.
I'm following the tutorials at - http://struts.apache.org/2.x/docs/using-struts-2-tags.html
Here is the code.
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="messageStore.message" /></h2>
<p>I've said hello <s:property value="helloCount" /> times!</p>
<p><s:property value="messageStore" /></p>
</body>
</html>
Thanks
Have you added the struts2-core-.jar to the libraries of the project? If you have try to clean the project.
In ECLIPSE:
Right click on the project and select the option "Validate". That's all.
This is probably Eclipse getting a bit 'confused' rather than a genuine error.
I had the same thing yesterday when working through a Struts 2 tutorial, but it disappeared after opening/closing the JSPs.
The same thing happened to me. I am using Maven to compile. So, I disabled and re-enabled Maven in my project and that solved it.
I faced similar problem. This is what I did to resolve the issue.
1.Select Project and right click.
2.Click on properties.
3.Click on libraries tab.
4.Click on 'Add Jars'.
5.Add relavent jar for your error.
hope this helps.
Thanks.
I had this problem because I copied the libraries manually and directly into the folder WEB-INF/lib of the project and eclipse doesn't see them (I suggest you copy any necessary libraries into WEB-INF/lib only through eclipse!). So only a refresh of the project solved the problem for me because all the other solutions didn't work!!
(I'm using Eclipse Kepler unber Ubuntu)
As William mentioned, this sounds like your IDE may not know where the TLD for the tag library is in your project, but once packaged and running, it is working fine.
add struts2-core-.jar to the project
download it here
http://struts.apache.org/development/2.x/index.html
go to Project tab > Clean..
Even if cleaning the project doesn't clear the error, right-click on the project and click refresh
Then right-click on the project, go to properties, Java Build Path >> 'Order and Export' tab >> make sure JRE library and Maven dependencies are checked.
that would probably solve it as it did for me