javaweb中servlet创建实例

最近初学javaweb,下面将介绍如何创建一个简单的javaweb实例。

首先把eclipse以及相应的Java jdk和Tomcat服务器配置完成。然后在eclipse里面新建Dynamic Web Project项目件。


在建好的项目中的doget方法中添加如下的代码:
 
   
package king;
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 javax.swing.JOptionPane; /**
public class Firstking extends HttpServlet {
* Servlet implementation class Firstking */ @WebServlet("/Firstking") private static final long serialVersionUID = 1L;
public Firstking() {
private static final Object[] OK = null; /** * @see HttpServlet#HttpServlet() */ super();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
/**
// TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); response.getWriter().write("hello world!"); }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ // TODO Auto-generated method stub doGet(request, response);
}
}

这样运行该Java文件就能够看到熟悉的hello word。哈哈。




你可能感兴趣的:(javaweb)