在MyEclipse中设置JSP编码格式

来源:互联网 发布:avr单片机教程 郭天祥 编辑:程序博客网 时间:2024/05/16 12:33

 今天我新建了一个JSP文件,写了样例,启动服务器发现页面乱码。经过检查,发现新建的JSP文件的编码格式是“ISO-8859-1”,后来改成“UTF-8”,页面也不乱码了。随后,我想如果新建JSP默认的编码格式是“UTF-8”就好了。

在网上查询资料,我发现没有几个正确答案,于是问了一下同事,最终改了JSP默认编码。

1、未改编码之前,新建的JSP页面

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'school.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     This is my JSP page. <br>  
  27.   </body>  
  28. </html>  
2、依次点击“Window->Preference”

3、找到“MyEclipse->Files and Editors->JSP”


4、编码格式选择“UTF-8”


5、点击“Apply”,单击“OK”,然后再次新建JSP文件

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'MyJsp.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     This is my JSP page. <br>  
  27.   </body>  
  28. </html>  
0 0