servlet是什么?
emmm...servlet主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。
Servlet工作流程分为三个阶段:init(初始化),service(运行),destroy(销毁)
Servlet没有main方法,所有行为由Container控制。Container是web容器中的servlet容器,常见的web容器有tomcat等。
在加载Servlet的.class后,Servlet会由构造函数生成一个实例,然后Container调用init()方法完成参数的初始化,接着调用service()方法,service会根据网页的请求,调用doGet或者doPost方法,最后调用销毁方法。整个流程如下图:
使用springboot编写一个servlet,show me the code,dont bb...
maven配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
servlet代码:
package com.juliet.testServlet.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @Author: Juliet
* @Date: 2018/3/7
* @Usage: Juliet's graduation design
* Description:
*/
public class servletDemo extends HttpServlet {
private static final long serialVersionUID = -9200488852821156029L;
// 该函数用于初始化该servlet, 类似于我们的类的构造函数
// 该函数只是会被调用一次, 当用户第一次访问该servlet的时候被调用
public void init(ServletConfig parm1) throws ServletException
{
System.out.println("init servlet demo !");
}
//提供service
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("service......");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>this is a servlet demo</h1>");
out.println("</body>");
out.println("</html>");
}
// 销毁servlet实例(释放内存)
// 1 reload 该servlet(webApp)
// 2 关闭Tomcat 或者说 关机之后 都会调用这个函数
public void destroy()
{
System.out.println("destory servlet demo");
}
}
springboot Main函数代码:
package com.juliet.testServlet;
import com.juliet.testServlet.servlet.servletDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
//自动扫描servlet
@ServletComponentScan
@SpringBootApplication
public class TestServletApplication {
public static void main(String[] args) {
SpringApplication.run(TestServletApplication.class, args);
}
//servlet注册
@Bean
public ServletRegistrationBean servletRegistrationBean() {
// ServletName默认值为首字母小写,即myServlet
return new ServletRegistrationBean(new servletDemo(),"/juliet/*");
}
}
测试结果展示:
[图片上传失败...(image-b0ba0e-1520406726860)]
刷新网页后,控制台显示如下:
[图片上传失败...(image-1f9353-1520406726860)]
servlet是否线程安全?
不是,因为每个servlet在tomcat中只有一个实例,当多个请求过来时,tomcat会开启多个线程去调用servlet,当servlet中具有静态变量或者实例变量的时候,就不能保证线程安全,做个实验验证下:
servlet代码:
package com.juliet.testServlet.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @Author: Juliet
* @Date: 2018/3/7
* @Usage: Juliet's graduation design
* Description:
*/
public class servletThreadsafeDemo extends HttpServlet{
private static final long serialVersionUID = -8284164610778425578L;
private static int staticInt = 0;
int instanceInt = 100;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("service......");
staticInt++;
instanceInt++;
try {
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
System.out.println("thread name is: "+Thread.currentThread().getName()+" instanceInt is: "+instanceInt);
System.out.println("thread name is: "+Thread.currentThread().getName()+" staticInt is: "+staticInt);
}
}
springboot main函数代码:
package com.juliet.testServlet;
import com.juliet.testServlet.servlet.servletDemo;
import com.juliet.testServlet.servlet.servletThreadsafeDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
//自动扫描servlet
@ServletComponentScan
@SpringBootApplication
public class TestServletApplication {
public static void main(String[] args) {
SpringApplication.run(TestServletApplication.class, args);
}
//servlet注册
@Bean
public ServletRegistrationBean servletDemoBean() {
return new ServletRegistrationBean(new servletDemo(),"/servletDemo/*");
}
//servlet注册
@Bean
public ServletRegistrationBean servletThreadSafeBean() {
return new ServletRegistrationBean(new servletThreadsafeDemo(),"/servletThreadSafe/*");
}
}
刷新几次网页后,测试结果如下:
可以看出tomcat容器是调用了多线程来并发处理servlet请求的,普通的servlet并不支持线程安全
如何使servlet支持线程安全呢?
具体有三种方法:
1.简单粗暴的使用synchronized 关键字,就是给代码块上锁,该关键字的使用方法详细自己去百度哦~
使用方法如下:
Public class XXXXXX extends HttpServlet {
synchronized (this){XXXX}
}
使用该方法后,测试结果如下:
这种方法使得一个servlet在一段时间只能处理一个请求,使得处理请求的吞吐量降低,很多请求将处于阻塞状态。
2.实现 SingleThreadModel 接口
public class XXXXX extends HttpServlet implements SingleThreadModel {
…………
}
如果一个Servlet实现了SingleThreadModel接口,Servlet引擎将为每个新的请求创建一个单独的Servlet实例,这将引起大量的系统开销。
3.避免使用实例变量和静态变量
解决一个问题最佛系的办法就是避免问题发生...