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" %>
Related
I am using Eclipse IDE 2022-06 , Java/JDK 1.6, Windows 10 pro x64, Spring 2.5.6 .
I need encoding UTF-8 by default, not ISO-8859-1. I don't want edit many files, many time manually. How to change template when create new JSP file?
In the New JSP File dialog click Next
to choose a template or
click the JSP Templates link to go to the preferences Web > JSP Files > Editor > Templates where you can change the template for JSP.
By default, in the JSP templates the variable ${encoding} is used which can be configured in the preferences Web > JSP Files.
sample result
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
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" %>
I have been trying to get my '.jsp' file to use a '.css' file that I have created. I had originally stored the '.css' file in my 'WEB-INF' folder but through some searching found that the 'WEB-INF' folder is not public and can therefore not store the file so I moved it outside into the 'webapp' folder but I am still not getting anywhere.
The files 'index.jsp' and 'index.css' files are in the same folder:
'HelloWord/src/main/webapp'
My '/jsp' file links the '.css' as shown
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="${pageContext.request.contextPath}/index.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MM Vehicle Registration</title>
</head>
I get the following error when I try to run:
Nov 28, 2015 3:42:00 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWorld/index.css] in DispatcherServlet with name 'dispatcher'
The web page still displays ony without the css decoration. The file is already declared in a '.jsp' that is being rendered fine so why would I receiving this error?
Spring takes control over all URLs. You need to put <mvc:resources> into your configurtion and preferably put your client resources into WEB-INF/resources/ directory. Also check How to handle static content in Spring MVC?
try replace
'${pageContext.request.contextPath}/index.css'
with
'/index.css'.
Are both index.css and index.jsp in the root folder of webapp?
Ok,the strutre is right.Now I suggest you to check you web.xml.
You might have something like this?
<servlet-mapping>
<servlet>dispatcher</servlet>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit
<servlet-mapping>
<servlet>dispatcher</servlet>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
then try to access to /helloworld/index.css.if ok,rollback '/index.css'. to '${pageContext.request.contextPath}/index.css'
I have a JSP which simply shows and image as header. I will change this into a .tag file for custom tag development. I am doing this in eclipse and my project structure is -
The jsp i am trying to run on the server is Header.jsp under jsp folder. The problem is that the image is not displayed, even when I use the fully qualified path of the image. Insted, i see a red cross. How do I fix this ?
When I use this file as a .tag file referenced by another jsp, the contents of tag do not appear in that jsp.
JSP code -
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="myTags" tagdir="/WEB-INF/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<img src="/images/java_logo.gif"><br>
</body>
</html>
The URL you specify here is absolute for your site, not the web app.
<img src="/images/java_logo.gif">
So try to use (with WEBAPPROOT replaced by the correct name)
<img src="/WEBAPPROOT/images/java_logo.gif">
or make it relative:
<img src="../images/java_logo.gif">
If you're in JSP or Facelets, it is way better to use HttpServletRequest#getContextPath:
<img src="${request.contextPath}/images/java_logo.gif" />
If you happen to use JSTL:
<img src="<c:url value="images/java_logo.gif" />" />
In this way, you avoid using relative paths and/or guessing what would be your current web application path (this is in case you change the name to display the app or something similar). For example, if you happen to have this structure:
- WebContent
- images
+ java_logo.gif
- jsp
+ Header.jsp
- anotherFolder
+ Another.jsp
If you want to add java_logo.gif in Another.jsp, you just need to do this:
<img src="${request.contextPath}/images/java_logo.gif" />
Unlike the relative path:
<img src="../../images/java_logo.gif" />
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();
....
%>