实在不知道servlet是否单例的,于是乎,做了一个小小的测试,不知道这样测试是否准确,望大牛指正。感激!
1. 写了一个servlet
class="java" name="code">
public class IsSingletonServlet extends HttpServlet {
private static final long serialVersionUID = 485167805086334673L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.println(this);
}
}
2. 配置web.xml
<servlet>
<servlet-name>checkSingleton</servlet-name>
<servlet-class>com.jesse.servlet.IsSingletonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>checkSingleton</servlet-name>
<url-pattern>/checksingleton</url-pattern>
</servlet-mapping>
3. 测试页面
<body>
<%
String context = request.getContextPath();
%>
<form action="<%=context %>/checksingleton" method="get">
<input type="submit" value="测试是否单例">
</form>
</body>
4. 同时打开多个页面访问,输出相同
com.jesse.servlet.IsSingletonServlet@4c230
5. 以上证实servlet是单例的