我在将一个特殊字符(如cyrillic或umlauts)从jsp发送到servlet时遇到问题.我非常感谢你的帮助.
这是我做的:
在jsp中定义了utf-8字符集:
<%@ 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" />
...
<div class="authentication">
<form name="registerForm" action="register" method="get" accept-charset="UTF-8">
<input type="input" name="newUser" size="17"/>
<input type="submit" value="senden" />
</form>
</div>
...
Run Code Online (Sandbox Code Playgroud)在server.xml中为Connector设置Tomcat URIEncoding
<Connector URIEncoding="UTF-8" ...
Run Code Online (Sandbox Code Playgroud)在servlet内部 - 将CharacterEncoding设置为UTF并读取请求
public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("newUser");
System.out.println("encoding: "+request.getCharacterEncoding());
System.out.println("received: "+username);
Run Code Online (Sandbox Code Playgroud)以下是使用时显示的内容:Однако
encoding: UTF-8
received: ??????
Run Code Online (Sandbox Code Playgroud)我错过了什么吗?我想servlet无法正确解码String,但我不知道为什么会发生这种情况.我已经遵循了关于这个主题的所有建议,但没有运气.
提前致谢.
伙计们,首先感谢您的贡献,我在这里找到了很好的回复.然而,我遇到了一个我无法弄清楚的问题,如果有人可以提供任何帮助,我将不胜感激.
我正在用C#开发这个应用程序,可以将图像从计算机上传到用户的photoblog.为此,我是用于主要用PHP编写的photoblogs的pixig pixelpost平台.
我在这里和其他网页上搜索过,但那里提供的例子对我没用.以下是我在我的示例中使用的内容:( 使用HTTPWebrequest上传文件(multipart/form-data))和(http://bytes.com/topic/c-sharp/answers/268661-how-upload-file-via- c-code)一旦准备就绪,我将在互联网上免费提供,也可以创建一个Windows移动版本,因为我是pixpost的粉丝.
这是我用过的代码:
string formUrl = "http://localhost/pixelpost/admin/index.php?x=login";
string formParams = string.Format("user={0}&password={1}", "user-String", "password-String");
string cookieHeader;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
req.AllowAutoRedirect = false;
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
cookieHeader = resp.Headers["Set-Cookie"];
string pageSource;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
Console.WriteLine();
}
string getUrl = "http://localhost/pixelpost/admin/index.php";
HttpWebRequest getRequest …
Run Code Online (Sandbox Code Playgroud)