继续上次的tomcat
主要是配置tomcat插件
---------------------------------------------------------------------------------------------------------------------------------
servlet
生命周期三种方法:
init和destroy都是只能使用一次,而service是可以使用多次,我的这个强制关闭tomcat时候实惠执行destroy的,可能是因为版本问题。反而视频的那种destroy不能被执行
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet(urlPatterns = "/demo1",loadOnStartup = 1)
public class ServletDemo2 implements Servlet {
/*
* 初始化方法
* 1.调用时机:默认情况下:seervlet被第一次访问时调用
* *loadOnStartup:
* 2.调用次数:1次
*
* */
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init...");
}
/*
* 提供服务
* 1.调用时机:每一次servlet北访问时
* 2.调用次数:多次
* */
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("servlet hello world");
}
/*
* 销毁方法
* 1.调用时机:内存释放或者服务器关闭时候,servlet对象被销毁,
* 2.调用次数:1次
* */
public void destroy() {
System.out.println("destroy...");
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
}
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class ServletDemo4 implements Servlet {
public void init(ServletConfig servletConfig) throws ServletException {
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
//根据请求方式不同,进行分别处理
HttpServletRequest request= (HttpServletRequest) servletRequest;
//1.获取请求方式
String method = request.getMethod();
//2.判断
if("GET".equals(method)){
//get方式的处理逻辑
} else if ("POST".equals(method)) {
//post方式处理逻辑
}
}
public String getServletInfo() {
return null;
}
public void destroy() {
}
}
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class MyHttpServlet implements Servlet {
public void init(ServletConfig servletConfig) throws ServletException {
}
public ServletConfig getServletConfig() {
return null;
}
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
//根据请求方式不同,进行分别处理
HttpServletRequest request= (HttpServletRequest) servletRequest;
//1.获取请求方式
String method = request.getMethod();
//2.判断
if("GET".equals(method)){
//get方式的处理逻辑
doGet(servletRequest,servletResponse);
} else if ("POST".equals(method)) {
//post方式处理逻辑
doPost(servletRequest,servletResponse);
}
}
protected void doPost(ServletRequest servletRequest, ServletResponse servletResponse) {
}
protected void doGet(ServletRequest servletRequest, ServletResponse servletResponse) {
}
public String getServletInfo() {
return null;
}
public void destroy() {
}
}
package com.itheima.web;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@WebServlet("/demo5")
public class ServletDemo5 extends MyHttpServlet {
@Override
protected void doGet(ServletRequest servletRequest, ServletResponse servletResponse) {
System.out.println("get......");
}
@Override
protected void doPost(ServletRequest servletRequest, ServletResponse servletResponse) {
System.out.println("post......");
}
}
一个路径,同时满足精确匹配和目录匹配,优先精确。
用xml配置确实要简单很多,在大目录WEB-INF下面的xml文件中,编写xml文件,然后在一般类中编写相应的代码
<!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>Archetype Created Web Application</display-name>
<!-- Servlet全类名 -->
<servlet>
<servlet-name>demo9</servlet-name>
<servlet-class>com.itheima.web.ServletDemo9</servlet-class>
</servlet>
<!-- Servlet 访问路径-->
<servlet-mapping>
<servlet-name>demo9</servlet-name>
<url-pattern>/demo9</url-pattern>
</servlet-mapping>
</web-app>
package com.itheima.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo10 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo10 get ...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo10 post...");
}
}
以上是servlet,完毕
---------------------------------------------------------------------------------------------------------------------------------
Request(请求)和Response(响应)
有点听不懂了,很懵逼,已经发呆很久了。学不进,不知道是天气问题还是我的问题
溜了溜了