gpt4 book ai didi

Java ee 注释不起作用

转载 作者:行者123 更新时间:2023-12-02 06:35:27 26 4
gpt4 key购买 nike

我第一次尝试在我的 servlet 中编写注释。 @WebServlet 工作正常。当我添加 @webInitParam 时,我得到了红线。还,当我尝试使用 @POST 注释时,它给出了 “POST 无法解析为类型”。这是我的代码:

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

}

最佳答案

导入不包括子包。从javax.servlet.annotation包导入类

import javax.servlet.annotation.WebInitParam;

如果不导入 WebServlet,很难看出 servlet 是如何编译的(?)。

import javax.servlet.annotation.WebServlet;

POST 注释位于 JAX-RS library

import javax.ws.rs.POST;

关于Java ee 注释不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19713098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com