一个html文件表单传输数据到一个java Servlet文件,中文怎么取得的是乱码?
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/l...
<!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=gbk">
<title>Insert title here</title>
</head>
<body>
<form action=HelloWorld method="post">
name:<input type=text name="username" /><br/>
password:<input type=password name="password"/><br/>
<input type=submit value="submit"/>
</form>
</body></html>
//HelloWorld.java
package com.bjsxt;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet.....");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost....");
PrintWriter out=response.getWriter();
out.println("<html><head><title></title></head><body>Hello World!</body></html>");
String str1= request.getParameter("password");
out.println(str1);
String str2=request.getParameter("username");
str2=new String(str2.getBytes("iso-8859-1"),"gbk");
out.println(str2);
}
} 展开
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<form action=HelloWorld method="post">
name:<input type=text name="username" /><br/>
password:<input type=password name="password"/><br/>
<input type=submit value="submit"/>
</form>
</body></html>
//HelloWorld.java
package com.bjsxt;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet.....");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost....");
PrintWriter out=response.getWriter();
out.println("<html><head><title></title></head><body>Hello World!</body></html>");
String str1= request.getParameter("password");
out.println(str1);
String str2=request.getParameter("username");
str2=new String(str2.getBytes("iso-8859-1"),"gbk");
out.println(str2);
}
} 展开
6个回答
展开全部
tomcat, server.xml 端口配置地方 加个URIEncoding="UTF-8"
页面和servlet全改成UTF-8
页面和servlet全改成UTF-8
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在doPost方法最前添加request.setCharacterEncoding("gbk")
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Java的默认编码是UTF-8,你要将编码格式转化一下,,request.setCharacterEncoding("gbk");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看你tomcat的编码设置了木有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在servletPrintWriter out=response.getWriter();前加上
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询