Reputation: 619
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);
}
}
Upvotes: 0
Views: 1197
Reputation: 159764
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;
Upvotes: 2