Hi if someone could look at my code and maybe see something I am not, this is for a lab for a class assignment of mine. Thank you!
I am programming a simple servlet in a dynamic web project using eclipse. One of the labs requirements is to grab an init parameter from the web.xml in a Servlet.
When I try to get the value of an init-param in my servlet. It keeps returning null.
Anyone see anything wrong that I am not:
im using command:
this.getServletConfig().getInitParameter("title");
In class ConvertServlet that is in a00730628.controller package with in the doPost function. I also tried from the init function but got null there to, so I am thinking there is an error in my xml. I am using version 3.0 and Tomcat 7.26
And here is my 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>
Edit added my Servlet Code:
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());
}
}
}
You are using the annotations in the servlet. To add the init parameter using annotations,follow the following syntax and remove the servlet configarations in
web.xml.If you don’t want to use annotations remove
@WebServlet("/convert")from the servlet class.