zoukankan      html  css  js  c++  java
  • eclipse+tomcat+httpServlet初学

    1.在eclipse中配置tomcat,参考上一边文章,建立的web项目tomcatWeb目录结构


    2.添加Servlet处理类


    3. 实现doGet()与doPost()方法:

    <span style="font-size:18px;">public class HttpServletDemo extends HttpServlet {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 4230239235404655360L;
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws IOException {
    		response.setContentType("text/html;charset=GB2312");
    		PrintWriter printWriter = response.getWriter();
    		// printWriter.print("hello, china");
    		String title = "HelloServlet";
    		String heading1 = "HelloServlet的doGet方法的输出:";
    		printWriter = response.getWriter();
    		printWriter.print("<HTML><HEAD><TITLE>" + title + "</TITLE>");
    		printWriter.print("</HEAD><BODY>");
    		printWriter.print(heading1);
    		printWriter.println("<h1><p>:您好</h1>");
    		printWriter.print("</body></HTML>");
    		printWriter.write("");
    		printWriter.flush();
    		printWriter.close();
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws IOException {
    		doGet(request, response);
    	}
    }</span>
    4. 在web.xml中配置servlet

    <span style="font-size:18px;"> <servlet>
        <servlet-name>TomcatWeb</servlet-name>
        <servlet-class>tomcat.servlet.HttpServletDemo</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>TomcatWeb</servlet-name>
        <url-pattern>/servlet/HttpServletDemo</url-pattern>
      </servlet-mapping></span>


    5. 用配置好的tomcat容器运行该web项目

    方式1:右击项目--run as - run on server - 选择配置好的tomcat容器

    方式2:右击运行


    6. 在浏览器上输入:http://localhost:8080/TomcatWeb/servlet/HttpServletDemo,即可看到



  • 相关阅读:
    hdu 5335 Walk Out (搜索)
    Hdu 5336 XYZ and Drops (bfs 模拟)
    Zznu 1913: yifan and matrix (多路归并)
    hdu 5316 Magician (线段树)
    Bzoj 2038: [2009国家集训队]小Z的袜子(hose)
    Poj 1741 Tree (树的分治)
    LightOJ 1027
    1067
    Closest Common Ancestors---poj1470(LCA+离线算法)
    1128
  • 原文地址:https://www.cnblogs.com/marcotan/p/4256919.html
Copyright © 2011-2022 走看看