0

我正在使用 Apache TomCat 服务器在 eclips 中运行 WEB 服务程序。我正在手动运行它。在此期间,它会给我错误,即找不到符号 LoginResource.class。

我有两个文件 LoginResource.java 和 LoginApplication.java。两个文件都被编译但是当我在服务器上运行它们时它给了我这个错误。当我直接在服务器上运行整个项目时,它会运行,但是当我手动运行服务器时会出错。

为了解决这个问题,我将两个文件(.class)放在同一个文件夹中,但仍然是同样的错误。任何人都有解决这个问题。以下是我的两个 .java 文件- LoginApplication---->

package com.login;

import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

public class LoginApplication extends Application {

    public Restlet createInboundRoot() {
    Router router = new Router(getContext());
    router.attach("/login", LoginResource.class);

    return router;
    }
}

登录资源--->

package com.login;

import org.json.JSONObject;
//import org.restlet.data.CharacterSet;
import org.restlet.data.Form;
//import org.restlet.data.Language;
//import org.restlet.data.MediaType;
import org.restlet.data.Parameter;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;
import java.sql.*;

public class LoginResource extends ServerResource {

    String name = "Login Example";


    public String getName()
    {
        return name;
    }


    protected Representation put(Representation input) {

        //Representation r = null;
        JSONObject jsonResult = new JSONObject();

        Connection con = null;
        String dbUrl = "jdbc:mysql://localhost:3306/loginDb";
        String driver = "com.mysql.jdbc.Driver";

        //String result = "Login Failed ...";

        try {

            Class.forName(driver).newInstance();
            con = DriverManager.getConnection(dbUrl, "loginDb", "loginDb");
            System.out.println("Connected to the database");

            Statement stm = con.createStatement();
            ResultSet rs = null;

            String inputText = input.getText();
            System.out.println(inputText);
            Form form = new Form(inputText);

            String email = "";
            String password = "";
            for (Parameter entry : form) {

                if(entry.getName().equals("email"))
                {
                    email = entry.getValue();   
                }

                if(entry.getName().equals("password"))
                {
                    password = entry.getValue();                            
                }
                System.out.println(entry.getName() + "=" + entry.getValue());
            }

         if(email.equals("") || password.equals(""))
         {
             System.out.println("EmailId or Password not provided");
             jsonResult.put("success", false);
             jsonResult.put("message", "EmailId or Password not provided");
         }
         else
         {
            //rs = stm.executeQuery("select * from users where email = '" + email + "' and password = '" + password + "'");
            String query = "select * from users where email='" + email + "' and password='" + password + "'";
            rs = stm.executeQuery(query);

            System.out.println("Query = " + query);

            if(rs.next())
            {
                query = "select * from users where email='" + email + "' and password='" + password + "' and verified = 1";
                ResultSet rs2 = stm.executeQuery(query);

                if(rs2.next())
                {
                    System.out.println("Logged In successfully");
                    jsonResult.put("success", true);
                    jsonResult.put("message", "Logged In successfully");
                }
                else
                {
                    System.out.println("Please confirm ");
                    jsonResult.put("success", false);
                    jsonResult.put("message", "Please confirm");
                }
                rs2.close();
            }
            else
            {
                System.out.println("In correct username or password");
                jsonResult.put("success", false);
                jsonResult.put("message", "In correct username or password");
            }

            /*r = new StringRepresentation(
                    jsonResult.toString(),
                    MediaType.APPLICATION_JSON,
                    Language.ALL,
                    CharacterSet.UTF_8);*/

            rs.close();
         }

        stm.close();
        con.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println(jsonResult);
        return new StringRepresentation(jsonResult.toString());
    }

}
4

1 回答 1

0

问题原因: class文件必须放在eclipse的.metadata目录下,不能放在tomcat webapp目录下(看你用什么eclipse插件)

解决方案:在 tomcat 中部署您的应用程序(war 文件),现在您应该可以正常启动应用程序了

于 2012-04-24T12:50:17.217 回答