File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes Sending characters to server using JSP <form> tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Sending characters to server using JSP <form> tag" Watch "Sending characters to server using JSP <form> tag" New topic
Author

Sending characters to server using JSP <form> tag

David Krcmarik
Greenhorn

Joined: Jun 02, 2011
Posts: 5

I want to send č, á, ě using tag <form> in JSP but I got this: Å¡, á, ě . Please can you help me where is the problem in this index.jsp file deployed to Tomcat?:

<%@ 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>Page</title>
</head><body>
<form>
<input type="text" name="first"/>
<input type="submit" value="Send" "/>
</form>
<%
String s1 = String.valueOf(request.getParameter("first"));
System.out.println(s1);
%>
<%=s1%>

<%= System.getProperty("file.encoding") %>
</body></html>

Does this JSP file works at your application server?
Dave
David Krcmarik
Greenhorn

Joined: Jun 02, 2011
Posts: 5

Here are some comments. I have tried it on Firefox 4.0.1 and IE 8.0 64 bit. My PC is running 64bit Windows7 and Tomcat 7.0. JDK is 1.6.0_24 and I have used the system property JAVA_TOOL_OPTIONS set to -Dfile.encoding=UTF8. Instead of <form> tag I have tried also <form method="get" accept-charset="UTF-8"> and also <form method="post" accept-charset="UTF-8">. I have also tried the response.setContentType("text/html; charset=UTF-8") and response.setCharacterEncoding("UTF-8"). But nothing helped.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Let me try to clarify all of that. When you say you want to "send" those characters from a JSP, you are actually talking about the HTML generated by that JSP. You want that HTML to send a request containing those characters. You aren't saying you want the JSP to generate some HTML which contains those characters. Right so far?

Then you have something about "my PC". That's the PC which is running your server, correct? Looking at the PC where the browsers are located isn't going to be relevant. Neither are the browsers, in fact.

So to summarize, if I understand correctly, you want your browser to send a request which contains non-ASCII characters and you want that request to be received correctly in your server. But what is happening instead is, despite your best attempts to configure everything to use UTF-8, something is causing the request to be treated as if it's in some other charset.

Well, first of all this all has nothing to do with the fact that your HTML was generated by a JSP. So the JSP forum isn't the right place for the question. So since your server is Tomcat, I'm going to move it to the Tomcat forum.

I was just working with this exact question the day before yesterday.So I can tell you the answer for Websphere, but not for Tomcat. But essentially you have to tell Tomcat to treat requests as if their charset was UTF-8. It appears that you tried to set Tomcat's file.encoding system property to UTF-8... did that work? You posted a JSP which answers that question but didn't tell us the answer.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I did a search of the Tomcat forum and found a few posts which mentioned configuration options like this:



Possibly that might also apply to your problem?
David Krcmarik
Greenhorn

Joined: Jun 02, 2011
Posts: 5

Hi thanks for reply. Unfortunately it did not help. Anyway your summary was exactly right - simply I want to send non-ascii characters via HTML generated using JSP to server. I have changed the configuration in server.xml as you suggested: <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" /> . My server runs on port 8081. Just to be exactly precise the body of my page now looks like this:
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String s1 = String.valueOf(request.getParameter("first"));
System.out.println(s1);
%>
<%=s1%> <br/>
<%= System.getProperty("file.encoding") %>

The response for <%= System.getProperty("file.encoding") %> is UTF-8. I have tried it also on JBoss 5.0.1 with the same problem.
What is very interesting the the whole thing works well when I (on JBoss) have similar example but the request.getParameter("first") is managed via getters/setters using Java Beans. When you deploy the JSP page into your server does it work correctly with non-ascii characters?

David Krcmarik
Greenhorn

Joined: Jun 02, 2011
Posts: 5

It actually helped - forget to restart servers. Thanks a lot! Anyway it works only with <form method="get"> but does not work with <form method="post"> . I have encountered similar problem on some forum but was not solved as well. Anyway I am glad that get method works :-)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

"GET" sends data as part of the URL. The URL is parsed by the server. That's why server configuration settings can be important.

"POST" sends data as part of the content. Content has a MIME type and encoding. So the place to set character set (code page) information would be on the HTML POST element itself. I think the "ACCEPT-CHARSET" attribute is probably the one you want.


Customer surveys are for companies who didn't pay proper attention to begin with.
David Krcmarik
Greenhorn

Joined: Jun 02, 2011
Posts: 5

Hi this does not help. But for POST I have discovered that request.setCharacterEncoding("UTF-8"); works :-) . Thanks all for your helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending characters to server using JSP <form> tag