最近寫jsp遇到一系列的低級錯誤,記錄下來權當前車可鑑吧。css
錯誤提示:html
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /jsp04/Zj04_sj_test1_1.jsp(2,1) Page directive must not have multiple occurrences of pageencodingjava
程序代碼以下:sql
1 <%@ page language="java" import="java.util.*" pageEncoding="gbk"%> 2 <%@ page language="java" import="jsp04.*" pageEncoding="gbk"%> 3 <%@ page import="javax.servlet.jsp.jstl.sql.Result;"%> 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 14 <title>民意調查</title> 15 16 <meta http-equiv="pragma" content="no-cache"> 17 <meta http-equiv="cache-control" content="no-cache"> 18 <meta http-equiv="expires" content="0"> 19 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 20 <meta http-equiv="description" content="This is my page"> 21 <!-- 22 <link rel="stylesheet" type="text/css" href="styles.css"> 23 --> 24 25 </head> 26 27 <body> 28 <br> 29 在校大學生是否適合作兼職 <br> 30 <br> 31 <% 32 //構造sql語句,取出調查 33 String sql="select * from surveyItem where surveyID=1"; 34 Object params[]={}; 35 //獲取調查項列表 36 Result result=DBHelp.runSelectSql(sql,params); 37 Map[] rows=result.getRows(); 38 //循環訪問每個調查項 39 for(int i=0;i<rows.length;i++){ 40 //獲得調查項目的ID 41 int id=Integer.parseInt(rows[i].get("id").toString()); 42 //獲得調查項 43 String item=rows[i].get("item").toString(); 44 %> 45 <input type="radio" name="radsurver" value="<%=id %>"><%=item %> 46 <% 47 } 48 %><br> 49 <input type="submie" name="Submit" value="提交"> 50 <input type="reset" name="Submit2" value="重置"> 51 </body> 52 </html>
首先,org.apache.jasper.JasperException 中的JasperException
這個異常時jsp引擎解析jsp頁面生成servlet時產生的,故錯誤是在轉譯階段,應該是頁面的語法有問題,而不是執行階段出錯
而後,檢查一下配置,發現頁面編碼設置了兩次,雖然是同樣的編碼,但仍是不被容許的
最後,第二行改成:
<%@ page import="jsp04.*" %>
OK !apache