0
我開發了一個基於客戶端證書來驗證用戶的servlet。 但是,我通過在類定義中放置了一些變量以使它們在會話之間共享而犯了一個錯誤。 我應該將它們放入doGet方法中。Servlet - 數據源連接的變量作用域最佳實踐
現在我想獲得有關DataSource最佳實踐的建議。 我在類中聲明InitialContext和Datasource,並在init方法中初始化它們。 我在doSet中聲明瞭連接,並在其中執行了ds.getConnection。 我的下列代碼是否正確?
在此先感謝。
public class readclientcertificate extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
// moved in doGet private X509Certificate cert;
// moved in doGet private String strUUID = "";
// moved in doGet private String strFormsURL = "";
private InitialContext ic;
private DataSource ds = null;
public void init(ServletConfig config) throws ServletException, NamingException {
super.init(config);
ic = new InitialContext();
ds = (DataSource)ic.lookup("jdbc/OracleCoreDS");
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
X509Certificate cert = null;
String strUUID = "";
String strFormsURL = "";
Connection conn = null;
// Retreive Certificate
try{
X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
cert = certs[0];
} catch(Exception e){
out.println(e.getMessage());
}
// Connect to DB and Insert Certificate Informations.
if (cert != null) {
try {
conn = ds.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select K$LOGON.FA_ADD_CERTIFICATE('" + cert.getSerialNumber() + "','" + cert.getSubjectDN() + "','" + request.getRemoteAddr() + "','" + request.getRemoteHost() + "','" + request.getRequestURL() + "','" +strFormsURL + "') from dual");
while (rs.next()) {
strUUID = rs.getString(1);
}
conn.close();
} catch (SQLException se) {
throw new ServletException(se);
} catch (NamingException ne) {
throw new ServletException(ne);
}
}