无法编译Tomcat的index.jsp示例

时间:2014-04-07 18:06:09

标签: java jsp tomcat jaas scriptlet

每当我尝试运行Tomcat在index.jsp上提供的/jsp/security/protected示例时,我都会收到以下错误:

type Exception report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 31 in the jsp file: /index.jsp
util.HTMLFilter cannot be resolved to a type
28: <body bgcolor="white">
29: 
30: You are logged in as remote user
31: <b><%= util.HTMLFilter.filter(request.getRemoteUser()) %></b>
32: in session <b><%= session.getId() %></b><br><br>
33: 
34: <%


An error occurred at line: 38 in the jsp file: /index.jsp
util.HTMLFilter cannot be resolved to a type
35:   if (request.getUserPrincipal() != null) {
36: %>
37:     Your user principal name is
38:     <b><%= util.HTMLFilter.filter(request.getUserPrincipal().getName()) %></b>
39:     <br><br>
40: <%
41:   } else {


An error occurred at line: 56 in the jsp file: /index.jsp
util.HTMLFilter cannot be resolved to a type
53:     if (request.isUserInRole(role)) {
54: %>
55:       You have been granted role
56:       <b><%= util.HTMLFilter.filter(role) %></b><br><br>
57: <%
58:     } else {
59: %>


An error occurred at line: 61 in the jsp file: /index.jsp
util.HTMLFilter cannot be resolved to a type
58:     } else {
59: %>
60:       You have <i>not</i> been granted role
61:       <b><%= util.HTMLFilter.filter(role) %></b><br><br>
62: <%
63:     }
64:   }


An error occurred at line: 70 in the jsp file: /index.jsp
util.HTMLFilter cannot be resolved to a type
67: To check whether your username has been granted a particular role,
68: enter it here:
69: <form method="GET" action='<%= response.encodeURL("index.jsp") %>'>
70: <input type="text" name="role" value="<%= util.HTMLFilter.filter(role) %>">
71: </form>
72: <br><br>
73: 


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:476)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

该文件的代码是

<%
  if (request.getParameter("logoff") != null) {
    session.invalidate();
    response.sendRedirect("index.jsp");
    return;
  }
%>
<html>
<head>
<title>Protected Page for Examples</title>
</head>
<body bgcolor="white">

You are logged in as remote user
<b><%= util.HTMLFilter.filter(request.getRemoteUser()) %></b>
in session <b><%= session.getId() %></b><br><br>

<%
  if (request.getUserPrincipal() != null) {
%>
    Your user principal name is
    <b><%= util.HTMLFilter.filter(request.getUserPrincipal().getName()) %></b>
    <br><br>
<%
  } else {
%>
    No user principal could be identified.<br><br>
<%
  }
%>

<%
  String role = request.getParameter("role");
  if (role == null)
    role = "";
  if (role.length() > 0) {
    if (request.isUserInRole(role)) {
%>
      You have been granted role
      <b><%= util.HTMLFilter.filter(role) %></b><br><br>
<%
    } else {
%>
      You have <i>not</i> been granted role
      <b><%= util.HTMLFilter.filter(role) %></b><br><br>
<%
    }
  }
%>

To check whether your username has been granted a particular role,
enter it here:
<form method="GET" action='<%= response.encodeURL("index.jsp") %>'>
<input type="text" name="role" value="<%= util.HTMLFilter.filter(role) %>">
</form>
<br><br>

If you have configured this app for form-based authentication, you can log
off by clicking
<a href='<%= response.encodeURL("index.jsp?logoff=true") %>'>here</a>.
This should cause you to be returned to the logon page after the redirect
that is performed.

</body>
</html>

我认为这是一个经过良好测试的例子,我想我错过了一些东西,一个图书馆或其他东西。可能是什么问题?

JAAS实现没问题,因为如果直接指向login.jsp

我可以登录

4 个答案:

答案 0 :(得分:1)

基于日志,JSP引擎无法找到类util.HTMLFilter

如果它是第三方,那么jar文件应该在WEB-INF/lib中,如果它是您编写的类,则编译的* .class文件应该在WEB-INF/classes中,并且包名应该是对的。

答案 1 :(得分:0)

相关错误消息显示

util.HTMLFilter cannot be resolved to a type

因此,您应该检查是否在该软件包中有一个软件包util,一个类HTMLFilter,以及是否满足HTMLFilter所有的依赖项,例如该类的所有成员也在类路径中。

答案 2 :(得分:0)

您需要将其插入顶部

的JSP页面
<%@ page import="util.HTMLFilter" %>

如果没有它,JSP将不会编译下划线代码中引用的类。

答案 3 :(得分:0)

更准确地说,您需要做的就是从WEB-INF / classes / util /复制HTMLFilter.java源文件,并将其放在相应的Eclipse src / util / HTMLFilter.java中(根据需要创建目录),右键单击项目,Build Project,你就可以了。

如果您不使用Eclipse,则只需将同一目录中的HTMLFilter.class复制到相应的WEB-INF / classes / util /目录中。