Tomcat
先下载apache-tomcat-9.0.0.M26
下载完后其中有几个文件夹
Tomcat文件夹
bin: 可执行文件(打开 关闭服务器的文件)
conf: 服务器的配置信息
lib: 服务器使用的jar包
logs: 服务器日志
temp: 产生的临时文件
webapps: 默认的存放工程(应用程序)的文件夹
work: 服务器自己的工作空间
wtpwebapps: 跟Eclipse关联后产生的存放工程的文件夹
创建Servlet的几种方式
创建Servlet的方式一
从控制台启动Tomcat服务器命令
先去往Tomcat服务器下的 bin文件夹
cd (apache-tomcat-9.0.0.M26文件夹的目录)
获取文件夹权限(读写)
sudo chmod 775*.sh
执行打开服务器
sudo sh ./startup.sh
执行关闭服务器
sudo sh./shutdown.sh
访问服务器网址(url)
http:
协议:
Servlet 小服务程序
是个Java类 是个接口
注意:在浏览器上访问的每一个页面都是Servlet
每个Servlet都需要配置一个网址 在web.xml中配置
完成后在Eclipse上 实现Servlet接口
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;
}
}
用户如何从浏览器访问到Servlet类?
1.用户通过浏览器,使用网址访问服务器(Tomcat)
2.服务器通过网址可以找到对应项目的web.xml文件
3.通过网址找到对应Servletname
4.通过Servletname找到对应的Servlet类
5.创建Servlet对象
6.执行生命周期几个方法
实例化(无参构造方法) -> init初始化方法 -> service服务方法 -> 销毁destroy方法
配置web.xml文件
<?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-servlet-01</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>/demo02</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo03</servlet-name>
<servlet-class>com.lanou3g.Demo03</servlet-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>demo03</servlet-name>
<url-pattern>/demo03</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo04</servlet-name>
<servlet-class>com.lanou3g.Demo04</servlet-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>demo03</servlet-name>
<url-pattern>/demo04</url-pattern>
</servlet-mapping>
</web-app>
创建Servlet方式二
public class Demo02 extends GenericServlet{
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("我是demo02");
}
}
创建Servlet方式三
public class Demo03 extends HttpServlet {
private ServletConfig config;
@Override public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String value = this.config.getInitParameter("encoding");
System.out.println(value);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
创建ServletConfig方式一
public class Demo03 extends HttpServlet {
private ServletConfig config;
@Override public void init(ServletConfig config) throws ServletException {
super.init(config);
this.config = config;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String value = this.config.getInitParameter("encoding");
System.out.println(value);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
获取ServletConfig方式二
public class Demo04 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String parameter = this.getServletConfig().getInitParameter("encoding");
System.out.println(parameter);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
域对象
ServletContext
域对象(在一定范围内 存储信息的对象)
ServletContext范围: 整个程序中都可以访问到 并且只有一个 单例对象
每个Servlet都可以访问到整个域对象
如何获取ServletContext对象
方法1:从ServletConfig对象中获取
方法2:从父类中直接获取
注意: 所有的域对象 都有设置/获取/删除的方法
方式一
public class Demo05 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext application = this.getServletConfig().getServletContext();
application.setAttribute("username", "xiaoming");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
方式二
public class Demo06 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object object = this.getServletContext().getAttribute("username");
System.out.println(object);
}
}