I have a problem to display German special characters.
My Java Bean tests some user input and fills an error message string if there is an error. In the jsp I access the Bean's error message if there is one using JSTL Core like so:
<c:if test="${MyBean.errorMsg != ''}">
<c:out value="${MyBean.errorMsg}" />
</c:if>
The error message is: "Alle Felder müssen ausgefüllt werden.";
It is displayed in Chrome as follows: "Alle Felder müssen ausgefüllt werden."
I tried including the following in the JSP:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
which worked for all text I output directly in JSP but not via the
prefix c.
I tried to set the JSTL character encoding to UTF-8 via FTM:
<%# taglib uri = "http://java.sun.com/jsp/jstl/fmt" prefix = "fmt" %>
<fmt:requestEncoding value = "UTF-8" />
Which did not solve it.
I put the following in the Controller Servlet that handles the request:
request.setCharacterEncoding("UTF-8");
This did not have any effect.
Related
I am trying to set a variable (named as "o") in jsp in the body of tag - how could I do it without scriplets?
I have wrote this piece of code but it is not working:
<a class="overfl" href="myServlet?action=request.setAttribute('o',i)"> ${values[i]} </a>
Try with JSTL Core c:set Tag to set the attribute in any scope.
Sample code:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="salary" scope="request" value="${2000*2}"/>
ServletRequest#setAttribute() method doesn't return any value.
Get the value back in the same way as you are doing here ${values[i]}
or try with JSTL Core c:out Tag to get the value back.
In your case simply pass the action values as query parameter as shown below:
<a class="overfl" href="myServlet?action=${i}"> ${values[i]} </a>
And get the value back at server side using
String action = servletRequest.getParameter("action");
If the varaible is not already defined in your request's attribute so call <%request.setAttribute('o',i); %> then if you want to write it to the jsp output you have to to write <%request.getAttribute('o') %> in the place where you want to add it's value like this :
<%request.setAttribute('o',i); %>
<a class="overfl" href="myServlet?action=<%=request.getAttribute('o') %>"> ${values[i]} </a>
In my jsp page i am adding title attribute dynamically in anchor using this code.
<c:set var="titleAttributeValue" value="${(anchorListfields[3] != '') ? 'title=${anchorListfields[3]}' : ''}" scope="page" />
but rather than showing the value of title attribute in anchor it showing like this <span><span>Start my Business</span><i></i></span>
how can i achieve this where is my error in syntax
You codes seems to be working.
Ensure you have JSTL library in your /WEB-INF/lib.
Add <%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> directive to your page.
I am using glassfish 4
I have used these imports:
org.apache.commons.lang3.StringEscapeUtils java.util.Properties,java.util.Map
and few other package imports.
I have placed this code:
<%#page language="java" contentType="text/html; charset=UTF-8" %>
and this code: <% WebAppConfig webConf = new WebAppConfig( this.getServletContext() );
request.setCharacterEncoding("UTF-8");
%>
But there are few russian characters that I can not get. I'm trying to get 2 names declared as parameters in the URL, without any luck.
The URL is not encoded/escaped.
I tried 2 ways to get the parameter without any luck.
1st I tried: String fullName = request.getParameter("fullName");
What I got is: ������� ����� �����������
Then I tried to pass the variables to a bean:
EEventBean ee = new EEventBean(); and
ee.setFullName(request.getParameter("fullName"));
The output was the same.
The way I'm trying to print the result is:
<tr>
<td width="50%">осударственного работника</td>
<td width="50%" class="value"><%= ee.getFullName() %></td>
</tr>
Every russian word can be showed in the form, but I can't show the 2 parameters...
Is there a way to get the correct parameter from the URL?
EDIT: the server is Tomcat 5.5.28
What HTTP server are you using? It might well be failing to handle non-ASCII data when rendering JSP. Tomcat had a similar issue a few versions back: UTF-8 encoding fix for Tomcat and JSP.
in my application I need to prepare a path for XML file inside JSP page. I'm doing someting like this:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
<!DOCTYPE html>
<c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />
But there is a problem, I get the followind exception:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 6 in the jsp file: /users.jsp
String literal is not properly closed by a double-quote
3: <%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
4: <%# taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %>
5: <!DOCTYPE html>
6: <c:set var="abs_path" value='<%= getServletContext().getRealPath("").replace(" ", "%20").replace("\\", "/") %>' />
Apparently it's about this part: .replace("\\", "/")
When I delete it, I don't get this exception.
What's that about? I will be very grateful for any clue.
EDIT:
I use this variable in the following way:
<c:import url="file:/${abs_path}/MyProject/xml/users.xml" var="inputDoc" charEncoding="UTF-8" />
<c:import url="xsl/users_list.xsl"
var="stylesheet" charEncoding="UTF-8" />
<x:transform
xml = "${inputDoc}"
xslt = "${stylesheet}">
</x:transform>
It is not about "\\" being a regular expression ... because that wouldn't result in a JSP compilation error. (And besides, the argument of String.replace(String) isn't interpreted as a regex.)
However, the compilation error does seem to be saying that you need double escaping, and I think that the reason is that the JSP syntax is "consuming" one level of escaping itself ... in this context.
This is from the JSP 2.1 spec:
JSP.1.6 Quoting and Escape Conventions
...
Quoting in Attributes
Quotation is done consistently regardless of whether the attribute value is a
literal or a request-time attribute expression. Quoting can be used in attribute
values regardless of whether they are delimited using single or double quotes. It is
only required as described below.
A ‘ is quoted as \’. This is required within a single quote-delimited attribute
value.
A “ is quoted as \”. This is required within a double quote-delimited attribute
value.
A \ is quoted as \\
Only when the EL is enabled for a page (see Section JSP.3.3.2, “Deactivating
EL Evaluation”), a literal $ can be quoted by \$. Similarly, a literal # can be
quoted by \#. This is not required but is useful for quoting EL expressions.
A %> is quoted as %\>
A <% is quoted as <\%
The entities ' and " are available to describe single and double
quotes.
Anyway, try writing the offending code fragment as replace("\\\\", "/").
How do I access a JSP tag's attribute value within a JSTL tag? In the code below, I would like to access the url attribute and test if it's empty. I am using the JSTL 1.0 specification.
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%# attribute name="url" required="false"%>
<c:if test="${!empty url}">
...
</c:if>
If you're creating tag files, then you're using at least JSP 2.0 - which means you should be using at least JSTL 1.1. At any rate, the attribute directive should create a page-scoped var with the same name as its name attribute (unless it's optional and not provided). So, can you provide any more detail on the errors and/or output you're observing?