进行servlet开发及中文乱码处理 post与get
来源:互联网 发布:淘宝模板怎么制作 编辑:程序博客网 时间:2024/06/05 23:02
开发步骤:
1.新建一个web project,Project Name为FirstFormServlet,j2EE level 选Java EE5.0
2.工程右键选new->Html文件,文件名form.html
3.在form.html的<body></body>间输入
<form action="./FormServlet" method="GET">
<input name="username">
<input type="submit">
</form>
</BR>
<form action="./FormServlet" method="POST">
<input name="username">
<input type="submit">
</form>
4.工程右键->Myeclips->add and remove deployment,选择Tomcat 6.x或5.x进行发布
5.工程右键,新建一个Servlet,package为servlet,name为FormServlet
6.在弹出的对话框中把Servlet/JSP Mapping URL:改为/FormServlet,确定。
7.在FormServlet.java文件中修改代码
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("你输入的用户名:");
out.print(request.getParameter("username"));
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("你输入的用户名:");
out.print(request.getParameter("username"));
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
8.修改代码后重新发布,在Servers面板,FirstFormServlet工程右键或工程中右键->Myeclipse->.
9.浏览器中输入:http://localhost:8080/FirstFormServlet/form.html
在get:文本框中输入用户名:梦里香巴拉,提交
发现中文是乱码,在FormServlet.java文件doGet()及doPost()中修改
response.setContentType("text/html");为response.setContentType("text/html;charset=GBK");
10.修改代码后重新发布,及测试。(server面板,Browser可以打开看发布情况)
11.发现post方法的乱码仍存在。
在doPost()中修改为
request.setCharacterEncoding("GBK");
//response.setContentType("text/html");
response.setContentType("text/html;charset=GBK");
12.输入"张三"使用post,发现还有乱码。这是因为form.html中的编码为"utf-8"
改为 <meta http-equiv="content-type" content="text/html; charset=GBK">
13.在doGet()中 request.setCharacterEncoding("GBK");乱码还存在。使用GET方法编码转换。
改为 out.print("你输入的用户名:");
String username=request.getParameter("username");
username =new String(username.getBytes("ISO8859-1"),"GBK");
out.print(username);
14.输入中文名,测试doGet(),doPost都无乱码。
15.最终的代码如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("你输入的用户名:");
//GET方法代码转换
String username=request.getParameter("username");
username =new String(username.getBytes("ISO8859-1"),"GBK");
out.print(username);
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
//response.setContentType("text/html");
response.setContentType("text/html;charset=GBK");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC /"-//W3C//DTD HTML 4.01 Transitional//EN/">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print("你输入的用户名:");
out.print(request.getParameter("username"));
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
16.注意html文件中<form action="./Formservlet">要和web.xml文件配置中 <url-pattern>相一致,否则会出错:scription The requested resource (FormServlet) is not available.
<servlet-mapping>
<servlet-name>FormServlet</servlet-name>
<url-pattern>/FormServlet</url-pattern>
</servlet-mapping>
<form action="./FormServlet" method=...>
- 进行servlet开发及中文乱码处理 post与get
- Servlet开发GET和POST中文乱码处理(原创)
- servlet中post与get请求中文乱码解决
- servlet开发及中文乱码处理
- post、get处理中文乱码问题
- post及get请求中文乱码解决方案
- Servlet处理get请求中文乱码问题
- servlet post get乱码
- 解决servlet中文乱码问题及get、post请求方式的讲解
- post与get请求中文乱码问题
- get,post中文乱码
- get中文乱码/post
- 解决Servlet中get或post中文乱码问题
- 解决servlet在post/get传递中文乱码的问题
- Servlet中文乱码原因 解决 Get 和 Post 和客户端
- servlet中get和post的中文乱码解决
- Servlet中文乱码原因 解决 Get 和 Post 和客户端
- javaWeb开发如果处理get 请求跟post 请求的中文乱码
- javascript 向上滚动公告、水平滚动公告(翻页效果)
- poj2419
- Windows下JAVA环境变量的设置祥解[转]
- 工厂方法模式-4
- tomcat -server端口修改
- 进行servlet开发及中文乱码处理 post与get
- 使用 GNU profiler 来提高代码运行速度
- win7访问网络共享文件提示用户名或者密码错误
- 删除所有ascii编码的字符和数字...
- 解决commons.CommonsLogger (CommonsLogger.java:45) Could not find method
- Rose 2003 在 Xp Home不能安装”解决方案 [转]
- js的三种对话框
- DotNetBar
- linux查看系统信息命令