带你快速上手Servlet

网友投稿 601 2023-01-22

带你快速上手Servlet

带你快速上手Servlet

一、Servlet与Tomcat的关系

(1)Tomcat是什么?

Tomcat其实是Web服务器和Servlet容器的结合体

(2)什么是Web服务器?

比如,我当前在杭州,你能否用自己的电脑访问我桌面上的一张图片?恐怕不行,我们太习惯通过URL访问的一个网站、-一部电影了。一个资源,如果没有URL映射,那么外界几乎很难访问,而Web服务器的作用说穿了就是:将某个主机上的资源映射为一个URL供外界访问

二、什么是Servlet

(1)什么是Servlet容器?

Servlet是运行在Web服务器或应用服务器上的程序。

Servlet容器,顾名思义里面存着Servlet对象,我们为什么能够通过Web服务器映射的URL访问资源?肯定需要写程序处理请求,主要3个过程:接受请求,处理请求,响应请求。

三、Servlet的类结构

通过继承HttpServlet实现Servlet接口

一般在实际项目开发中,都是使用继承HttpServlet类的方式去实现Servlet程序

(1)编写一个类去继承HttpServlet类

(2)根据业务需要重写doGet或doPost方法

(3)到web.xml中的配置servlet程序的访问地址

四、ServletConfig类

ServletConfig代表的是当前Servlet在web.xml中的配置信息

String getServletName(); ---获取当前Servlet在web.xml中配置的名字

ServletContext getServletContext();---获取当前Servlet指定名称的初始化参数的值

String getInitParameter(String var1);---获取当前Servlet所有初始化参数的名字组成的枚举

Enumeration getInitParameterNames();---获取代表当前web应用的ServletContext对象

(1)作用:

1、可以获取Servlet程序的别名Servlet-name的值

2、获取初始化参数init-param

3、获取ServletContext对象

@Override

public void init(ServletConfig servletConfig) throws ServletException {

// 1、可以获取Servlet程序的别名Servlet-name的值

System.out.println(servletConfig.getServletName());

// 2、获取初始化参数init-param

System.out.println(servletConfig.getInitParameter("username"));

// 3、获取ServletContext对象

System.out.println(servletConfig.getServletContext());

System.out.println("2、执行初始化方法");

}

五、ServletContext类

(1)什么是ServletContext?

1、ServletContext是一个接口,它表示Servlet上下文对象。

2、一个Web工程,只有一个ServletContext对象实例

3、ServletContext是一个域对象。

4、ServletContext是在web工程部署启动的时候创建,在web工程停止的时候销毁。

什么是域对象?

域对象,是可以像Map一样存取数据的对象,叫域对象。

这里的域指的是存取数据的操作范围,整个web工程。

存数据 取数据 删除数据

Map put() get() remove()

域对象 setAttribute() getAttribute() removeAttribute()

(2) ServletContext类的四个作用

1、获取web.xml中配置的上下文参数context-param

public class ContextServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//1、 获取web.xml中配置上下文参数context-param

ServletContext servletContext = getServletConfig().getServletContext();

String username = servletContext.getInitParameter("username");

System.out.println("context-param参数的username"+username);

}

}

在web.xml中

username

context

2、获取当前的工程路径,格式:/工程路径

3、获取工程部署后在服务器硬盘上的绝对路径

(3)ServletContext像map一样存取数据

public class ContextServlet1 extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//获取ServletContext对象

ServletContext context = getServletContext();

System.out.println("保存之前:Context1 获取key1的值是:"+context.getAttribute("key1"));

context.setAttribute("key1","value1");

System.out.println("Context1中获取域数据key1的值是:"+context.getAttribute("key1"));

}

}

保存之前:Context1 获取key1的值是:null

Context1中获取域数据key1的值是:value1

Context2中获取域数据key1的值是:value1

六、Servlet的生命周期

public class HelloServlet implements Servlet {

public HelloServlet() {

System.out.println("1、执行构造器方法");

}

@Override

public void init(ServletConfig servletConfig) throws ServletException {

System.out.println("2、执行初始化方法");

}

@Override

public ServletConfig getServletConfig() {

return null;

}

//service方法是专门用来处理请求和响应的

@Override

public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

System.out.println("3、hello servlet 被访问了");

}

@Override

public String getServletInfo() {

return null;

}

@Override

public void destroy() {

System.out.println(" 4、执行销毁方法");

}

}

执行的结果

1、执行构造器方法

2、执行初始化方法

3、hello servlet 被访问了

3、hello servlet 被访问了

3、hello servlet 被访问了

3、hello servlet 被访问了

3、hello servlet 被访问了

3、hello servlet 被访问了

3、hello servlet 被访问了

http://3、hello servlet 被访问了

3、hello servlet 被访问了

G:\softWareInstall\apache-tomcat-9.0.45\bin\catalina.bat stop

Using CATALINA_BASE:   "C:\Users\Administrator\AppData\Local\JetBrains\IntelliJIdea2020.1\tomcat\Unnamed_Servlet"

Using CATALINA_HOME:   "G:\softWareInstall\apache-tomcat-9.0.45"

Using CATALINA_TMPDIR: "G:\softWareInstall\apache-tomcat-9.0.45\temp"

Using JRE_HOME:        "C:\Program Files\java\jdk1.8.0_60"

Using CLASSPATH:       "G:\softWareInstall\apache-tomcat-9.0.45\bin\bootstrap.jar;G:\softWareInstall\apache-tomcat-9.0.45\bin\tomcat-juli.jar"

Using CATALINA_OPTS:   ""

03-May-2021 14:33:11.909 淇℃伅 [main] org.apache.catalina.core.StandardServer.await 閫氳繃鍏抽棴绔彛鎺ユ敹鍒版湁鏁堢殑鍏抽棴鍛戒护銆傛鍦ㄥ仠姝㈡湇鍔″櫒瀹炰緥銆�

03-May-2021 14:33:11.909 淇℃伅 [main] org.apache.coyote.AbstractProtocol.pause 鏆傚仠ProtocolHandler["http-nio-8080"]

03-May-2021 14:33:12.289 淇℃伅 [main] org.apache.catalina.core.StandardService.stopInternal 姝e湪鍋滄鏈嶅姟[Catalina]

 4、执行销毁方法

(1)执行Servlet构造器方法

(2) 执行init初始化方法

第一、二步,是在第一次访问的时候创建Servlet程序会调用

(3)执行 Service方法

第三步、每次访问都会调用

(4)执行destroy销毁方法

第四步:在web工程停止的时候调用

七、Get、Post

get、post请求都会走Service方法,那么怎么区分get、post请求

//service方法是专门用来处理请求和响应的

@Override

public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {

System.out.println("3、hello servlet 被访问了");

HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;

String method = httpServletRequest.getMethod();

if ("Get".equals(method)){

}

if ("POST".equals(method)){

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解IDEA的快捷键及智能提示
下一篇:教你怎么用SpringBoot整合Swagger作为API
相关文章

 发表评论

暂时没有评论,来抢沙发吧~