为什么Java会出现这个异常?
我正在研究 servlet,并且正在尝试创建一个小程序来花时间完成我创建的课程。但我却遇到了例外。这是代码:
用户
package Business;
public class User {
private String userId;
public User() {
userId = "";
}
public User(String id) {
userId = id;
}
public void setUserId(String id) {
userId = id;
}
public String getUserId() {
return userId;
}
}
时间
package Business;
import java.util.Calendar;
import java.util.GregorianCalendar;
import business.User;
public class Hours {
private User user;
private Calendar cal = new GregorianCalendar();
private int min = 0;
public Hours(User user) {
this.user = user;
min = cal.get(Calendar.MINUTE);
}
public String getMinutes() {
return Integer.toString(min);
}
public User getUser() {
return user;
}
}
检查用户
package process;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Business.User;
import Business.Hours;
public class CheckUser extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = request.getParameter("userID");
User user1 = new User(userId);
Hours h1 = new Hours(user1);
String url = "/show.jsp";
request.setAttribute("hours", h1);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = request.getParameter("userID");
User user1 = new User(userId);
Hours h1 = new Hours(user1);
String url = "/show.jsp";
request.setAttribute("hours", h1);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = request.getParameter("userID");
User user1 = new User(userId);
Hours h1 = new Hours(user1);
String url = "/show.jsp";
request.setAttribute("hours", h1);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
索引
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Project</title>
</head>
<body>
<h1>Employees</h1>
<form action="<%=response.encodeURL("CheckUser")%>" method="get">
<input type="submit" value="Diogo">
<input type="hidden" name="userId" value="1">
</form>
</body>
</html>
显示
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%@ page import="Business.*, process.*" %>
<%
Hours h1 = (Hours) request.getAttribute("hours");
String userId =h1.getUser().getUserId();
%>
<h1><%= userId %></h1>
</body>
</html>
异常
类型异常报告
消息
描述服务器遇到内部错误(),阻止 它无法满足此请求。
异常
<块引用>java.lang.RuntimeException:无法编译的源代码 - 错误的 ctor sym 类型:process.CheckUser.doGet(CheckUser.java:37) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 注意 Apache 中提供了根本原因的完整堆栈跟踪 Tomcat/7.0.23 日志。
I'm studying servlets and I'm trying to create a small program to get the hour through a class that I've created. But I've bee getting a exception. Here is the code:
User
package Business;
public class User {
private String userId;
public User() {
userId = "";
}
public User(String id) {
userId = id;
}
public void setUserId(String id) {
userId = id;
}
public String getUserId() {
return userId;
}
}
Hours
package Business;
import java.util.Calendar;
import java.util.GregorianCalendar;
import business.User;
public class Hours {
private User user;
private Calendar cal = new GregorianCalendar();
private int min = 0;
public Hours(User user) {
this.user = user;
min = cal.get(Calendar.MINUTE);
}
public String getMinutes() {
return Integer.toString(min);
}
public User getUser() {
return user;
}
}
CheckUser
package process;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Business.User;
import Business.Hours;
public class CheckUser extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = request.getParameter("userID");
User user1 = new User(userId);
Hours h1 = new Hours(user1);
String url = "/show.jsp";
request.setAttribute("hours", h1);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = request.getParameter("userID");
User user1 = new User(userId);
Hours h1 = new Hours(user1);
String url = "/show.jsp";
request.setAttribute("hours", h1);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userId = request.getParameter("userID");
User user1 = new User(userId);
Hours h1 = new Hours(user1);
String url = "/show.jsp";
request.setAttribute("hours", h1);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
Index
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Project</title>
</head>
<body>
<h1>Employees</h1>
<form action="<%=response.encodeURL("CheckUser")%>" method="get">
<input type="submit" value="Diogo">
<input type="hidden" name="userId" value="1">
</form>
</body>
</html>
Show
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%@ page import="Business.*, process.*" %>
<%
Hours h1 = (Hours) request.getAttribute("hours");
String userId =h1.getUser().getUserId();
%>
<h1><%= userId %></h1>
</body>
</html>
Exception
type Exception report
message
description The server encountered an internal error () that prevented
it from fulfilling this request.exception
java.lang.RuntimeException: Uncompilable source code - Erroneous ctor sym type: process.CheckUser.doGet(CheckUser.java:37)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) note The
full stack trace of the root cause is available in the Apache
Tomcat/7.0.23 logs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您收到异常是因为您已部署并尝试执行包含编译错误的代码。
我怀疑编译错误是由于这一行造成的:
因为您实际上在
Business
包中声明了User
类,而不是business
包。我有一种感觉,你无视 Java 命名约定是搬起石头砸自己的脚:
You are getting an exception because you've deployed and attempted to execute code with a compilation error in it.
And I suspect that the compilation error is due to this line:
because you've actually declared the
User
class in theBusiness
package, not thebusiness
package.I have a feeling that you have shot yourself in the foot by ignoring the Java naming conventions:
您的包是
package Business;
但您使用了Hours
类importbusiness.User;
请记住包区分大小写,根据标准它应该是小写的
your package is
package Business;
butHours
class you have usedimport business.User;
remember package is case sensitive and as per standard it should be in lowercase
您在包名称中使用了两种不同的大小写:
包名称应全部小写(或者至少每个段应以小写开头),以遵循正常的 Java 命名约定。
标准化您的
business
包并重新编译。You are using two different cases for in your package names:
Package names should be all lower case (or at least each segment should start with a lower case) to follow normal Java naming conventions.
Normalize your
business
package and re-compile.我唯一认为可能错误的是,在
Hours.java
中您导入了一个类business.User
但在CheckUser.java
中您使用 Business.User 类。Java 区分大小写。这是两个不同的类,因此代码中的类型不匹配。
The only think I can see that might be wrong is that in
Hours.java
you import a classbusiness.User
but inCheckUser.java
you use a classBusiness.User
.Java is case sensitive. These are two different classes, so the types in the code does not match.
在 process.CheckUser 中,您创建一个 Business.User,但在 Business.Hour 中,您需要一个 business.User。请注意 Business 和 Business 中的不同情况。
In process.CheckUser you create a Business.User, but in your Business.Hour you require a business.User. Notice the different case in Business and business.