搭建开发环境:
JDK:1.7 64位 JAVA_HOME=E:\jdk1.7
Tomcat 7 eclipse 环境下开发
首先 说明下这个是本人的学习笔记
原理笔记:
1 tomcat服务器相关
Tomcat 中最终要的是server.xml文件,复制粘贴到eclipse下的一个xml文件,点击design可以看见她的树状结构;
tomcat服务器启动的时候实例化,catalina service对象,和各个协议的Connector对象,connector对象,接受http或其他协议的请求对象,这里暂时用httpRequest对象来讲。
connector 对象接受客户端/浏览器的请求,交给servlet引擎,然后匹配主机host(一台物理PC可以映射成多个Host,C:\Windows\System32\drivers\etc 该目录下,打开hosts,可以映射多个虚拟主机,而且这个地方tomcat服务器启动时,会实例化,engine,connector,host等对象)。然后匹配到某个context,一个context就是某一个应用。
途中碰到的问题:
tomcat 启动秒退 ,后来百度后在startup.bat文件和shutdown.bat的首位,加上
1 可能原因1
set JAVA_HOME=E:\jdk1.7
SET TOMCAT_HOME=D:\Program Files\study_programs\tomcat7.1\apache-tomcat-7.0.62
2 可能原因2 server.xml 配置context时,少了结尾</Context>
报错,可以看,logs文件下的日志文件
以上是tomcat单独启动
3 在server Location 下默认deploy pah就是发布路径 默认会生成wtpwebapps,这里重新定位。还有 use tomcat installation. 打钩
之后就是 new web Dynamic project . 工程名是Test2
以下是代码:
package org.lxh.servletdemo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class ServletHello implements Servlet{
public void destroy() {
// TODO Auto-generated method stub
}
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("gbk");
PrintWriter pw = new PrintWriter(response.getOutputStream());
pw.println("操你大爷!!");
pw.close();
System.out.println("hello caonima!!");
}
}
以下是web.xml下注册servlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.lxh.servletdemo.ServletHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>ServletHello</display-name>
<servlet-name>ServletHello</servlet-name>
<servlet-class>org.lxh.servletdemo.ServletHello</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletHello</servlet-name>
<url-pattern>/ServletHello</url-pattern>
</servlet-mapping>
</web-app>
然后启动tomcat服务器
过程如下:
INFO: Starting service Catalina
INFO: Starting Servlet Engine: Apache Tomcat/7.0.62
十一月 29, 2015 9:33:28 下午 org.apache.catalina.startup.HostConfig deployDescriptor
这里如果发布成功的话,会有一下这句话
INFO: Deploying configuration descriptor D:\Program Files\study_programs\tomcat7.1\apache-tomcat-7.0.62\conf\Catalina\localhost\Test2.xml
INFO: ContextListener: contextInitialized() 初始化 context监听
INFO: SessionListener: contextInitialized() 初始化 回话监听
浏览器敲入http://localhost:8080/Test2/hello,根据xml配置,资源定位到web-infD:\Program Files\study_programs\tomcat7.1\apache-tomcat-7.0.62\webapps\Test2\WEB-INF\classes\org\lxh\servletdemo ServletHello.class,这个class文件,装载这个类,当监听到httpRequest请求时,engine封装http请求信息,给这个class下的,service方法,然后执行这个方法。