为什么字符串会是Null呢
web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestResource</display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestResource</servlet-name>
<servlet-class>TestResource.TestResource</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestResource</servlet-name>
<url-pattern>/servlet/TestResource</url-pattern>
</servlet-mapping>
<env-entry>
<env-entry-name>hello</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>hello</env-entry-value>
</env-entry>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
TestResource。Java内容如下:
package TestResource;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestResource extends HttpServlet {
String hello1;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Context ctx;
try {
ctx = new InitialContext();
hello1=(String)ctx.lookup("hello");
} catch (NamingException e) {
e.printStackTrace();
}
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println("<b>注入的字符串为:</b><br/> - "+hello1+"<br/>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
doGet(request, response);
}
}
------解决思路----------------------
ctx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("相对于java:comp/env环境的名字");
hello1=envCtx.lookup("hello");
------解决思路----------------------
楼上正解
------解决思路----------------------
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
hello1= (String)envCtx.lookup("hello");
对比一下,发现你少了 Context envCtx = (Context)initCtx.lookup("java:comp/env"); 这行代码