[尝试将我自己的包导入servlet时,包不存在错误

问题描述 投票:1回答:1

尝试使用tomcat将我自己的包导入到im制作的servlet中。

正在使用的两个Java文件是servlet(movieServlet.java),另一个正在尝试从包中导入的类(Movie.java)

文件夹的层次结构是:

classes
|
+--movieServlet.java
|
+--movie
   |
   +--Movie.java

movieServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import movie.Movie;

@WebServlet(urlPatterns = {"/movieServlet"})
public class movieServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    Movie Movie = new Movie();
    request.setAttribute("movies", Movie.getAllMovies());

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsps/movies.jsp");
    dispatcher.forward(request,response);
} 


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    PrintWriter out = response.getWriter();


    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/jsps/movieDetails.jsp");
    dispatcher.forward(request,response);
} 

}

这是Movie.java的顶部(它是一个长文件)

package movie;
import java.util.*;
import java.util.List;
import javax.sql.*;
import java.sql.*;
import javax.naming.InitialContext;

public class Movie implements java.io.Serializable
{
private static DataSource dataSource = null;
private static Connection c = null;

private int id;
private String title;
private String url;
private int year;

public Movie(){}

如果需要更多信息,请询问:),这确实困扰了我。

java tomcat servlets package
1个回答
0
投票

我是新来的,如果我的回答不完美,请原谅。如前所述,您想导入自己的包含Movie类的包。我尝试实现您所有的主要方法,并且它运行完美,并且我从Movie.java中获得了价值。

movieServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import movie.Movie;

public class movieServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
              PrintWriter out = response.getWriter();
               Movie Movie = new Movie();
               request.setAttribute("movies", Movie.getAllMovies());
               out.println(request.getAttribute("movies"));
   }


 }

Movie.java

package movie;
public class Movie {
String s;
   public Movie(){
      s="hello zDoctor";   
   }
   public String getAllMovies(){
       return s;
   }
}

servlet output

file structure

© www.soinside.com 2019 - 2024. All rights reserved.