servlet开发及中文乱码处理
来源:互联网 发布:吉林大数据在职研究生 编辑:程序博客网 时间:2024/06/05 19:32
开发步骤:
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开发及中文乱码处理
- 进行servlet开发及中文乱码处理 post与get
- JSP及Servlet中文乱码处理总结
- JSP及Servlet中文乱码处理总结
- servlet中文乱码处理
- Servlet中文乱码处理
- Servlet中文乱码处理
- Servlet 处理中文乱码
- Servlet中文乱码处理
- Servlet(11)HttpServletResponse、HttpServletRequest详解及中文乱码处理
- Servlet开发GET和POST中文乱码处理(原创)
- Servlet中文乱码的处理
- Servlet中文乱码处理小结
- Servlet 中中文乱码处理
- Web开发中的中文处理及乱码问题解决
- 处理struts servlet jsp 中文乱码问题
- JSP和Servlet的中文乱码处理
- JSP与Servlet的中文乱码处理
- .net1.1程序在windows server 2008下部署
- (MSDN)提高时间关键代码的技巧
- java开源框架集成常见错误之Cannot find bean under name org.apache.struts.taglib.html.BEAN
- 触发器
- MOSS 2007管理中心如何管理员修改密码
- servlet开发及中文乱码处理
- 学了5天Arm,今天谈谈初学感受
- Python的运算符优先级
- 子框架中使用javascript更改当前浏览器窗口的url
- rs.open "select * from bbs",conn,1,1 参数说明
- 暂别pb
- C++中随机函数rand()和srand()的用法
- 使用gzip减少流量
- 数据库类