aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/http/www/Login.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-02-02 14:36:04 +0300
committerGravatar Vitaly Takmazov2016-02-02 14:36:04 +0300
commitbd3892546b51f0d31e959d45c138b009d35b2692 (patch)
tree3c3945f114472344cc3a8866391fa08f02975143 /src/main/java/com/juick/http/www/Login.java
parentf2a2660e753fa11478fd3edadfdff9c000b22cc9 (diff)
spring-jdbc
Diffstat (limited to 'src/main/java/com/juick/http/www/Login.java')
-rw-r--r--src/main/java/com/juick/http/www/Login.java39
1 files changed, 14 insertions, 25 deletions
diff --git a/src/main/java/com/juick/http/www/Login.java b/src/main/java/com/juick/http/www/Login.java
index 3e9c5e09..00775fc1 100644
--- a/src/main/java/com/juick/http/www/Login.java
+++ b/src/main/java/com/juick/http/www/Login.java
@@ -17,15 +17,14 @@
*/
package com.juick.http.www;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
+import org.springframework.jdbc.core.JdbcTemplate;
+
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.PrintWriter;
/**
*
@@ -33,7 +32,8 @@ import javax.servlet.http.HttpServletResponse;
*/
public class Login {
- protected void doGetLoginForm(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGetLoginForm(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
if (visitor != null) {
Utils.sendTemporaryRedirect(response, "/");
@@ -41,8 +41,7 @@ public class Login {
}
response.setContentType("text/html; charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
+ try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
@@ -182,12 +181,11 @@ public class Login {
out.println("</body>");
out.println("</html>");
- } finally {
- out.close();
}
}
- protected void doGetLogin(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGetLogin(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
String hash = request.getQueryString();
if (hash.length() > 32) {
response.sendError(400);
@@ -204,7 +202,8 @@ public class Login {
}
}
- protected void doPostLogin(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doPostLogin(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (username == null || password == null || username.length() > 32 || password.isEmpty()) {
@@ -230,20 +229,10 @@ public class Login {
}
}
- protected void doGetLogout(Connection sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ protected void doGetLogout(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
- if (visitor != null) {
- PreparedStatement stmt = null;
- try {
- stmt = sql.prepareStatement("DELETE FROM logins WHERE user_id=?");
- stmt.setInt(1, visitor.getUID());
- stmt.executeUpdate();
- } catch (SQLException e) {
- System.err.println(e);
- } finally {
- Utils.finishSQL(null, stmt);
- }
- }
+ sql.update("DELETE FROM logins WHERE user_id=?", visitor.getUID());
Cookie c = new Cookie("hash", "-");
c.setDomain(".juick.com");