|
之前导师希望用Android手机或者用Android操作系统的ARM作服务器,来达到数据的预处理或实现小区域的智能监控,当时由于笔者缺乏这方面的知识就直接否定啦。有一天没事在网上查相关资料的时候发现有这样的开源项目(i-jetty)。后来笔者查阅很多学术论文都有这方面的知识,但是他们只注重自己的研究成果,最多也就是放几张软件运行的展示图(有没有运行成功谁也不知道);就是没有怎么配置i-jetty、怎么在i-jetty下面运行WEB项目(可能对他们来说这些都是小儿科的东西吧)。今天笔者就详细的讲解怎么在i-jetty下面运行servlet。
第一步:首先要下载i-jetty源码(或者直接下载i-jetty APP安装也可以),但是笔者建议还是下载源码最好,这样我们可以查看源码提高自己思维能力和编程能力;还有一个好处就是我们可以做扩张。例如:我们可以添加自动连接蓝牙模块,或者tcp/ip网络设备来收数据采集终端的数据。这样我们可以把Android操作系统的ARM做成“黑盒子”。通过浏览器就可以看到我们监控的数据。
第二步:要保证i-jetty安装成功后我们就可以开发servlet程序啦,就打印一句话吧。
package comm;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PrintInfoServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public PrintInfoServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
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.print("有关I-Jetty的测试程序");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
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.print("有关I-Jetty的测试程序");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
第三步:保存并编译上面的源码
第四步:找到Android SDK下面的dx.bat批处理文件(如果对SDK进行过环境变量的配置可以直接在DOS页面中使用dx.bat命令),笔者的位置为:F:\办工具软件\adt-bundle-windows-x86-20130917.467161976\adt-bundle-windows-x86-20130917\sdk\build-tools\android-4.3;然后执行下面的命令。
dx.bat --dex --output=F:\JAVAWEB\PrintInfo\WebRoot\classes.zip F:\JAVAWEB\PrintInfo\WebRoot\WEB-INF\classes F:\JAVAWEB\PrintInfo\WebRoot\WEB-INF\lib
注意:F:\JAVAWEB\PrintInfo是笔者项目的位置;当在F:\JAVAWEB\PrintInfo\WebRoot目录下面出现classes.zip文件就说明成功啦!
第五步:把生成的classes.zip放在WEB-INF目录下面的lib目录下,把原先的classes.zip文件删除。
第六步:打包程序。在MyEclipse中找到我们的项目刷新后可以在lib下看到classes.zip文件,这时右击项目名选择Export --> Java EE --> WAR file 点击保存位置和完成就可以啦,然后我们把war文件拷贝到手机或者ARM下面i-jetty安装目录下的webapps下即可,重启程序即可访问了。
第七步:测试
笔者在火狐浏览器输入http://172.19.183.5:8080/PrintInfo/PrintInfoServlet就看到下面的运行界面就说明成功啦。
小结:如果明白原理,利用源码可以做出很多东西,网上的很多学术论文就是扩展这个技术(例如:i-jetty在智能家居的研究,利用i-jetty对生理数据的采集都是类似的)。最后笔者文笔技术有限,有错误的地方请大神不吝赐教!谢谢! |
|