jsp:param encoding problems - java

So I have several .jsp files:
one of the files has the head tag and has the title of the page:
<%# page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${param.title}</title>
</head>
The other files include the first one and pass to it a param using jsp:param:
<%# page pageEncoding="UTF-8"%>
<jsp:include page="consoleheader.jsp">
<jsp:param name="title" value="Título"/>
</jsp:include>
<body>
...
</body>
</html>
Any non-ASCII characters that I pass using jsp:param are getting garbled when I do this (the í in Título for instance). Everywhere else it works fine.
All jsp files are encoded using UTF-8. I have not set any charset configurations on my JVM. Anyone knows how to fix this without setting the JVM encoding by hand?

I had a similar problem with jsp params and hacked it in the following way:
main.jsp:
<%# page pageEncoding="UTF-8"%>
<html>
<head/>
<body>
<jsp:include page="other.jsp">
<%-- í = í --%>
<jsp:param name="title" value="Título"/>
</jsp:include>
</body>
</html>
other.jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page pageEncoding="UTF-8"%>
<h1><c:out value="${param.title}" escapeXml="false"/></h1>
I know it is not the best solution, but it worked for me.
Edit
I found an other solution that is could work for you too:
Adding the setCharacterEncoding line below before the jsp:include does the trick.<% request.setCharacterEncoding("utf-8"); %>

Could the param value be dinamic? . If not, replace "í" for
í

Using JSTL worked here. It's more verbose though:
"head":
<%# page pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${title}</title>
</head>
"body":
<%# page pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="title" scope="request" value="Título"/>
<jsp:include page="consoleheader.jsp">
<body>
...
</body>
</html>

Related

contextPath is empty in JSP page while using it with Spring Boot

I want to load CSS on the JSP page. But I am getting empty pageContext.request.contextPath in JSP page. I am using this approach because if I move the page around in different directories within the templates or JSP then I should not need to keep on making changes to paths of referred CSS or js files.
Jsp page is as below
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%#page isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Home Page</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/default-theme.css" />
</head>
<body>
<div class="container">
<H2>Reached Home.</H2>
</div>
</body>
</html>
application.properties file is as below
spring.mvc.view.prefix=/templates/jsp/
spring.mvc.view.suffix=.jsp
I am using Spring version 2.6.0. Along with the following dependencies in pom.xml-
spring-boot-starter-web
spring-boot-starter-tomcat (scope: provided)
tomcat-embed-jasper (scope: provided)
jstl
As per Spring Boot, pageContext is Implicit. Then what could be going wrong here?
Any help or suggestions would be appriciated.
check if remove <%#page isELIgnored="false"%>
if not work test : request.getContextPath()
after all see this link :
pageContext.request.contextPath not working

JSTL not recognized in JSP after adding jar

I added jstl-1.2.jar to my lib folder, then I included:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
on top of my JSP file and still It says `Unknown tag (c:forEach).
welcome.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>Insert title here</title>
</head>
<body>
<c:forEach>
</c:forEach>
</body>
</html>
Project construction HERE
1.Use only jstl-1.2.jar
2.Try to get some items to iterate, for example:
<c:forEach items="${requestScope.list}" var="item">
</c:forEach>

<sec:authorize does not work in some jsp pages

I want to show the Authenticated user's firstname and lastname. I put related code segment in Header.jsp and include it in two other jsp pages, Index.jsp and News.jsp.
When Header.jsp included in News.jsp, it works correctly and displays Authenticated user's firstname and lastname, but when it's include in Index.jspm it doesn’t show anything.
My pages structure looks like blow:
Header.jsp
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div class="usertext">
<span id="loginedUserFullname">
<sec:authorize access="isAuthenticated()">
<sec:authentication property="principal.firstname" />
<sec:authentication property="principal.lastname" />
<span class="welcome">welcome</span>
<a class="logout" href='/view/cpanel/Logout.jsp'>(loge out)</a>
</sec:authorize>
</span>
</div>
</nav>
Index.jsp
<%# page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8" isELIgnored="false"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="keywords" content="">
<meta name="description" content="">
<jsp:include page="/includes/front/Head.jsp"></jsp:include>
<title></title>
</head>
<body>
<jsp:include page="/includes/front/Header.jsp"></jsp:include>
<jsp:include page="/includes/front/Scripts.jsp"></jsp:include>
</body>
News.jsp
<%# page contentType="text/html; charset=UTF-8" language="java" pageEncoding="UTF-8" isELIgnored="false"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="keywords" content="">
<meta name="description" content="">
<jsp:include page="/includes/front/Head.jsp"></jsp:include>
<title></title>
</head>
<body>
<jsp:include page="/includes/front/Header.jsp"></jsp:include>
<jsp:include page="/includes/front/Scripts.jsp"></jsp:include>
</body>
whats wrong with <sec> that doesn't work in the Index.jsp?
Is it possible that Index.jsp does not require the user the be authenticated but News.jsp does?

unable to compile class for jsp with root cause string index out of range

i have been facing this error whenever i try to run the code. what i do here is the jsp page is redirected from a filter using the following code:
getServletContext().getRequestDispatcher("/myPage.jsp").forward(request,response);
the browser displays the Error
Unable to compile class for jsp" along with the root cause "String index out of range: 0
the myPage.jsp page is as follows:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%# page session="false"%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>My page</title>
</head>
<body>
<p>My text</p>
</body>
</html>

Warning in an Anchor Tag in a JSP File

I am able see the warning in a jsp file for the anchor tag.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# page import="java.util.Date" %>
<%# 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>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bills Added</title>
</head>
<body>
<h1>Bills Added</h1>
<p>You have added a new bill at</p>
<c:url var="mainUrl" value="/bills/add" />
<p>Return to Main List</p>
</body>
</html>
In the line <p>Return to Main List</p> its showing a warning:
WebContent/WEB-INF/jsp/${mainUrl} not found.
I can understand about the c:url tag but in the href with the place holder ${mainUrl}, then its showing warning in all the files which I have placed like this format. Please neeed suggestion to fix this or why am getting this?

Categories