problem in importing java class in jsp - java

i have created a java package in source packages in netbeans
i have a jsp file in a web folder
now i want to import this java package in jsp file but i am not getting my package name in import command

Import package,
<%# page language="java" import="yourpackage.subpackage.*,java.util.*" %>
or,
<%
yourpackage.subpackage.ClassName k=new yourpackage.subpackage.ClassName();
....
%>

Related

java- Possible to import a jspf file?

I have a java class in a different module that the jspf file is located.In a normal JSP file, I would import it this way:
<%#include file="../../../WEB-INF/jspf/corelib.jspf" %>
Right now,I want the equivalent of this in my .java class but can't find a proper way to import.
How would I import this?

Java code statement inside jsp not executing when including in other jsp file

I have a folder in my project and some jsp file into that folder and I want to check valid session on each page. So I have created a JSP file that contains the session checking code, inside web-inf folder and including that file into other jsp file but the code is executing but response.sendRedirect() not working.
So I have Admin folder that contain all files, WEB-INF/jsp/SessionValidate.jsp file(this file has to be included into admin folder's file. See the comments in the code
Admin/xyz.jsp
<jsp:include page="/WEB-INF/jsp/SessionValidate.jsp"></jsp:include>
WEB-INF/jsp/SessionValidate.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
if(session.getAttribute("Roles")==null){
response.sendRedirect("/Shopping/Login");
<!-- response.sendRedirect(..) -->
System.out.println("hello2");
<!-- Hello is printing -->
}
else{
if(!session.getAttribute("Roles").equals("ADM")){
response.sendRedirect("/Shopping/User");
}
else{%>
<jsp:include page="/WEB-INF/jsp/admin.jsp"></jsp:include>
<%}
}%>
So when I paste this code directly into the xyz.jsp its working but not like this.
you are doing a dynamic include with
<jsp:include page="....
try this syntax
<%# include file="/WEB-INF/jsp/SessionValidate.jsp" %>

The import src cannot be resolved

I'm a beginner in JSP but understand Java and HTML. I am running into this error when trying to run a JSP file in Eclipse on line three, can someone please help?
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#page import = "src/Person.java" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
An import directive in a JSP is just like an import statement in a Java source file: it expects a fully-qualified class name, not a path to a source file.
First of all, you must put your class Person in a package. Put the source file MyPerson.java in a directory mypackage, for example, and add a package statement at the top of the source file:
package mypackage;
Then, you can import it in the JSP by specifying the fully-qualified class name:
<%#page import="mypackage.Person" %>

How do I import my own class in another folder into a jsp file?

In the same directory as my index.jsp, I have a folder called 132b.
Inside 132b I have a folder called classes which contains a bunch of Java files.
In my index.jsp, I have tried:
<%# page import="132b/classes/*" %>
<%# page import="132b.classes.*" %>
The first attempt complained about an unexpected end of comment.
The 2nd attempt couldn't find the class I was trying to instantiate so it couldn't be resolved to a type.
How do I properly import/include the classes I wrote?
By default it will search Java files inside src folder till you explicitly set it to be something else..
Try putting 132b folder inside src folder and see if <%# page import="132b.classes.*" %> works.
I always prefer to import only classes which are needed in that particular page instead of importing all the classes of a package.

<c:import> tag error: java.io.FileNotFoundException

I'm trying to import file from the Header.jsp in my file by using import tag url attribute, but I'm getting runtime error
java.io.FileNotFoundException: http://localhost:8081/latest/header.jsp
The imported file and the importing file in the same web app(latest).
The code of the importing file is:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html><body>
<c:import url="http://localhost:8081/latest/header.jsp" charEncoding="UTF-8" />
<em>Web services Support Group.</em><br><br>
</body></html>
and the code of imported file is:
<em><strong>${param.name}</strong></em><br>
If they're in the same webapp, you don't need a ful URL, you just need the URI relative to the webapp root:
<c:import url="/header.jsp" charEncoding="UTF-8" />
probably you are using the wrong path, where is the file header.jsp? is it in a directory called "latest"? or is "latest" the context path of your application?
skaffman is right, you don't need the full url, but just the url relative to the web app root.

Categories