我正在使用servlet 3.0+容器中提供的「@Resource String ...」注入來輕鬆地向servlet提供配置參數。如果密鑰不存在於JNDI中(表示配置錯誤),我希望缺省值能夠工作並失敗@Resource error:「命名空間中foo.NewServlet/userName的命名綁定已存在」
我已經在Netbeans 8.2中用Glassfish 4.1.1玩過一個簡單的servlet,我希望userName
場,和setFullName(String fullName)
集:
package foo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {
@Resource(description="user name")
String userName;
private String fullname;
@Resource()
public void setFullName(String fullName){
this.fullname = fullName;
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("Full name = " + fullname);
out.println("<h1>Servlet NewServlet at " + request.getContextPath() + "</h1>");
out.println("Username = " + userName);
out.println("</body>");
out.println("</html>");
}
}
// Autogenerated stuff omitted
}
沒有 「web.xml中」 的字段爲空只(無故障)。然後我玩弄了「web.xml」,看看我可以如何定義它。 「java:comp/env/foo:NewServlet/fullName」名稱是Glassfish 4.1.1似乎創建的全名設置器的名稱。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<env-entry >
<env-entry-name>java:comp/env/foo.NewServlet/fullName</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>!BAR!</env-entry-value>
</env-entry>
<env-entry >
<env-entry-name>java:comp/env/foo.NewServlet/userName</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>!USERNAME!</env-entry-value>
</env-entry>
</web-app>
這再失敗,
Severe: Exception while deploying the app [WebApplication4] : Naming binding already exists for foo.NewServlet/userName in namespace {java:module/env/foo.NewServlet/userName=Env-Prop: java:comp/env/foo.NewServlet/[email protected] [email protected]@[email protected]@, java:module/env/foo.NewServlet/fullName=Env-Prop: java:comp/env/foo.NewServlet/[email protected] [email protected]@[email protected]@}
沒有什麼別的,但在該項目這兩個文件。顯然我誤解了一些基本的東西,但是閱讀Java EE教程並尋找建議並沒有幫助我。我真的很喜歡兩件事:
- 要麼使用容器生成的默認值或只是像「我們的應用程序。全名」這樣的鍵提供任何提示@資源標記。
- 如果有任何錯誤,包括鍵值不存在,則會大聲地失敗。
建議?一個好的答案會給500點獎勵。
在Wildfly/Glassfish行爲上打開的單獨問題 - https://stackoverflow.com/q/45716813/53897 –