简体   繁体   中英

Java ee annotations wont work

I'm trying to write annotations for the first time in my servlet. The @WebServlet is working fine. It is when I add @webInitParam that I get the red line. Also, when I try to use the @POST annotation it gives me "POST cannot be resolved to a type" . Here's my code:

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);
}

}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM