轉自:http://bbs.csdn.net/topics/210058964
旁白
今天將html程式碼改為jsp檔案時,遇到報錯,錯誤很低階,但是很深刻,特此將從csdn中查到的解決方案共享出來。
正題
在myeclipse中編寫程式碼如下:
q1.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>q1.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> </head> <body> <FORM METHOD=POST ACTION="q2.jsp"> 請輸入您的姓名: <INPUT TYPE=TEXT NAME="thename"> <INPUT TYPE=SUBMIT VALUE="SUBMIT"> </FORM> </body> </html>
q2.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1" pageEncoding="GBK"%> <% 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>My JSP 'q2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% String name=""; %> <% name=request.getParameter("thename"); session.putValue("thename",name); %> Your name : <%= name %> <p> <FORM METHOD=POST ACTION="q3.jsp"> The food you like : <INPUT TYPE=TEXT NAME="food"> <P> <INPUT TYPE=SUBMIT VALUE="SUBMIT"> </P> </FORM> </body> </html>
但在瀏覽器瀏覽瀏覽q2.jsp時卻出現如下錯誤:
HTTP Status 500 - -------------------------------------------------------------------------------- type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception org.apache.jasper.JasperException: /q2.jsp(1,1) Page directive must not have multiple occurrences of pageencoding org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40) org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407) org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:102) org.apache.jasper.compiler.Validator$DirectiveVisitor.visit(Validator.java:194) org.apache.jasper.compiler.Node$PageDirective.accept(Node.java:590) org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343) org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2393) org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2399) org.apache.jasper.compiler.Node$Root.accept(Node.java:489) org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2343) org.apache.jasper.compiler.Validator.validate(Validator.java:1700) org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:165) org.apache.jasper.compiler.Compiler.compile(Compiler.java:314) org.apache.jasper.compiler.Compiler.compile(Compiler.java:294) org.apache.jasper.compiler.Compiler.compile(Compiler.java:281) org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs. -------------------------------------------------------------------------------- Apache Tomcat/6.0.16
解決方案:
pageEncoding="ISO-8859-1" pageEncoding="GBK"%
不能指定多個pageEncoding
(/q2.jsp(1,1) Page directive must not have multiple occurrences of pageencoding)