当前位置: 首页 > 知识库问答 >
问题:

JSP编译中的获取错误:“令牌”import“语法错误,该令牌后面需要名称”

东门航
2023-03-14

错误为

严重:路径为[/HelloWorld]的上下文中servlet[jsp]的servlet.service()引发异常[无法为jsp编译类:

在生成的Java文件[f:\Java EE project.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\catalina\localhost\helloworld\org\apache\jsp\game_jsp.]第[28]行出现错误,令牌“import”语法错误,该令牌后面需要名称

StackTrace:]根本原因为org.apache.jasper.JasperException:无法为JSP编译类:

在生成的Java文件[f:\Java EE project.metadata.plugins\org.eclipse.wst.server.core\tmp0\work\catalina\localhost\helloworld\org\apache\jsp\game_jsp.]第[28]行出现错误,令牌“import”语法错误,该令牌后面需要名称

堆栈跟踪:

at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:458)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:389)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:361)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:345)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:501)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我的代码是(newfile.jsp):

null

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hii i am sumit. (Welcome to JSP)</h1>
<form name="myform" action="game.jsp" method="post" enctype="multipart/form-data">
    Specify your name:<br />
      <input type="text" name="name" size="15"/><br />
  Specify your Image:<br />
    Specify your File:<br />
    <input type="submit" name="Submit" value="Submit your files"/>
</form>
</body>
</html>

null

(game.jsp):

null

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
   <%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.util.*"%>
 <%@ page import="org.apache.commons.io.*"%>
<%@ page import="org.apache.commons.io.filefilter.*"%>
<%@ page import="org.apache.commons.io.input.*"%>
 <%@ page import="org.apache.commons.io.monitor.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ page import="org.apache.commons.io.comparator.*"%>
<%@ page import="org.apache.commons.io.serialization.*" %>
 <%@ page import="java.io.*,java.util.*, javax.servlet.*," %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ServletFileUpload upload = new ServletFileUpload();

//Parse the request
List<FileItem> multifiles = upload.parseRequest(request);
try{
for(FileItem item : multifiles){
	 if(item.isFormField()){
		 out.println("sumit singh is king");
	 }
	 else{
		 
	 }
 }
}catch(Exception e){
   out.println("sf");
}
%>
</body>
</html>

null

共有2个答案

公西俊才
2023-03-14

game.jsp中删除上次导入的,使用下面的代码更新game.jsp

更新的game.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="org.apache.commons.fileupload.util.*"%>
<%@ page import="org.apache.commons.io.*"%>
<%@ page import="org.apache.commons.io.filefilter.*"%>
<%@ page import="org.apache.commons.io.input.*"%>
<%@ page import="org.apache.commons.io.monitor.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ page import="org.apache.commons.io.comparator.*"%>
<%@ page import="org.apache.commons.io.serialization.*" %>
<%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ServletFileUpload upload = new ServletFileUpload();

//Parse the request
List<FileItem> multifiles = upload.parseRequest(request);
try{
for(FileItem item : multifiles){
     if(item.isFormField()){
         out.println("sumit singh is king");
     }
     else{

     }
 }
}catch(Exception e){
   out.println("sf");
}
%>
</body>
</html>
有睿
2023-03-14

在上一次导入中添加了额外的,这使编译器期望再次导入,请改用:

 <%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
 类似资料: