日期:2014-05-16 浏览次数:20377 次
1、jsp页面test.jsp // jsp保存格式吗,设置为utf-8格式 <%@ page language="java" pageEncoding="utf-8"%> // jsp解码格式,设置为utf-8,不设置也没问题吗 // 百度网上资料说,如果不设置此处,页面的中文显示有问题,试了把好像并没问题呢。何解,待研究。 <%@ page contentType="text/html;charset=utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>jsp乱码问题</title> // 提示浏览器采用utf-8编码格式处理 <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> //提交到TestServlet.java类处理表单 <form action="testServlet" method="post"> <table> <tr> <td> 用户昵称* </td> <td> <input type="text" name="username" /> </td> </tr> <tr> <td> </td> <td> <input type="submit" value="提交测试"/> </td> </tr> </table> </form> </body> </html>
2、tomcat下web.xml配置如下 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>testServlet</servlet-name> <servlet-class>com.test.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>testServlet</servlet-name> <url-pattern>/testServlet</url-pattern> </servlet-mapping> </web-app>3、TestServlet.java如下
package com.test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // 处理get提交方式 this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // 处理post提交方式 String username = request.getParameter("username"); // servlet层测试乱码 System.out.println("servlet = "+username); // 显示成了乱码 } }