servlet开发及中文乱码处理
2009-07-31 11:10
218 查看
Myeclipse+tomcat进行servlet开发及中文乱码处理
2008年05月18日 星期日 下午 04:28
2008年05月18日 星期日 下午 04:28
开发步骤: 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+JSP+MySQL5.6模式下开发web应用出现的中文乱码处理
- Servlet开发GET和POST中文乱码处理(原创)
- 进行servlet开发及中文乱码处理 post与get
- 如何处理struts2开发中的中文乱码问题
- JSP和Servlet的中文乱码处理
- Web开发中遇到的中文乱码处理
- JavaWeb项目的中文乱码的原因以及Servlet中处理GET请求和POST请求编码过滤器
- JSP与Servlet的中文乱码处理
- Servlet中文乱码处理
- Servlet学习笔记(中文乱码处理)
- JSP和Servlet的中文乱码处理
- JSP和Servlet的中文乱码处理
- JSP和Servlet的中文乱码处理
- BS开发技巧之四: 处理取中文参数出现乱码问题
- JSP与Servlet的中文乱码处理
- JSP和Servlet的中文乱码处理
- JSP和Servlet的中文乱码处理
- Servlet中文乱码处理
- BS开发技巧之三: 处理中文文件名附件存到本地时缺省出现乱码问题
- JSP及Servlet中文乱码处理总结