为什么Java会出现这个异常?

发布于 2024-12-21 22:10:08 字数 4410 浏览 0 评论 0原文

我正在研究 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青丝拂面 2024-12-28 22:10:08

您收到异常是因为您已部署并尝试执行包含编译错误的代码。

我怀疑编译错误是由于这一行造成的:

import business.User;

因为您实际上在 Business 包中声明了 User 类,而不是 business 包。


我有一种感觉,你无视 Java 命名约定是搬起石头砸自己的脚:

  • 包名称应该全部小写。
  • 包名称应以唯一标识您的“业务”的字符串作为前缀,以最大程度地减少您的包/类名称与其他名称冲突的风险。按照惯例,这是一个反向 DNS 名称

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:

import business.User;

because you've actually declared the User class in the Business package, not the business package.


I have a feeling that you have shot yourself in the foot by ignoring the Java naming conventions:

  • Package names should be all lower-case.
  • The package name should be prefixed with a string that uniquely identifies your "business" to minimize the risk that your package/class names will collide with someone elses. Conventionally this is a reverse DNS name
信仰 2024-12-28 22:10:08

您的包是 package Business; 但您使用了 Hoursimportbusiness.User;
请记住包区分大小写,根据标准它应该是小写的

your package is package Business; but Hours class you have used import business.User;
remember package is case sensitive and as per standard it should be in lowercase

万人眼中万个我 2024-12-28 22:10:08

您在包名称中使用了两种不同的大小写:

package Business;
import business.User;

包名称应全部小写(或者至少每个段应以小写开头),以遵循正常的 Java 命名约定。

标准化您的 business 包并重新编译。

You are using two different cases for in your package names:

package Business;
import business.User;

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.

三寸金莲 2024-12-28 22:10:08

我唯一认为可能错误的是,在 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 class business.User but in CheckUser.java you use a class Business.User.

Java is case sensitive. These are two different classes, so the types in the code does not match.

无敌元气妹 2024-12-28 22:10:08

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文