J2EE开发环境搭建

J2EE开发环境搭建
JDK安装
环境变量设置
java_home = " c:\java\jdk1.6.0_16 "
path
+= " %java_home%\bin;%java_home%\jre\bin "
classpath
= " .;%java_home%\lib\dt.jar;%java_home%\lib\tools.jar "
安装验证
java  - version
程序验证
编辑文件HelloWorld . java
public   class  HelloWorld{
    
public   static   void  main(String args[]) {
        System.out.println(
" Hello World! " );
    }
}
javac HelloWorld.java
java HelloWorld
Tomcat安装
解压缩
环境变量
CATALINA_HOME = " c:\tomcat "
CATALINA_BASE
= " c:\tomcat "
TOMCAT_HOME
= " c:\tomcat "
classpath+=" %CATALINA_HOME%\lib\servlet-api.jar"
安装运行
c :\ tomcat \ bin \ 下添加tomcatmonitor快捷方式
tomcat6w . exe  // MS // Tomcat6
运行service install安装服务
运行tomcatmonitor.bat启动服务
安装验证
http :// localhost : 8080
jsp验证
在c :\ tomcat \ webapps \ 下建目录myapp
下建目录WEB - INF
在c
:\ tomcat \ webapps \ myapp \ WEB - INF \ 下建文件web . xml
<? xml version="1.0"  encoding="ISO-8859-1" ?>
<! DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app >
    
< display-name > My Web Application </ display-name >
    
< description > A application for test. </ description >
</ web-app >  
在c:\tomcat\webapps\myapp\下建文件index.jsp
< html >
    
< body >
        
< center > Now time is:  <% = new  java.util.Date() %> </ center >
    
</ body >
</ html >
重启Tomcat
http://localhost:8080/myapp/index.jsp
Eclipse安装
解压c :\ eclipse
配置
启动eclipse
进入window
-> perference
选择java
-> Installed JRES
选择正确的jre环境
点击编辑
,
Add External JARS 添加c
:\ tomcat \ lib \ 下的servlet . jar , el - api . jar , jasper . jar 和 jsp - api . jar四个文件

建立自己的Servlet
在c :\ tomcat \ webapps \ myapp \ WEB - INF \ classes \ test 下 建HelloWorld . java
package  test;

import  java.io. * ;
import  javax.servlet. * ;
import  javax.servlet.http. * ;
public   class  HelloWorld  extends  HttpServlet
{
   
public   void  doGet(HttpServletRequest request,HttpServletResponse response) throws  ServletException,IOException
    {
        response.setContentType(
" text/html " );
        PrintWriter out 
=  response.getWriter();
        out.println(
" <html><head><title> " );
        out.println(
" This is my first Servlet " );
        out.println(
" </title></head><body> " );
        out.println(
" <h1>Hello,World!</h1> " );
        out.println(
" </body></html> " );
    }
}
javac HelloWorld.java
打开C:\Tomcat\webapps\myapp\WEB-INF\web.xml
在<web-app></web-app>添加下面这段程序:
< servlet >
    
< servlet-name > HelloWorld </ servlet-name >
    
< servlet-class > test.HelloWorld </ servlet-class >
</ servlet >
< servlet-mapping >
    
< servlet-name > HelloWorld </ servlet-name >
    
< url-pattern > /HelloWorld </ url-pattern >
</ servlet-mapping >
重启tomcat
http://localhost:8080/myapp/HelloWorld
建立自己java Bean
在c:\tomcat\webapps\myapp\WEB-INF\classes\test\下建立TestBean.java
package  test;
public   class  TestBean
{
    
private  String name  = null ;
    
public  TestBean(String nameInit){
        
this .name  =  nameInit;
    }
    
public   void  setName(String newName){
        
this .name = newName;
    }
    
public  String getName(){
        
return   this .name;
    }
}
javac TestBean.java
在c:\tomcat\webapps\myapp\下新建一个新的jsp文件testBean.jsp
<% @ page import = " test.TestBean "   %>
< html >
    
< head >
    
< title > Test Bean </ title >
    
</ head >
    
< body >
        
< center >
<%
    TestBean testBean 
=   new  TestBean( " Http://yexin218.cublog.cn " );
 
%>
Java Bean Test:
    The author's blog address is 
<% = testBean.getName() %>
        
</ center >
    
</ body >
</ html >
重启tomcat
http://localhost:8080/myapp/testBean.jsp
虚拟目录
打开c :\ tomcat \ conf \ server . xml 文件 , 在  < Host >  和  </ Host >  之间加入
< Context  path ="/myapp"  docBase ="c:\myapp"  debug ="0"  reloadable ="true"  crossContext ="true"   />

你可能感兴趣的:(J2EE开发环境搭建)