LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-18-2020, 02:20 PM   #1
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Rep: Reputation: 139Reputation: 139
JSP Content-TYpe not working


I'm using Firefox in debug mode (F12). I have a .jsp program in which I specify:
Code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
But whenever I submit any form I get a console error:
Code:
A form was submitted in the windows-1252 encoding which cannot encode all Unicode characters, so user input may get corrupted. To avoid this problem, the page should be changed so that the form is submitted in the UTF-8 encoding either by changing the encoding of the page itself to UTF-8 or by specifying accept-charset=utf-8 on the form element.
I've also tried
Code:
<%@ page contentType="text/html;encoding=UTF-8" %>
Same error. This happens on pretty much all of the JSP files I have which contain forms.

Why?
 
Old 08-18-2020, 03:14 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,075

Rep: Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475Reputation: 1475
On Firefox console you can see the Http-header fields too (in Network window). Content-Type in Http-header has priority over <meta> http tag.
 
Old 08-19-2020, 07:54 AM   #3
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 570

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366

The error message is pretty clear:
Quote:
A form was submitted in the windows-1252 encoding which cannot encode all Unicode characters, so user input may get corrupted. To avoid this problem, the page should be changed so that the form is submitted in the UTF-8 encoding either by changing the encoding of the page itself to UTF-8 or by specifying accept-charset=utf-8 on the form element.
i.e: update your form tag(s) to include that attribute, and the responses will be sent as UTF-8 irrespective of what the Content-Type header might be.
Code:
<form ... accept-charset=utf-8 >

As for setting the page encoding, leave Content-Type alone, make sure your response starts with "<!doctype html />", and then put "<meta charset=utf-8 />" right at the start of your HEAD section.

i.e the start of your document should look very similar to one of these:
Code:
<!doctype html /><meta charset=utf-8 />
Code:
<!doctype html /><html ... ><head><meta charset=utf-8 />
 
1 members found this post helpful.
Old 09-03-2020, 11:44 AM   #4
mfoley
Senior Member
 
Registered: Oct 2008
Location: Columbus, Ohio USA
Distribution: Slackware
Posts: 1,789

Original Poster
Rep: Reputation: 139Reputation: 139
boughtonp: yes, adding "accept-charset=utf-8" to the <form> tag worked. The "cannot encode all Unicode characters" message went away.

So, I take it that "<meta charset=utf-8 />" in the <head> section must not apply to forms? If that's the case, what is it used for, non-form related text such as general page content? Seems silly to exclude forms from inheriting this attribute.

With respect to forms, does the "accept-charset=utf-8" get applied to everything on the form, general text and <input> fields, or just <input> fields?
 
Old 09-03-2020, 12:08 PM   #5
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 570

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366

The meta tag is defining the charset/encoding for the current HTML page in the response the server sent, (which includes all the content of the page as it's received).

The form's attribute applies to the POST request that the client sends when the form is submitted, (which will include all fields in the form, probably excluding file uploads).

If those sentences aren't clear, it may be because you need to better understand the HTTP request and response process - this is fundamental to how websites work, and really useful knowledge, but is often glossed over in web development tutorials/etc.

 
  


Reply

Tags
charset, jsp


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP to Oracle with type 4 driver... problem. obedraju Ubuntu 1 10-18-2011 12:48 AM
JSP to Oracle with type 4 driver... problem obedraju Linux - Software 2 10-15-2011 09:54 AM
problem in adding dynamic content in via expressions in JSP gozlemci Programming 1 09-19-2011 04:00 PM
Getting Content-type/Mime type of the file using shellscript swathikumar Linux - Newbie 9 05-24-2011 11:48 PM
about Jakarta Struts, Jsp's and Tomcat.... problem iterating in a jsp poeta_boy Programming 2 09-06-2005 02:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration