我正在学习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;
}
}
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);
}
}
指数<%@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>
异常
type Exception report
消息描述服务器遇到一个内部错误(),阻止
异常
. lang。RuntimeException:不可编译的源代码-错误的操作系统类型:process.CheckUser.doGet(CheckUser.java:37)javax.servlet.http.HttpServlet.service (HttpServlet.java: 621)http. httpservlet .service(HttpServlet.java:722对根本原因的全栈跟踪在Apache中是可用的Tomcat/7.0.23日志。
您将得到一个异常,因为您已经部署并试图执行包含编译错误的代码。
我怀疑编译错误是由于这一行:
import business.User;
因为您实际上在Business
包中声明了User
类,而不是在business
包中。
我有一种感觉,你忽略了Java的命名约定,这是搬起石头砸自己的脚:
- 包名应该全部小写。
- 包名应该以唯一标识您的"业务"的字符串作为前缀,以最大限度地减少您的包/类名与其他人冲突的风险。通常这是一个反向DNS名称
您的包是package Business;
但Hours
类您使用import business.User;
记住,package是区分大小写的按照标准,它应该是小写的
在包名中使用了两种不同的大小写
package Business;
import business.User;
包名应该全部小写(或者至少每个片段都应该以小写开头),以遵循正常的Java命名约定。
规范business
包并重新编译
我唯一认为这可能是错误的是,在Hours.java
中,您导入business.User
类,但在CheckUser.java
中,您使用Business.User
类。
Java区分大小写。这是两个不同的类,所以代码中的类型不匹配。
正在处理中。CheckUser您创建一个业务。用户,但在您的业务。当您需要business.User时。请注意Business和Business中的不同情况。