Cookie based Login in servlets

CookieLoginServlet

package com.rdta;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

import java.sql.*;

public class CookieLoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
sendLoginForm(response, false);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String userName = request.getParameter("userName");
String password = request.getParameter("password");
if (login(userName, password)) {

Cookie c1 = new Cookie("userName", userName);
Cookie c2 = new Cookie("password", password);
response.addCookie(c1);
response.addCookie(c2);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<META HTTP-EQUIV=Refresh CONTENT=0;URL=ContentServlet>");
}
else {
sendLoginForm(response, true);
}
}

private void sendLoginForm(HttpServletResponse response, boolean withErrorMessage)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Login</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<CENTER>");

if (withErrorMessage) {
out.println("Login failed. Please try again.<BR>");

}
out.println("<BR>");
out.println("<BR><H2>Login Page</H2>");
out.println("<BR>");
out.println("<BR>Please enter your user name and password.");
out.println("<BR>");
out.println("<BR><FORM METHOD=POST>");
out.println("<TABLE>");
out.println("<TR>");
out.println("<TD>User Name:</TD>");
out.println("<TD><INPUT TYPE=TEXT NAME=userName></TD>");
out.println("</TR>");
out.println("<TR>");
out.println("<TD>Password:</TD>");
out.println("<TD><INPUT TYPE=PASSWORD NAME=password></TD>");
out.println("</TR>");
out.println("<TR>");
out.println("<TD ALIGN=RIGHT COLSPAN=2>");
out.println("<INPUT TYPE=SUBMIT VALUE=Login></TD>");
out.println("</TR>");
out.println("</TABLE>");
out.println("</FORM>");
out.println("</CENTER>");
out.println("</BODY>");
out.println("</HTML>");
}

public static boolean login(String userName, String password) {
try {

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/db2","root","indian");
Statement st = con.createStatement();
String sql = "SELECT name FROM login WHERE name='" +userName+ "' AND pwd='" + password + "'";

ResultSet rs = st.executeQuery(sql);

if (rs.next()) {
rs.close();
st.close();
con.close();
return true;
}
rs.close();
st.close();
con.close();
}
catch (ClassNotFoundException e) {
System.out.println(e.toString());
}
catch (SQLException e) {
System.out.println(e.toString());
}
catch (Exception e) {
System.out.println(e.toString());
}
return false;
}
}



ContentServlet


package com.rdta;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ContentServlet extends HttpServlet {

public String loginUrl = "CookieLoginServlet";

public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Cookie[] cookies = request.getCookies();
int length = cookies.length;
String userName = null;
String password = null;

for (int i=0; i<length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("userName"))
userName = cookie.getValue();
else if (cookie.getName().equals("password"))
password = cookie.getValue();
}

if (userName==null || password==null || !CookieLoginServlet.login(userName,
password))
response.sendRedirect(loginUrl);

// This is an authorized user, okay to display content
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Welcome</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Welcome.");
out.println("</BODY>");
out.println("</HTML>");
}

/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
}
web.xml

<web-app>

<servlet>
<servlet-name>cookielog</servlet-name>
<servlet-class>com.rdta.CookieLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookielog</servlet-name>
<url-pattern>/cookielogin</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ContentServlet</servlet-name>
<servlet-class>com.rdta.ContentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContentServlet</servlet-name>
<url-pattern>/ContentServlet</url-pattern>
</servlet-mapping>

</web-app>

cookielogin.html

<html>
<body>
<form method="get" action="cookielogin">
<input type="submit" value="submit"/>
</form>
</body>
</html>

No comments: