0

我第一次尝试在我的 servlet 中编写注释。@WebServlet工作正常。当我添加时@webInitParam,我得到了红线。此外,当我尝试使用@POST它给我的注释时"POST cannot be resolved to a type"。这是我的代码:

package servlets;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet implementation class Calc
 */
@WebServlet (loadOnStartup = 1 , urlPatterns = { "/CoolPage" } , 
 initParams = {
        @WebInitParam(name="text" , value="hello" , description="simple text"),
    @WebInitParam(name="times", value="10" , description="times to print")
              }
) 
public class Calc extends HttpServlet {
    private static final long serialVersionUID = 1L;


public Calc() {
}


 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws              ServletException, IOException {
    // TODO Auto-generated method stub
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
@POST
protected void doThePost(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException {

    System.out.println("Inside the POST method");
    String username = request.getParameter("userName");
    String password = request.getParameter("password");

    request.setAttribute("userName", username);
    request.setAttribute("password", password);

    RequestDispatcher rd = request.getRequestDispatcher("jspGetting.jsp");
    rd.forward(request, response);
}

}
4

1 回答 1

2

Imports do not include sub-packages. Import the class from the javax.servlet.annotation package

import javax.servlet.annotation.WebInitParam;

It's hard to see how the servlet could compile without WebServlet being imported either(?).

import javax.servlet.annotation.WebServlet;

The POST annotation is located within the JAX-RS library

import javax.ws.rs.POST;
于 2013-10-31T17:37:22.130 回答