java - 错误 : cannot find symbol, 找不到我的 bean

标签 java tomcat

我得到了代码,我必须让它做其他事情。当我去编译我的 servlet 时,它无法识别我的 bean。我已经从所有不同的目录中删除、重新编译和尝试。我不知道为什么这不起作用。

import BH.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 
import java.util.*;
import java.util.function.*;
import static java.util.Arrays.asList;
import java.text.DateFormat;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;
public class sessionServlet extends HttpServlet {
    private    List<String[]>   the_sessions;
    private    DateFormat df;
    public static ReentrantLock thelock = new ReentrantLock();


    public void init() throws ServletException  {
        the_sessions=new ArrayList<String[]>();
        df=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {    
        if ((!(req.getParameter("task")==null))&&(req.getParameter("task").trim().equals("deploy"))) {
            PrintWriter out = res.getWriter();
            out.println("<html>");
            out.println("<body>");
            out.println("<hr /><center><h1>sessionServlet Deployed</h1></center><hr />");
            out.println("</body>");
            out.println("</html>"); 
            return;
        }
        Consumer <String> forwardTo =(s) ->ForwardTo(s,req,res);
        boolean is_first_visit=true;
        String[] this_session=new String[3];
        String manager = req.getParameter("manager");
        for (String [] a_session :the_sessions) {
            if (a_session[0].equals(manager)) {  //Found an active session
                is_first_visit=false;
                this_session=a_session;
                break;
            }
        }
        if ((req.getParameter("task")==null)&&(!is_first_visit)) {
            the_sessions.remove(this_session);
            is_first_visit=true; // just used http://hoare.cs.umsl.edu/servlet/js_test/sessionServlet
        }
        req.setAttribute("thesessioncount",the_sessions.size());
        if (is_first_visit) {
            if (the_sessions.size()==10) {
                forwardTo.accept("noSessions.jsp");  //No Available Sessions
                return;
            }
            String randomStr = getRandomString();
            String[] new_session = {randomStr,df.format(new Date()),"need a name"};
            the_sessions.add(new_session);
            this_session=new_session;
            req.setAttribute("manager",randomStr);
            forwardTo.accept("startSession.jsp");
            return;
        }
        String the_name="";
        String the_pw="";

        if (this_session[2].equals("need a name")) { //No name given yet
            the_name=req.getParameter("whoisit");
            the_pw=req.getParameter("passwd");

            if ((the_name==null)||(the_name.trim().length()==0)||checkPW(the_name,the_pw)) {       //checkPW returns false if correct
                the_sessions.remove(this_session);
                req.setAttribute("thesessioncount",the_sessions.size());
                forwardTo.accept("startSession.jsp");
                return;  // didn't enter a name in startSession
            }
        }
        this_session[2]=the_name.trim();
        req.setAttribute("thename", this_session[2]);
        if (tooLong(this_session[1],df.format(new Date()))) {  //Has the session timed out?
            the_sessions.remove(this_session);
            forwardTo.accept("Expired.jsp");
            return;
        } else {
            this_session[1]=df.format(new Date()); //reset the last session activity time
            NotesBean thesenotes=new NotesBean();

            if(req.getParameter("task").trim().equals("9")){
                the_sessions.remove(this_session);
                forwardTo.accept("exit.jsp");
                return;
            }   

            thelock.lock();
            if (!req.getParameter("task").trim().equals("0")) {                                                   //add ACC here, also show/update posts            
                thesenotes.setAll(req.getParameter("java_source"),Integer.parseInt(req.getParameter("version")));
                if (req.getParameter("task").trim().equals("2")) {
                    thesenotes.setNotes(req.getParameter("notes"),req.getParameter("java_source"),Integer.parseInt(req.getParameter("version")));
                }
            }
            req.setAttribute("thesessioncount",the_sessions.size());
            req.setAttribute("theBean",thesenotes);
            req.setAttribute("manager",manager);
            //req.setAttribute("theURL", "http://www.umsl.edu/~siegelj/turing.jpg");
            forwardTo.accept("getNotes.jsp");
            thelock.unlock();
            return;
        }
    }//end doGet

    boolean tooLong(String now,String then){
        //Check amount of time that passed
        return false;
    }
    boolean checkPW(String name,String password){
        AccountBean theAccount = new AccountBean();
        theAccount.setAccount(name);

        if(theAccount.isPassword(password))
            return false;

        return true;
    }

    public void log(String s){
        FileWriter fileWriter = null;
        try {
            String content =s+" at :"+new Date(System.currentTimeMillis()).toString()+"\n";
            File theLogFile = new File("C:/Tomcat/webapps/js_test/session.log");
            fileWriter = new FileWriter(theLogFile,true);
            fileWriter.write(content);
        } catch (IOException ex) {
        } finally {
            try {
                fileWriter.close();
            } catch (IOException ex) {

            }
        }

    }

    void ForwardTo(String s,HttpServletRequest req, HttpServletResponse res)
    {
        RequestDispatcher rd= req.getRequestDispatcher(s);
        try {
            rd.forward(req, res);
        } catch (IOException|ServletException is) {
            log(" req from "+s+" not forwarded at ");
            try {
                throw is;
            } catch (Exception e) {
            }
        }
    }

    public void destroy()
    {
        log("The instance was destroyed");
    }
    public String getRandomString(){
        byte[] randbyte=new byte[10];
        Random rand  = new Random(System.currentTimeMillis());
        for (int idx = 0; idx <10; ++idx) {
            int randomInt = rand.nextInt(26); //0<=randomInt<26
            //System.out.println(randomInt);
            randbyte[idx]=(byte)(randomInt+65);  
        }
        try {
            String rs=new String(randbyte, "UTF-8");
            //System.out.println(rs);
            return rs;
        } catch (Exception e) {
            //System.out.println("bad string");
            return "bad";
        }
    }

}

AccountBean.java

package mybeans;
import   BH.*;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Arrays;
import static java.util.Arrays.asList;
import java.sql.*;

public class AccountBean implements java.io.Serializable {  
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost/cs4010";
    static final String USER = "cs4010";
    static final String PASS = "cs4010";

    private String account="";
    private String password="";

    public AccountBean(){

    }

        public void setAccount(String a)
    {
        account = a;
    }

    public String getAccount()
    {
        return account;
    }

    public void setPassword(String p)
    {
        password = p;
    }

    public String getPassword(){
        return password;
    }

    public void createAccount()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
            Statement stmt = conn.createStatement();
         //     System.out.println("here: "+Bytes_Hex.String2HexString(n));
            String this_query="INSERT INTO ries_userpass (username, password) VALUES ("+account+","+password+");";
       //     System.out.println(this_query);
            stmt.executeUpdate(this_query);
            stmt.close();
            conn.close();
        } catch (Exception e) {

        }
        return ;
    }

    public boolean isPassword(String p)            //make this to use
    {
        try {
            Class.forName("com.mysql.jdbc.Driver"); 
            Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
            Statement stmt = conn.createStatement();
            String this_query=" SELECT * from ries_userpass WHERE username="+account+";"; 
            ResultSet rs = stmt.executeQuery(this_query);

            while (rs.next()) {                                 
                password=Bytes_Hex.HexString2String(rs.getString("posts")); 

                if(password.equals(p))
                {
                    rs.close();
                    stmt.close();
                    conn.close();
                    return true;
                }
            }

            rs.close();
            stmt.close();
            conn.close();
        } 
        catch (Exception e) {
            return false;
        }

        return false;
    }
}

最佳答案

添加此语句 import mybeans.*;,因为您正在使用 AccountBean 但您没有导入该类包;

import BH.*;
import mybeans.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 
import java.util.*;
import java.util.function.*;
import static java.util.Arrays.asList;
import java.text.DateFormat;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random;
public class sessionServlet extends HttpServlet {
    private    List<String[]>   the_sessions;
    private    DateFormat df;
    public static ReentrantLock thelock = new ReentrantLock();


    public void init() throws ServletException  {
        the_sessions=new ArrayList<String[]>();
        df=DateFormat.getDateTimeInstance(DateFormat.LONG,DateFormat.LONG);
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException
    {    
        if ((!(req.getParameter("task")==null))&&(req.getParameter("task").trim().equals("deploy"))) {
            PrintWriter out = res.getWriter();
            out.println("<html>");
            out.println("<body>");
            out.println("<hr /><center><h1>sessionServlet Deployed</h1></center><hr />");
            out.println("</body>");
            out.println("</html>"); 
            return;
        }
        Consumer <String> forwardTo =(s) ->ForwardTo(s,req,res);
        boolean is_first_visit=true;
        String[] this_session=new String[3];
        String manager = req.getParameter("manager");
        for (String [] a_session :the_sessions) {
            if (a_session[0].equals(manager)) {  //Found an active session
                is_first_visit=false;
                this_session=a_session;
                break;
            }
        }
        if ((req.getParameter("task")==null)&&(!is_first_visit)) {
            the_sessions.remove(this_session);
            is_first_visit=true; // just used http://hoare.cs.umsl.edu/servlet/js_test/sessionServlet
        }
        req.setAttribute("thesessioncount",the_sessions.size());
        if (is_first_visit) {
            if (the_sessions.size()==10) {
                forwardTo.accept("noSessions.jsp");  //No Available Sessions
                return;
            }
            String randomStr = getRandomString();
            String[] new_session = {randomStr,df.format(new Date()),"need a name"};
            the_sessions.add(new_session);
            this_session=new_session;
            req.setAttribute("manager",randomStr);
            forwardTo.accept("startSession.jsp");
            return;
        }
        String the_name="";
        String the_pw="";

        if (this_session[2].equals("need a name")) { //No name given yet
            the_name=req.getParameter("whoisit");
            the_pw=req.getParameter("passwd");

            if ((the_name==null)||(the_name.trim().length()==0)||checkPW(the_name,the_pw)) {       //checkPW returns false if correct
                the_sessions.remove(this_session);
                req.setAttribute("thesessioncount",the_sessions.size());
                forwardTo.accept("startSession.jsp");
                return;  // didn't enter a name in startSession
            }
        }
        this_session[2]=the_name.trim();
        req.setAttribute("thename", this_session[2]);
        if (tooLong(this_session[1],df.format(new Date()))) {  //Has the session timed out?
            the_sessions.remove(this_session);
            forwardTo.accept("Expired.jsp");
            return;
        } else {
            this_session[1]=df.format(new Date()); //reset the last session activity time
            NotesBean thesenotes=new NotesBean();

            if(req.getParameter("task").trim().equals("9")){
                the_sessions.remove(this_session);
                forwardTo.accept("exit.jsp");
                return;
            }   

            thelock.lock();
            if (!req.getParameter("task").trim().equals("0")) {                                                   //add ACC here, also show/update posts            
                thesenotes.setAll(req.getParameter("java_source"),Integer.parseInt(req.getParameter("version")));
                if (req.getParameter("task").trim().equals("2")) {
                    thesenotes.setNotes(req.getParameter("notes"),req.getParameter("java_source"),Integer.parseInt(req.getParameter("version")));
                }
            }
            req.setAttribute("thesessioncount",the_sessions.size());
            req.setAttribute("theBean",thesenotes);
            req.setAttribute("manager",manager);
            //req.setAttribute("theURL", "http://www.umsl.edu/~siegelj/turing.jpg");
            forwardTo.accept("getNotes.jsp");
            thelock.unlock();
            return;
        }
    }//end doGet

    boolean tooLong(String now,String then){
        //Check amount of time that passed
        return false;
    }
    boolean checkPW(String name,String password){
        AccountBean theAccount = new AccountBean();
        theAccount.setAccount(name);

        if(theAccount.isPassword(password))
            return false;

        return true;
    }

    public void log(String s){
        FileWriter fileWriter = null;
        try {
            String content =s+" at :"+new Date(System.currentTimeMillis()).toString()+"\n";
            File theLogFile = new File("C:/Tomcat/webapps/js_test/session.log");
            fileWriter = new FileWriter(theLogFile,true);
            fileWriter.write(content);
        } catch (IOException ex) {
        } finally {
            try {
                fileWriter.close();
            } catch (IOException ex) {

            }
        }

    }

    void ForwardTo(String s,HttpServletRequest req, HttpServletResponse res)
    {
        RequestDispatcher rd= req.getRequestDispatcher(s);
        try {
            rd.forward(req, res);
        } catch (IOException|ServletException is) {
            log(" req from "+s+" not forwarded at ");
            try {
                throw is;
            } catch (Exception e) {
            }
        }
    }

    public void destroy()
    {
        log("The instance was destroyed");
    }
    public String getRandomString(){
        byte[] randbyte=new byte[10];
        Random rand  = new Random(System.currentTimeMillis());
        for (int idx = 0; idx <10; ++idx) {
            int randomInt = rand.nextInt(26); //0<=randomInt<26
            //System.out.println(randomInt);
            randbyte[idx]=(byte)(randomInt+65);  
        }
        try {
            String rs=new String(randbyte, "UTF-8");
            //System.out.println(rs);
            return rs;
        } catch (Exception e) {
            //System.out.println("bad string");
            return "bad";
        }
    }

}

关于java - 错误 : cannot find symbol, 找不到我的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43564345/

相关文章:

java - IntelliJ IDEA 将一些未使用的方法变灰,但不是全部

java - Spring Boot Maven 插件 - 没有 BOOT-INF 目录

maven - 如何使用 tomcat8-maven-plugin 指定在另一个项目中构建的 WAR 以进行集成测试?

java - HTTPS Tomcat 重新路由请求

hibernate - 如何为 Hibernate 设置 Tomcat 7 Postgresql 数据源?

tomcat - WEB-INF 和/src/main/resources 文件夹有什么区别?

java - Spring ResourceLoader 在 JAR 中找不到资源

java - 为什么final可以在构造函数中初始化?

java - 是否可以将两种不同的样式应用于单个组件

tomcat - 如果其中的 Web 应用程序必须支持与 LDAPS 的连接,是否应将 tomcat 更改为 SSL 模式?