嗨,如果有人可以看我的代碼,也許看到我不是的東西,這是爲我的班級任務的實驗室。謝謝!JavaEE中的Init-param動態Web應用程序返回null
我正在使用eclipse編寫一個簡單的servlet在動態web項目中。其中一個實驗要求是從Servlet的web.xml中獲取init參數。
當我嘗試在我的servlet中獲取init-param的值時。它保持返回null。
任何人看到什麼不對,我不是:
IM使用命令:
this.getServletConfig().getInitParameter("title");
在課堂上ConvertServlet是在a00730628.controller包中的doPost功能。我也嘗試從init函數,但得到null,所以我想我的xml中有一個錯誤。我使用的3.0版本和Tomcat 7.26
這裏是我的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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>lab10</display-name>
<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>
<servlet>
<servlet-name>ConvertServlet</servlet-name>
<servlet-class>a00730628.controller.ConvertServlet</servlet-class>
<init-param>
<param-name>title</param-name>
<param-value>Temperature Converter Result</param-value>
</init-param>
</servlet>
</web-app>
編輯加入我的servlet代碼:
package a00730628.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import a00730628.util.TemperatureConverter;
import a00730628.view.Lab10Html;
/**
* Servlet implementation class ConvertServlet
*/
@WebServlet("/convert")
public class ConvertServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final int CEL_2_FAR = 0;
private static final int FAR_2_CEL = 1;
private String resultTitle = "";
/**
* @see HttpServlet#HttpServlet()
*/
public ConvertServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().println("<a href='index.html'>Please make a post request from here</a>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
resultTitle = this.getServletConfig().getInitParameter("title");
if (resultTitle == null) {
response.sendError(500, "Title param is null ... wa wa wa");
return;
}
int type = Integer.parseInt(request.getParameter("type"));
double number = Double.parseDouble(request.getParameter("number"));
double converted;
String result = "";
switch (type) {
case CEL_2_FAR:
converted = TemperatureConverter.celsiusToFarenheit(number);
result = String.format("%f celsius = %f farenheit", number, converted);
break;
case FAR_2_CEL:
converted = TemperatureConverter.farenheitToCelsius(number);
result = String.format("%f farenheit = %f celsius", number, converted);
break;
}
response.getWriter().print(Lab10Html.getLab10Html(resultTitle, result));
} catch (NumberFormatException e) {
response.sendError(400, "Please enter a number like 42. Number format error: "+ e.getMessage());
}
}
}
對不起,不包括,但沒有我沒在初始化 – 2012-03-30 21:37:27
調用super.init(配置)我要添加servlet代碼到我的文章 – 2012-03-30 21:42:32
它結束了在功能的doPost我 – 2012-03-30 21:45:17