Please help with my JSP Internationalization problem - java

I have problem with I18N in JSP, specifically, with forms.
When I enter some Czech characters (e.g., "ěščřžýá...") into my page one form, into the field "fieldOne", and then show text from that field on page two, instead of Czech characters I see this as "ÄÄ". (Note, the second page gets the Czech characters with "request.getProperty("fieldOne")")
Here is the source code:
Page one:
<%#page contentType="text/html"%>
<%#page pageEncoding="UTF-8"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html>
<head></head>
<body>
<form action="druha.jsp" method="post">
<input type="textarea" name="fieldOne">
<input type="submit">
</form>
</body>
</html>
Page two:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%# taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<html>
<head></head>
<body>
<h1>The text: </h1> <%=request.getProperty("fieldOne")%>
</body>
</html>
Thanks for help...

Which container are you using? This information is important for this kind of problems.
Anyway, try calling
request.setCharacterEncoding("UTF-8");
before reading the parameter. Sometimes setting the page encoding in the header directive isn't enough. You definitely need to do this in Tomcat and servlets, I assume that this could be also the case for JSPs.

Related

It is possible to pass jsp code as a parameter?

suppose I have a header (which is common for all pages regarding one type of object).
<%# page session="false" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix="header" uri="headerDir" %>
<%# taglib prefix="tabs" uri="tabDir" %>
<div id="content" class="inside>
<header:myHeader headerData="${myModel}">
<tabs:myTabs argument="${someArg}"
....
so In views that are shared for different objects now I am doing
<c:choose>
<c:when test="${myModel.type ==FIRST_TYPE}>
<header:myHeader headerData="${myModel}">
</c:when>
<c:otherwise>
<header:secondHeader headerData="${myModel}">
</c:otherwise>
<c:choose>
but I would like to avoid this choose, I am able to send this
as a parameter as I am passing arguments? so can I do something like the following
<%# page session="false" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%# taglib prefix="header" uri="headerDir" %>
<%# taglib prefix="tabs" uri="tabDir" %>
<div id="content" class="inside>
<header:myHeader headerData="${myModel}">
<tabs:myTabs argument="${someArg}" headerToUse="${myHeader}" //in some way pass the header?
....

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>

jstl - unable to load class for jsp

When I try to add jstl tags into my jsp I get this:
HTTP Status 500 - org.apache.jasper.JasperException: Unable to load class for JSP
JSP may look like this:
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<html>
<body>
<c:forEach items="${list}" var="post" >
<tr>
<td>${post.author}</td>
</tr>
</c:forEach>
</body>
</html>
I also tried to write
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
the result is the same.
Why does it happen?

jsp:param encoding problems

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>

JSF RuntimeException: Cannot find FacesContext

When I write <h:outputText value="Login Name"/> tag in my JSP, I get the following exception message:
Cannot find FacesContext
Without that my JSP works fine. Here is my JSP:
<%# page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<body>
Login Name <input type="text" value=""/><br>
<h:outputText value="Login Name"/>
Password<input type="password" value=""/><br>
<input type="submit" value="Login">
</body>
</html>
There are two flaws in your code:
The root cause of this exception is that you forgot to pass the request through the url-pattern of the FacesServlet as definied in web.xml. If the JSP page is for example named page.jsp and the url-pattern of the FacesServlet is for example *.jsf, then you need to invoke it by http://example.com/context/page.jsf instead of .jsp. This way the FacesServlet will be invoked and create the FacesContext. Otherwise the JSF components in the page will complain that the FacesContext cannot be found and you will face this particular exception.
The <f:view> is missing in the page. Wrap the entire <html> in it. E.g.
<%# page pageEncoding="UTF-8" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<!doctype ... >
<f:view>
<html>
...
</html>
</f:view>
By the way, that import attribute in the <%#page> is completely superfluous. Get rid of it.

Categories