JSP/Servlet环境配置

  1. 下载安装JDK并配置环境变量
    这里我下载安装的是jdk-7u51-windows-i586,如果你没有请到Oracle官网下载;
    【我的电脑】-【高级系统设置】-【环境变量】-系统变量中的Path添加Java bin目录;
    命令窗口测试配置环境变量正确与否。
  2. 下载服务器
    我学习使用的是Web容器apache-tomcat-7.0.50-windows-x86,如果没有请到Apache下载。
  3. 配置服务器
    确定SDK安装目录,即JAVA_HOME环境变量,这一步应该在第一步中已经配置;
    指定端口,默认是8080,如果你不喜欢用它,或者该端口被占用,应该指定为其它;
    执行startup.bat启动Tomcat,在浏览器中测试。
  4. 建立开发环境
    创建开发目录,我这里的为D:\project\MyServlet\src
    设置CLASSPATH,主要是为了告诉编译器Servlet类的位置,值为“.;D:\project\MyServlet\src;D:\tools\apache-tomcat-7.0.50\lib\servlet-api.jar”,配置如下:

    创建快捷方式,方便测试时快速启动和关闭Tomcat。
  5. 测试系统环境变量
    检查服务器的基本配置
    将helloworld.html和helloworld.jsp放置到<CATALINA_HOME>/webapps/ROOT下
    helloworld.html
    1. <span style="font-size: 18px;"><html>  
    2. <head>  
    3. <title>test</title>  
    4. </head>  
    5. <body>  
    6. Hello world  
    7. </body>  
    8. </html></span>  
    <html>
    <head>
    <title>test</title>
    </head>
    <body>
    Hello world
    </body>
    </html>
    helloworld.jsp
    1. <span style="font-size: 18px;"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
    2. <html>  
    3. <head>  
    4. <title>jsp test</title>  
    5. </head>  
    6. <body bgcolor="#fdf5e6">  
    7. <h1>Hello JSP.</h1>  
    8. Time:<%= new java.util.Date() %>  
    9. </body>  
    10. </html></span>  
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>jsp test</title>
    </head>
    <body bgcolor="#fdf5e6">
    <h1>Hello JSP.</h1>
    Time:<%= new java.util.Date() %>
    </body>
    </html>
    浏览器访问:


    测试不使用包的Servlet
    在命令窗口下编码HelloServlet.java,将编译后的class文件放到<CATALINA_HOME>/webapps/ROOT/WEB-INF/class下
    HelloServlet.java
    1. <span style="font-size: 18px;">import java.io.*;  
    2. import javax.servlet.*;  
    3. import javax.servlet.http.*;  
    4.   
    5. /** Simple servlet used to test server.*/  
    6. public class HelloServlet extends HttpServlet{  
    7.     public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{  
    8.         response.setContentType("text/html");  
    9.         PrintWriter out=response.getWriter();  
    10.         String docType="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";  
    11.         out.println(docType+  
    12.                     "<html>\n"+  
    13.                     "<head><title>Hello</title></head>\n"+  
    14.                     "<body bgcolor=\"#fdf5e6\">\n"+  
    15.                     "<h1>Hello world</h1>\n"+  
    16.                     "</body></html>");  
    17.     }  
    18.     public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{  
    19.         this.doGet(request,response);  
    20.     }  
    21. }</span>  
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /** Simple servlet used to test server.*/
    public class HelloServlet extends HttpServlet{
    	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    		response.setContentType("text/html");
    		PrintWriter out=response.getWriter();
    		String docType="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";
    		out.println(docType+
    					"<html>\n"+
    					"<head><title>Hello</title></head>\n"+
    					"<body bgcolor=\"#fdf5e6\">\n"+
    					"<h1>Hello world</h1>\n"+
    					"</body></html>");
    	}
    	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    		this.doGet(request,response);
    	}
    }
    编译HelloServlet.java

    因为我们使用的是tomcat7,无法使用其调用器invoker,所以要手动添加映射,打开<CATALINA_HOME>/webapps/ROOT/WEB-INF下的web.xml,编辑如下:

    1. <span style="font-size: 18px;"><?xml version="1.0" encoding="ISO-8859-1"?>  
    2. <!--  
    3.  Licensed to the Apache Software Foundation (ASF) under one or more  
    4.   contributor license agreements.  See the NOTICE file distributed with  
    5.   this work for additional information regarding copyright ownership.  
    6.   The ASF licenses this file to You under the Apache License, Version 2.0  
    7.   (the "License"); you may not use this file except in compliance with  
    8.   the License.  You may obtain a copy of the License at  
    9.   
    10.       http://www.apache.org/licenses/LICENSE-2.0  
    11.   
    12.   Unless required by applicable law or agreed to in writing, software  
    13.   distributed under the License is distributed on an "AS IS" BASIS,  
    14.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
    15.   See the License for the specific language governing permissions and  
    16.   limitations under the License.  
    17. -->  
    18.   
    19. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
    20.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    21.   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
    22.                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    23.   version="3.0"  
    24.   metadata-complete="true">  
    25.   
    26.   <display-name>Welcome to Tomcat</display-name>  
    27.   <description>  
    28.      Welcome to Tomcat  
    29.   </description>  
    30.   <servlet>  
    31.     <servlet-name>hello</servlet-name>  
    32.     <servlet-class>HelloServlet</servlet-class>  
    33.   </servlet>  
    34.   <servlet-mapping>  
    35.     <servlet-name>hello</servlet-name>  
    36.     <url-pattern>/HelloServlet</url-pattern>  
    37.   </servlet-mapping>  
    38.     
    39. </web-app>  
    40. </span>  
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--
     Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements.  See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License.  You may obtain a copy of the License at
    
          http://www.apache.org/licenses/LICENSE-2.0
    
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
    -->
    
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      version="3.0"
      metadata-complete="true">
    
      <display-name>Welcome to Tomcat</display-name>
      <description>
         Welcome to Tomcat
      </description>
      <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/HelloServlet</url-pattern>
      </servlet-mapping>
      
    </web-app>
    
    启动Tomcat,在浏览器访问,如下,则说明正确

    测试使用的Servlet
    带包的HelloServlet2.java如下:

    1. <span style="font-size: 18px;">package coreservlets;  
    2.   
    3. import java.io.*;  
    4. import javax.servlet.*;  
    5. import javax.servlet.http.*;  
    6.   
    7. /** Simple servlet used to test server.*/  
    8. public class HelloServlet2 extends HttpServlet{  
    9.     public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{  
    10.         response.setContentType("text/html");  
    11.         PrintWriter out=response.getWriter();  
    12.         String docType="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";  
    13.         out.println(docType+  
    14.                     "<html>\n"+  
    15.                     "<head><title>Hello</title></head>\n"+  
    16.                     "<body bgcolor=\"#fdf5e6\">\n"+  
    17.                     "<h1>Hello world (2)</h1>\n"+  
    18.                     "</body></html>");  
    19.     }  
    20.     public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{  
    21.         this.doGet(request,response);  
    22.     }  
    23. }</span>  
    package coreservlets;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /** Simple servlet used to test server.*/
    public class HelloServlet2 extends HttpServlet{
    	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    		response.setContentType("text/html");
    		PrintWriter out=response.getWriter();
    		String docType="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";
    		out.println(docType+
    					"<html>\n"+
    					"<head><title>Hello</title></head>\n"+
    					"<body bgcolor=\"#fdf5e6\">\n"+
    					"<h1>Hello world (2)</h1>\n"+
    					"</body></html>");
    	}
    	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    		this.doGet(request,response);
    	}
    }

    像上面一样,编译之后就class文件拷贝到<CATALINA_HOME>/webapps/ROOT/WEB-INF/class/coreservlets包下,并在web.xml文件中增加如下内容

    1. <span style="font-size: 18px;"><servlet>  
    2.     <servlet-name>hello2</servlet-name>  
    3.     <servlet-class>coreservlets.HelloServlet2</servlet-class>  
    4.   </servlet>  
    5.   <servlet-mapping>  
    6.     <servlet-name>hello2</servlet-name>  
    7.     <url-pattern>/HelloServlet2</url-pattern>  
    8.   </servlet-mapping></span>  
    <servlet>
        <servlet-name>hello2</servlet-name>
        <servlet-class>coreservlets.HelloServlet2</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>hello2</servlet-name>
        <url-pattern>/HelloServlet2</url-pattern>
      </servlet-mapping>

    在这里要注意,web.xml中,引用HelloServlet2时,包与class文件之间的分隔符是圆点“.";classes下的包名要与package coreservlets一致,否则,你懂的。
    重启tomcat,在浏览器测试,如下


    测试使用包和实用工具类的Servlet
    HelloServlet3.java

    1. <span style="font-size: 18px;">package coreservlets;  
    2.   
    3. import java.io.*;  
    4. import javax.servlet.*;  
    5. import javax.servlet.http.*;  
    6.   
    7. /** Simple servlet for testing the use of packages 
    8. *and utilities from the same package*/  
    9. public class HelloServlet3 extends HttpServlet{  
    10.     public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{  
    11.         response.setContentType("text/html");  
    12.         PrintWriter out=response.getWriter();  
    13.         String title="Hello (3)";  
    14.         out.println(ServletUtilities.headWithTitle(title)+  
    15.                     "<body bgcolor=\"#fdf5e6\">\n"+  
    16.                     "<h1>"+title+"</h1>\n"+  
    17.                     "</body></html>");  
    18.     }  
    19.     public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{  
    20.         this.doGet(request,response);  
    21.     }  
    22. }</span>  
    package coreservlets;
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /** Simple servlet for testing the use of packages
    *and utilities from the same package*/
    public class HelloServlet3 extends HttpServlet{
    	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    		response.setContentType("text/html");
    		PrintWriter out=response.getWriter();
    		String title="Hello (3)";
    		out.println(ServletUtilities.headWithTitle(title)+
    					"<body bgcolor=\"#fdf5e6\">\n"+
    					"<h1>"+title+"</h1>\n"+
    					"</body></html>");
    	}
    	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
    		this.doGet(request,response);
    	}
    }

    ServletUtilities.java

    1. <span style="font-size: 18px;">package coreservlets;  
    2.   
    3. import javax.servlet.*;  
    4. import javax.servlet.http.*;  
    5.   
    6. /**Some simple timesavers.Note that most are static methods.*/  
    7. public class ServletUtilities {  
    8.     public static final String DOCTYPE="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";  
    9.     public static String headWithTitle(String title){  
    10.         return(DOCTYPE+"\n"+"<html>\n"+"<head><title>"+title+"</title></head>\n");  
    11.     }  
    12. }  
    13.     </span>  
    package coreservlets;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /**Some simple timesavers.Note that most are static methods.*/
    public class ServletUtilities {
    	public static final String DOCTYPE="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "+"Transitional//EN\">\n";
    	public static String headWithTitle(String title){
    		return(DOCTYPE+"\n"+"<html>\n"+"<head><title>"+title+"</title></head>\n");
    	}
    }
    	

    剩下的就是编译,拷贝,修改web.xml,重启Tomcat,最后在浏览器中测试如下:



小结:是不是很麻烦呢?每次我都要调用javac filepath进行编译,编译之后再copy到tomcat下,太机械重复了,有没有简单的呢?

当然有了,这是下面要学习的内容,且看我们下篇学习吧!

 

 

 

http://blog.csdn.net/hankaibo/article/details/19705205

posted @ 2015-08-27 11:11  fleam  阅读(1029)  评论(0编辑  收藏  举报