与 com.ibm.example 的依赖关系会被添加至“已导入的软件包”部分。请注意,因为已使用 Web 3.0 支持配置了捆绑软件项目,所以已为 Servlet 软件包(例如 javax.servlet)添加了额外的导入条目。
单击完成。这时,会在 Web 项目中创建 ConverterServlet。
import com.ibm.example.EJBConverterLocal;
import java.io.PrintWriter;
import java.text.NumberFormat;
import javax.naming.InitialContext;
import javax.naming.NamingException;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter w = response.getWriter();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
try {
InitialContext context = new InitialContext();
EJBConverterLocal converter = (EJBConverterLocal) context
.lookup("osgi:service/" + EJBConverterLocal.class.getName());
String temperature = request.getParameter("temperature");
if (temperature == null) {
w.println("<p>A temperature value was not specified.</p>");
} else if (!temperature.matches("[-+]?[0-9]*\\.?[0-9]+")) {
w.println("Invalid temperature specified.");
} else {
double degrees = Double.parseDouble(temperature);
double celsius = converter.convertToCelsius(degrees);
w.println("<p>" + temperature + " degrees fahrenheit is "
+ nf.format(celsius) + " degrees celsius</p>");
double fahrenheit = converter.convertToFahrenheit(degrees);
w.println("<p>" + temperature + " degrees celsius is "
+ nf.format(fahrenheit) + " degrees fahrenheit</p>");
}
w.println("<a href='index.html'>Back</a>");
} catch (NamingException e) {
w.println(e.getMessage());
} catch (NumberFormatException e) {
w.println("An incorrect temperature was specified");
}
}
EJBConverterLocal converter = (EJBConverterLocal) context.lookup("osgi:service/" + EJBConverterLocal.class.getName());
要转换的温度值会通过 temperature 参数传递至 Servlet。此值会转换为名为 degrees 的双精度值,该双精度值可由转换器对象上的 convertToCelsius() 和 convertToFahrenheit() 方法进行处理。EJBConverterRemote converter = (EJBConverterRemote) context.lookup("osgi:service/" + EJBConverterRemote.class.getName() + "/(service.exported.interfaces=*)");
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
package com.ibm.example.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.NumberFormat;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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 com.ibm.example.EJBConverterLocal;
@WebServlet("/ConverterServlet")
public class ConverterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ConverterServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter w = response.getWriter();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
try {
InitialContext context = new InitialContext();
EJBConverterLocal converter = (EJBConverterLocal) context
.lookup("osgi:service/" + EJBConverterLocal.class.getName());
String temperature = request.getParameter("temperature");
if (temperature == null) {
w.println("<p>A temperature value was not specified.</p>");
} else if (!temperature.matches("[-+]?[0-9]*\\.?[0-9]+")) {
w.println("Invalid temperature specified.");
} else {
double degrees = Double.parseDouble(temperature);
double celsius = converter.convertToCelsius(degrees);
w.println("<p>" + temperature + " degrees fahrenheit is "
+ nf.format(celsius) + " degrees celsius</p>");
double fahrenheit = converter.convertToFahrenheit(degrees);
w.println("<p>" + temperature + " degrees celsius is "
+ nf.format(fahrenheit) + " degrees fahrenheit</p>");
}
w.println("<a href='index.html'>Back</a>");
} catch (NamingException e) {
w.println(e.getMessage());
} catch (NumberFormatException e) {
w.println("An incorrect temperature was specified");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>OSGi EJB Temperature Converter</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function validate(form) {
var temperature = new Number(form.temperature.value);
if (isNaN(temperature)) {
alert("Please enter a valid number.");
return false;
}
return true;
}
</script>
</head>
<body>
<form action="ConverterServlet" method="post" onsubmit="return validate(this);">
Enter a temperature value:
<br/>
<input type="text" id="temperature" name="temperature"/>
<br/>
<br/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
此 HTML 包含用于将 temperature 参数值提交至 ConverterServlet 的表单。在将其发送至 Servlet 之前,validate() 函数会验证要提交的值以确保输入的是数字值。