无法使用注释运行简单的servlet

时间:2018-03-22 21:07:32

标签: java servlets annotations

我是新的java程序员。所以我确实理解了在web.xml中通过映射等运行servlet的过程,我也可以通过注释启动servlet,我的问题是当我使用注释,并在服务器上运行项目时,为什么我要编写要在其中启动项目名称后的URL中的serlvet名称。

有没有办法完全不使用web.xml文件,给出注释,然后当我在服务器上运行项目时,它会自动启动并运行servlet?而不是我编写servlet名称并点击输入以启动它。

@WebServlet(urlPatterns = {"/GetDate"})

public class GetDate extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Date date =new Date();

        resp.setContentType("text/html");//this says we will send back html type 
        //get a pen to write back
        PrintWriter pen=resp.getWriter();
        //write back
        pen.write(date.toString());

    }
}

我希望上面这个简单的servlet只要我从eclipse选择在服务器上运行就会自动启动。

3 个答案:

答案 0 :(得分:0)

您可以在注释中指定启动时加载,然后在Web服务器启动时调用servlet的init()方法

@WebServlet(urlPatterns = {"/GetDate"}, loadOnStartup=1)
public class GetDate extends HttpServlet{

@Override
public void init() throws ServletException {
    // do some init stuff
}

答案 1 :(得分:0)

而不是@WebServlet(urlPatterns = {"/GetDate"})这样写@WebServlet("/GetDate")

@WebServlet("/GetDate")
public class GetDate extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Date date =new Date();

        resp.setContentType("text/html");//this says we will send back html type 
        //get a pen to write back
        PrintWriter pen=resp.getWriter();
        //write back
        pen.write(date.toString());

    }
}

答案 2 :(得分:0)

全部采用@WebServlet表示法。如果您将代码更改为:

var conn = new SqlConnection("serverinfo");
SqlCommand query = new SqlCommand("SELECT Store.StoreID, Store.Name, 
Product.Name, " +
" StockRequest.Quantity, StoreInventory.StockLevel from Store" +
" LEFT JOIN StoreInventory ON StoreInventory.StoreID = Store.StoreID" 
+ " LEFT JOIN Product ON StoreInventory.ProductID = 
Product.ProductID" + " LEFT JOIN StockRequest ON StockRequest.StoreID 
= Store.StoreID", conn);

SqlDataReader read;

        try {
            conn.Open();
            read = query.ExecuteReader();
            while (read.Read()) {
                Console.WriteLine("{0} {1} {2} {3} {4}",
                                  read["StoreID"],
                                  read["Name"],
                                  read["Name"],
                                  read["Quantity"],
                                  read["StockLevel"]);

            }
            conn.Close();
            } catch (Exception e) {}

然后在@WebServlet(urlPatterns = {"/"}) public class GetDate extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Date date =new Date(); resp.setContentType("text/html");//this says we will send back html type //get a pen to write back PrintWriter pen=resp.getWriter(); //write back pen.write(date.toString()); } } URL(在您的webapplication名称之后)

提供servlet

小心这样做。传统上,您从/位置投放HTML资源。如果你用servlet接管它,那么就无法提供HTML。另外,如果你有多个servlet,那么哪一个获得/路径 - 其余的去哪里?将URL与servlet匹配可能有点自我记录。请注意,不需要保留与servlet相同的名称 - 如果您愿意,请将其命名为/ - 该名称取决于您。