以前都是不用工具开发。开发比较麻烦,这里开始,把项目导入到idea里
(1)导入工程
(2)新建src目录,方便以后开发servlet等java代码
(3)导入jar配置项目
①配置java编译完放置路径,放在classes目录下
②导入tomcat的jar,
③编辑web.xml放的路径
④新增Airifacts
(1)在web.xml配置如下配置
<servlet>
<servlet-name>secondServletservlet-name>
<servlet-class>com.servletdemo.SecondServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>secondServletservlet-name>
<url-pattern>/secondServleturl-pattern>
servlet-mapping>
(2)开发
package com.servletdemo;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 〈第二个servlet〉
*
* @author PitterWang
* @create 2020/6/22
* @since 1.0.0
*/
public class SecondServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("index.jsp");
}
}
package com.servletdemo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 〈第一个Servlet〉
*
* @author PitterWang
* @create 2020/6/22
* @since 1.0.0
*/
@WebServlet(name = "firstServlet",urlPatterns = {"/firstServlet"})
public class FirstServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("index.jsp");
}
}
访问:http://localhost:8080/webDemo/firstServlet即可跳到index.jsp
@WebServlet的属性
属性名 | 类型 | 描述 |
---|---|---|
name | String | servlet-name,如果没有显示指定,该Servlet的取值为全限定名 |
value | String[] | 等价于 urlPatterns 属性,与该属性不能同时使用 |
urlPatterns | String[] | 指定Servlet url的匹配模式,等价于 |
loadOnStartup | int | 指定Servlet的加载顺序 |
initParams | webInitParam[] | 指定初始化参数 |
asyncSupported | boolean | 是否支持异步操作 |
description | String | 描述 |
displayName | String | servlet显示名 |
创建servlet的两个时机
(1)客户端第一次请求某个servlet的时候,系统创建servlet实例
(2)web应用启动时,立即创建servlet,load-on-startup Servlet
配置的两种方法
第一种:@WebServlet(name = "firstServlet",urlPatterns = {"/firstServlet"},loadOnStartup = 1)
第二种:
secondServlet
com.servletdemo.SecondServlet
2
每个Servlet生命周期
(1)创建servlet实例
(2)web容器调用init方法,对servlet进行初始化
(3)servlet初始化之后,用于一直响应客户端请求。如果客户端发送Get请求,指向doGet方法,发送Post请求,指向doPost方法。或者统一使用service()响应用户请求
(4)web容器销毁时,调用destroy()方法。
<servlet>
<servlet-name>secondServletservlet-name>
<servlet-class>com.servletdemo.SecondServletservlet-class>
<load-on-startup>2load-on-startup>
<init-param>
<param-name>nameparam-name>
<param-value>王成东param-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>secondServletservlet-name>
<url-pattern>/secondServleturl-pattern>
servlet-mapping>
package com.servletdemo;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 〈第二个servlet〉
*
* @author PitterWang
* @create 2020/6/22
* @since 1.0.0
*/
public class SecondServlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("初始化");
String name = getServletConfig().getInitParameter("name");
System.out.println(name);
super.init();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("调用");
resp.sendRedirect("index.jsp");
}
@Override
public void destroy() {
System.out.println("销毁");
super.destroy();
}
}
package com.servletdemo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 〈第一个Servlet〉
*
* @author PitterWang
* @create 2020/6/22
* @since 1.0.0
*/
@WebServlet(name = "firstServlet",urlPatterns = {"/firstServlet"},loadOnStartup = 1,
initParams = {@WebInitParam(name="password",value = "1234")})
public class FirstServlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("first创建");
String name = getServletConfig().getInitParameter("name");
String password = getServletConfig().getInitParameter("password");
System.out.println(name);
System.out.println(password);
super.init();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("index.jsp");
}
}
jsp开发标签库需要如下几个步骤
(1)开发自定义标签处理类
继承SimpleTagSupport类,重新doTag()方法
package com.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.Date;
/**
* 〈自定义标签类〉
*
* @author PitterWang
* @create 2020/6/22
* @since 1.0.0
*/
public class HelloTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
getJspContext().getOut().write("helloword" + new Date());
}
}
(2)建立一个*.tld文件,每个*.tld文件对应对应一个标签库,每个标签库可包含多个标签库
1.0
myshortname
http://mycompany.com
helloword
com.tag.HelloTag
empty
(3)在jsp中使用自定义标签
jsp页面中确定指定的标签需要两点
(1)标签库uri:确定使用那个标签库
(2)标签名:确定使用那个标签
使用标签分为两个步骤
(1)导入标签库
(2)使用标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入标签库,指定mytag前缀的标签--%>
<%@ taglib uri="http://mycompany.com" prefix="mytag"%>
Hello
WELCOME
<%--使用标签,其中mytag是标签前缀,根据taglib的编译指令--%>
(1)开发自定义标签处理类
继承SimpleTagSupport类,重新doTag()方法
package com.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.Date;
/**
* 〈带属性的标签〉
*
* @author PitterWang
* @create 2020/6/23
* @since 1.0.0
*/
public class AttributeTag extends SimpleTagSupport {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
@Override
public void doTag() throws JspException, IOException {
getJspContext().getOut().write("userName = " + userName + ",passWord = " + passWord);
}
}
(2)建立一个*.tld文件,每个*.tld文件对应对应一个标签库,每个标签库可包含多个标签库
attribute
com.tag.AttributeTag
empty
userName
true
true
passWord
true
true
(3)在jsp中使用自定义标签
1.Filter的作用
1~在HttpServletRequest到达Servlet之前,拦截客户的HttpServletRequest
2~根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据
3~在HttpServletResponse到达客户端之前,拦截HttpServletResponse。
4~根据需要检查HttpServletResponse,也可以修改HttpServletResponse头和数据
2.Filter的几种种类
1~用户授权的Filter
2~日志Filter
3~负责解码的Filter
4~能改变XML内容的XSLT Filter
5~Filter可以拦截多个请求或者响应
3.创建一个Filter步骤
1~创建Filter处理类
2~web.xml文件中配置Filter
4.Filter和Servlet配置和使用差不多
(1)创建Filter处理类
//实现javax.servlet.Filter接口,接口定义下面三个方法
package com.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* 〈拦截用户请求,把请求的信息记录到日志〉
*
* @author PitterWang
* @create 2020/6/23
* @since 1.0.0
*/
public class LogFilter implements Filter {
//访问Filter的配置信息
private FilterConfig config;
/**
* 初始化方法
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletContext servletContext = this.config.getServletContext();
long before = System.currentTimeMillis();
System.out.println("开始过滤");
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String servletPath = httpServletRequest.getServletPath();
System.out.println("l拦截的地址 = "+servletPath);
filterChain.doFilter(servletRequest,servletResponse);
long aftet = System.currentTimeMillis();
System.out.println("过滤结束");
System.out.println("请求呗定位到" + httpServletRequest.getRequestURI() + "用时 "+ (aftet - before));
}
/***
* 销毁方法
*/
@Override
public void destroy() {
this.config = null;
}
}
(2)两种配置方式
1.注解方式配置
在Filter处理类上加注解
@WebFilter(filterName = "log",urlPatterns = {"/*"})
2.web.xml方式配置
<filter>
<filter-name>logfilter-name>
<filter-class>com.filter.LogFilterfilter-class>
filter>
<filter-mapping>
<filter-name>logfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
使用Listener进行监听只需要两个步骤
1.定义Listener实现类
2.通过注解或者xml文件中配置Listener
(1)实现Listener类
常用的事件监听接口
ServletContextListener:用于监听Web应用的启动和关闭
ServletContextAttributeListener:用于监听application内属性的改变
ServletRequestListener:监听用户请求
ServletRequestAttributeListener:监听request范围内属性的改变
HttpSessionListener:监听session的开始与结束
HttpSessionAttributeListener:静态session范围内的属性改变
package com.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebFilter;
/**
* 〈Web应用启动和关闭监听〉
*
* @author PitterWang
* @create 2020/6/23
* @since 1.0.0
*/
/*@WebFilter*/
public class ConListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Web服务启动");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("Web服务关闭");
}
}
(2)配置Listener
方式一:注解方式
在Listener处理类上加注解@WebFilter
方式二:web.xml配置
<listener>
<listener-class>com.listener.ConListenerlistener-class>
listener>
测试项目下载地址;https://github.com/wangchengdonggo/webDemo.git