J2EE开发环境搭建
JDK安装
环境变量设置
Tomcat安装
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 "
安装验证path += " %java_home%\bin;%java_home%\jre\bin "
classpath = " .;%java_home%\lib\dt.jar;%java_home%\lib\tools.jar "
java
-
version
程序验证
编辑文件HelloWorld
.
java
java HelloWorld
public
class
HelloWorld{
public static void main(String args[]) {
System.out.println( " Hello World! " );
}
}
javac HelloWorld.javapublic static void main(String args[]) {
System.out.println( " Hello World! " );
}
}
java HelloWorld
解压缩
环境变量
Eclipse安装
环境变量
CATALINA_HOME
=
"
c:\tomcat
"
CATALINA_BASE = " c:\tomcat "
TOMCAT_HOME = " c:\tomcat "
classpath+=" %CATALINA_HOME%\lib\servlet-api.jar"
安装运行CATALINA_BASE = " c:\tomcat "
TOMCAT_HOME = " c:\tomcat "
classpath+=" %CATALINA_HOME%\lib\servlet-api.jar"
c
:\
tomcat
\
bin
\
下添加tomcatmonitor快捷方式
运行tomcatmonitor.bat启动服务
安装验证
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
http://localhost:8080/myapp/index.jsp
在 下建目录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<! 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 >
<
html
>
< body >
< center > Now time is: <% = new java.util.Date() %> </ center >
</ body >
</ html >
重启Tomcat< body >
< center > Now time is: <% = new java.util.Date() %> </ center >
</ body >
</ html >
http://localhost:8080/myapp/index.jsp
解压c
:\
eclipse
配置
建立自己的Servlet
配置
启动eclipse
进入window -> perference
选择java -> Installed JRES
选择正确的jre环境
点击编辑 ,
Add External JARS 添加c :\ tomcat \ lib \ 下的servlet . jar , el - api . jar , jasper . jar 和 jsp - api . jar四个文件
进入window -> perference
选择java -> Installed JRES
选择正确的jre环境
点击编辑 ,
Add External JARS 添加c :\ tomcat \ lib \ 下的servlet . jar , el - api . jar , jasper . jar 和 jsp - api . jar四个文件
在c
:\
tomcat
\
webapps
\
myapp
\
WEB
-
INF
\
classes
\
test 下 建HelloWorld
.
java
打开C:\Tomcat\webapps\myapp\WEB-INF\web.xml
在<web-app></web-app>添加下面这段程序:
http://localhost:8080/myapp/HelloWorld
建立自己java Bean
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.javaimport 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> " );
}
}
打开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< 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 >
http://localhost:8080/myapp/HelloWorld
在c:\tomcat\webapps\myapp\WEB-INF\classes\test\下建立TestBean.java
在c:\tomcat\webapps\myapp\下新建一个新的jsp文件testBean.jsp
http://localhost:8080/myapp/testBean.jsp
虚拟目录
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.javapublic 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;
}
}
在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< 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 >
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"
/>