異步處理
在servlet中service方法中
// 獲得異步操作的上下文環境
AsyncContext asyncContext = req.startAsync();
// 設置異步操作超時時間
asyncContext.setTimeout(5000);
// new HeavyWorkHandler(asyncContext).handle();
// 添加異步監聽器
asyncContext.addListener(new AsyncListener() {
@Override
public void onTimeout(AsyncEvent arg0) throws IOException {
}
@Override
public void onStartAsync(AsyncEvent arg0) throws IOException {
}
@Override
public void onError(AsyncEvent arg0) throws IOException {
}
@Override
public void onComplete(AsyncEvent arg0) throws IOException {
}
});
// 啟動異步上下文進行異步操作
asyncContext.start(() -> {
ServletRequest request = asyncContext.getRequest();
ServletResponse response = asyncContext.getResponse();
// 執行耗時間的操作
// 耗時間操作完成之后要么結束異步上下文
asyncContext.complete();
// 要么將請求派發到某個頁面上
asyncContext.dispatch("page.jsp");
});
好處:
將耗時間的操作放在異步上下文中執行主要的好處是:
不影響Tomcat單位時間接入的用戶請求的數量。如果沒有異步環境Tomcat默認的200個線程用完了之后,其他的請求就必須排隊等待Tomcat接入,這樣的話越靠后的請求等待時間越長,甚至有可能半分鐘時間都無法正常接入
創建一個類來處理異步操作
public class HeavyWorkHandler {
private AsyncContext context;
public HeavyWorkHandler (AsyncContext context) {
this.context = context;
}
public void handle() {
ServletRequest request = context.getRequest();
ServletResponse response = context.getResponse();
context.start(new Runnable() {
@Override
public void run() {
}
});
}
}
安全性
在tomcat-user.xml配置
<role rolename="authorizedUser" />
<role rolename="admin" />
<role rolename="manager" />
<role rolename="emp" />
<user username="jack" password="123456" roles="admin,manager" />
在web.xml配置
<!-- 配置安全性約束 -->
<security-constraint>
<!-- 指定需要安全約束的資源 -->
<web-resource-collection>
<web-resource-name>protected</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>DIGEST</auth-method>
<realm-name>protected</realm-name>
</login-config>
AA
AA - Authentication / Authorization
- What you konw?
- What you have?
- Whom you are?
客戶端證書驗證
SSL
創建密鑰 keytool -genkey -alias tomcat -keyalg RSA
keytool - certreq -alias tomcat -file tomcat.csr
keytool -importcert -alias tomcat -file tomcat.cer