Showing posts with label servlets. Show all posts
Showing posts with label servlets. Show all posts

How to use cookies and how to get all request headers in servlets

CookieServlet.java


package com.rdta;

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

public class CookieServlet extends HttpServlet {

/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie c1 = new Cookie("userName", "prasanthi");
Cookie c2 = new Cookie("password", "sourav");
response.addCookie(c1);
response.addCookie(c2);
Cookie c3=new Cookie("santhi","test");
c3.setPath(request.getContextPath());
c3.setMaxAge(120);
response.addCookie(c3);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Cookie Test</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("Please click the button to see the cookies sent to you.");
out.println("path"+c3.getPath());
out.println("path"+request.getContextPath());
out.println("<BR>");
out.println("<FORM METHOD=POST>");
out.println("<INPUT TYPE=SUBMIT VALUE=Submit>");
out.println("</FORM>");
out.println("</BODY>");
out.println("</HTML>");
}
/**Process the HTTP Post request*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException,IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Cookie Test</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<H2>Here are all the headers.</H2>");

Enumeration enum1 = request.getHeaderNames();
while (enum1.hasMoreElements()) {
String header = (String) enum1.nextElement();
out.print("<B>" + header + "</B>: ");
out.print(request.getHeader(header) + "<BR>");
}

out.println("<BR><BR><H2>And, here are all the cookies.</H2>");
Cookie[] cookies = request.getCookies();
int length = cookies.length;
for (int i=0; i<length; i++) {
Cookie cookie = cookies[i];

out.println("<B>Cookie Name:</B> " + cookie.getName() + "<BR>");
out.println("<B>Cookie Value:</B> " + cookie.getValue() + "<BR>");
}
out.println("</BODY>");
out.println("</HTML>");
}
}

cookie.html

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

web.xml

<web-app>

<servlet>
<servlet-name>cookie</servlet-name>
<servlet-class>com.rdta.CookieServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cookie</servlet-name>
<url-pattern>/cookie</url-pattern>
</servlet-mapping>

</web-app>

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;
}
}