使用MyEclipse编写Servlet

阅读更多
一 步骤
1、src->new->Servlet
2、重写doGet()或者doPost()方法
3、部署运行
 
二 代码
1、HelloServlet
package servlet;
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 HelloServlet extends HttpServlet {
    /**
     * Constructor of the object.
     */
    public HelloServlet() {
        super();
    }
    /**
     * Destruction of the servlet.
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    /**
     * The doGet method of the servlet.
     *
     * 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 {
        System.out.println("处理Get请求...");
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");
        out.println("  A Servlet");
        out.println("  ");
        out.println("Hello Servlet!
");
        out.println("  ");
        out.println("");
        out.flush();
        out.close();
    }
    /**
     * The doPost method of the servlet.
     *
     * 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 {
        System.out.println("处理Post请求...");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");
        out.println("  A Servlet");
        out.println("  ");
        out.println("Hello Servlet!
");
        out.println("  ");
        out.println("");
        out.flush();
        out.close();
    }
    /**
     * Initialization of the servlet.
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
        // Put your code here
    }
}
 
2、index.jsp
<%@  page  language = "java"  import = "java.util.*"  contentType = "text/html; charset=utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
DOCTYPE  HTML  PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
   < head >
     < base  href = " <%= basePath %> " >
   
     < title > My JSP 'index.jsp' starting page title >
         < meta  http-equiv = "pragma"  content = "no-cache" >
         < meta  http-equiv = "cache-control"  content = "no-cache" >
         < meta  http-equiv = "expires"  content = "0" >    
         < meta  http-equiv = "keywords"  content = "keyword1,keyword2,keyword3" >
         < meta  http-equiv = "description"  content = "This is my page" >
        
   head >
 
   < body >
      < h1 > 使用MyEclipse创建 Servlet 小例子 h1 >
     < hr >
     < a  href = "servlet/HelloServlet" > Get方式请求HelloServlet a >< br >
     < form  action = "servlet/HelloServlet"  method = "post" >
       < input  type = "submit"  value = "Post方式请求HelloServlet" />
     form >
   body >
html >
 
三 运行结果

使用MyEclipse编写Servlet_第1张图片
 
  • 使用MyEclipse编写Servlet_第2张图片
  • 大小: 25.6 KB
  • 查看图片附件

你可能感兴趣的:(Eclipse,Servlet)