zoukankan      html  css  js  c++  java
  • Mac OS中Java Servlet与Http通信

    Java中一个Servlet其实就是一个类,用来扩展服务器的性能,服务器上驻留着可以通过“请求-响应”编程模型来访问的应用程序。Servlet可以对任何类型的请求产生响应,但通常只用来扩展Web服务器的应用程序。Java Servlet技术为这些应用定义了一个特定于HTTP的servlet类,基类是HttpServlet。Servlet本身是有生命周期的,有兴趣的可以搜索其他文章,实现Java Servlet与Http通信,本文中需要下JDK(http://www.oracle.com/technetwork/java/javase/downloads/index.html)

    ,Tomcat(具体过程可以参考上篇文章Mac下Apache Tomcat安装配置),Ecliplse For Java EE(http://www.eclipse.org/downloads/)

    1.Eclipse下载

    2.启动Eclipse,新建Java Web项目;

    3.项目基本设置,设置运行的Tomcat版本,设置Servlet版本;

    如果没有特别的需求直接Finish即可,最终的目录如下,Java Resources中的Src目录设置存放类文件,WebContent可以存放JSP和Html文件,注意Web-INF下没有Web.xml,这是新版本的特性;

    4.如果第三步没有直接Finish而是Next应该是后面的两步是这样的:

    可以勾选是否需要web.xml,本文中不需要,根据自己的情况选择;

    5.WebContent下新建Html或者JSP文件:

    Html中代码如下:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Html测试</title>
    </head>
    <body>
    博客园-FlyElephant(http://www.cnblogs.com/xiaofeixiang)
    </body>
    </html>
    

    运行地址http://localhost:8080/WebDemo/FirstDemo.html

    JSP页面运行类似,就没有演示了,接下来介绍一下Servlet;

    6.Servlet文件

    设置一下直接Finish,不用设置包名;

    FirstServlet.java中的代码如下:

    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 FirstServlet
     */
    @WebServlet("/FirstServlet")
    public class FirstServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public FirstServlet() {
            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
    	}
    
    	/**
    	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    	}
    
    }
    

    7.doGet方法中加入一段输出代码,浏览会看到应有的效果:

    	    response.setContentType("text/html;charset=utf-8");
    	        PrintWriter out = response.getWriter();
    	        out.println("<!DOCTYPE HTML>");
    	        out.println("<Html>");
    	        out.println("<Head><title>FirstServlet</title></Head>");
    	        out.println("<Body>");
    	        out.print("FirstServlet----");
    	        out.println("FlyElephant(http://www.cnblogs.com/xiaofeixiang)");
    	        out.println("  </Body>");
    	        out.println("</Html>");
    	        out.flush();
    	        out.close();
    

      效果如下:

    8.第6步如果不直接Finish,Next之后的两个选择选项如下:

    选择实现的方法:

    Mac下简单的Java Web开发过程算是完成了,没有太多说教的东西,能满足基本的使用,如果有问题,欢迎私信我或者评论区交流~

    参考资料:Servlet的3.0特性(http://www.ibm.com/developerworks/cn/java/j-lo-servlet30/#major3)

  • 相关阅读:
    DTOJ #3328. 开箱子(unboxing)
    dtoi4649 光明
    dtoi4539「TJOI / HEOI2016」序列
    dtoi3031 交错和查询 (sum)
    dtoi4375「BJOI2019」删数
    dtoi4266 exchange
    dtoi4680 红黑兔
    dtoi1363 楼房重建 (rebuild)
    dtoi1927 [ONTAK2010]Peaks加强版
    dtoi4538 「TJOI / HEOI2016」排序
  • 原文地址:https://www.cnblogs.com/xiaofeixiang/p/4304450.html
Copyright © 2011-2022 走看看