Tomcat
1.tomcat文件夹下子文件的作用
bin:可执行文件(打开关闭服务器的文件)
conf:服务器的配置信息
logs:服务器日志
temp:产生临时文件
work:服务器自己的工作空间
webapps:默认的存放工程(应用程序)文件夹
wtpwebapps: 跟eclipse关联后产生的存放工程的文件夹
lib:服务器使用的jar包
从控制台启动Tomcat服务器的命令:
先去往Tomcat服务器下面的 bin文件夹
获取文件夹权限(读写)
sudo chmod 755 *.sh
执行打开服务器
sudo sh ./startup.sh
执行关闭服务器
sudo sh ./shutdown.sh
访问服务器网址(url)
http://localhost:8080
协议://本地地址:端口号/项目名(应用程序)/访问的资源
http://localhost:8080/sh-web-servlet01/1.html
Servlet 小服务程序
是个java类 是个接口
注意:在浏览器上访问的每一个网页都是一个servlet
每个servlet都需要配置一个网址 在web.xml中配置
用户如何从浏览器访问到Servlet类 简单谈一下
1.用户用网址访问服务器(tomcat)
2.服务器通过网址可以找到 对应项目的 web.xml文件
3.通过网址找到对应的servletname
4.通过servletname找到对应的 servlet类
5.创建servlet对象
6.执行生命周期几个方法
实例化-->初始化-->service服务-->销毁
创建Servlet的方法
(1.创建方式1:
public class Demo01 implements Servlet {
public Demo01() {
System.out.println("这时实例化方法");
}
@Override
public void init(ServletConfig arg0) throws ServletException {
System.out.println("这是初始化方法init");
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
System.out.println("这是服务方法service");
}
@Override
public void destroy() {
System.out.println("这是销毁方法destroy");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
}
(2.创建方式二(适配器模式创建)
使用哪个方法就重写哪个方法 用不上的方法可以不重写
public class Demo02 extends GenericServlet{
@Override
public void service(ServletRequest req, ServletResponse res)
throw ServleException, IOException {
System.out.println("这是demo02");
}
}
(3.创建方式三(模板设计模式)
public class Demo03 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("这是demo03");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
获取ServletConfig对象的方法
方法一:
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
}
String value = this.config.getInitParameter("encoding");
System.out.println(value);
方法二:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String value = this.getServletConfig().getInitParameter("encoding");
System.out.println(value);
Servlet的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>sh-web-servlet01</display-name>
<welcome-file-list>
<welcome-file>1.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>demo01</servlet-name>
<servlet-class>com.lanou3g.Demo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo01</servlet-name>
<url-pattern>/demo01</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo02</servlet-name>
<servlet-class>com.lanou3g.Demo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo02</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>