在 eclipse 中我创建了一个名为 Hello 的动态 Web 项目,然后创建了一个名为 Good 的 serverlet,编译运行,完全正常。代码如下:
package Hello;
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;
/**
* Servlet implementation class Good
*/
@WebServlet("/Good")
public class Good extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Good() {
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().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
/**
* @see HttpServlet#doPut(HttpServletRequest, HttpServletResponse)
*/
protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doDelete(HttpServletRequest, HttpServletResponse)
*/
protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
我是在Win7环境下运行的,在路径
C:\Users\Administrator\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Hello
找到 eclipse 临时部署的文件,把 Hello 文件夹复制到 centOS 的 /var/lib/tomcat/webapps 路径下即可。
输入 http://101.200.56.209:8080/Hello/Good 即可看到结果。
OK,到此为止,我已经知道在 centOS 服务器下用 Java 开发 web 项目的套路了。我的目标是开发一个REST API 形式的后台服务系统,现在已经具备最近本的知识了,再了解一下 MySQL 的用法,就可以动手开始干活了。