Wednesday, 27 March 2019

Real Time Login Application - Login, Logout and Profile

  1. index.html
  2. link.html
  3. login.html
  4. LoginServlet.java
  5. LogoutServlet.java
  6. ProfileServlet.java
  7. web.xml
index.html 

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="ISO-8859-1">  
<title>Servlet Login Example</title>  
</head>  
<body>  
  
<h1>Welcome to Login App by Cookie</h1>  
<a href="login.html">Login</a>|  
<a href="LogoutServlet">Logout</a>|  
<a href="ProfileServlet">Profile</a>  
  
</body>  
</html>  


servlet cookie login example 1


link.html


  1. <a href="login.html">Login</a> |  
  2. <a href="LogoutServlet">Logout</a> |  
  3. <a href="ProfileServlet">Profile</a>  
  4. <hr>



servlet cookie login example 2

Login.html

  1. <form action="LoginServlet" method="post">  
  2. Name:<input type="text" name="name"><br>  
  3. Password:<input type="password" name="password"><br>  
  4. <input type="submit" value="login">  
  5. </form>  


LoginServlet.java


  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.Cookie;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. public class LoginServlet extends HttpServlet {  
  9.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  10.                            throws ServletException, IOException {  
  11.         response.setContentType("text/html");  
  12.         PrintWriter out=response.getWriter();  
  13.           
  14.         request.getRequestDispatcher("link.html").include(request, response);  
  15.           
  16.         String name=request.getParameter("name");  
  17.         String password=request.getParameter("password");  
  18.           
  19.         if(password.equals("admin123")){  
  20.             out.print("You are successfully logged in!");  
  21.             out.print("<br>Welcome, "+name);  
  22.               
  23.             Cookie ck=new Cookie("name",name);  
  24.             response.addCookie(ck);  
  25.         }else{  
  26.             out.print("sorry, username or password error!");  
  27.             request.getRequestDispatcher("login.html").include(request, response);  
  28.         }  
  29.           
  30.         out.close();  
  31.     }  
  32.   
  33. }  

servlet cookie login example 4



servlet cookie login example 3




 LogoutServlet.java



  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.Cookie;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. public class LogoutServlet extends HttpServlet {  
  9.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  10.                         throws ServletException, IOException {  
  11.         response.setContentType("text/html");  
  12.         PrintWriter out=response.getWriter();  
  13.           
  14.           
  15.         request.getRequestDispatcher("link.html").include(request, response);  
  16.           
  17.         Cookie ck=new Cookie("name","");  
  18.         ck.setMaxAge(0);  
  19.         response.addCookie(ck);  
  20.           
  21.         out.print("you are successfully logged out!");  
  22.     }  
  23. }  
servlet cookie login example 6


ProfileServlet.java

  1. import java.io.IOException;  
  2. import java.io.PrintWriter;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.Cookie;  
  5. import javax.servlet.http.HttpServlet;  
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. public class ProfileServlet extends HttpServlet {  
  9.     protected void doGet(HttpServletRequest request, HttpServletResponse response)  
  10.                           throws ServletException, IOException {  
  11.         response.setContentType("text/html");  
  12.         PrintWriter out=response.getWriter();  
  13.           
  14.         request.getRequestDispatcher("link.html").include(request, response);  
  15.           
  16.         Cookie ck[]=request.getCookies();  
  17.         if(ck!=null){  
  18.          String name=ck[0].getValue();  
  19.         if(!name.equals("")||name!=null){  
  20.             out.print("<b>Welcome to Profile</b>");  
  21.             out.print("<br>Welcome, "+name);  
  22.         }  
  23.         }else{  
  24.             out.print("Please login first");  
  25.             request.getRequestDispatcher("login.html").include(request, response);  
  26.         }  
  27.         out.close();  
  28.     }  
  29. }  
servlet cookie login example 5


Web.xml


  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3. xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  4. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  5.     
  6.   <servlet>  
  7.     <description></description>  
  8.     <display-name>LoginServlet</display-name>  
  9.     <servlet-name>LoginServlet</servlet-name>  
  10.     <servlet-class>LoginServlet</servlet-class>  
  11.   </servlet>  
  12.   <servlet-mapping>  
  13.     <servlet-name>LoginServlet</servlet-name>  
  14.     <url-pattern>/LoginServlet</url-pattern>  
  15.   </servlet-mapping>  
  16.   <servlet>  
  17.     <description></description>  
  18.     <display-name>ProfileServlet</display-name>  
  19.     <servlet-name>ProfileServlet</servlet-name>  
  20.     <servlet-class>ProfileServlet</servlet-class>  
  21.   </servlet>  
  22.   <servlet-mapping>  
  23.     <servlet-name>ProfileServlet</servlet-name>  
  24.     <url-pattern>/ProfileServlet</url-pattern>  
  25.   </servlet-mapping>  
  26.   <servlet>  
  27.     <description></description>  
  28.     <display-name>LogoutServlet</display-name>  
  29.     <servlet-name>LogoutServlet</servlet-name>  
  30.     <servlet-class>LogoutServlet</servlet-class>  
  31.   </servlet>  
  32.   <servlet-mapping>  
  33.     <servlet-name>LogoutServlet</servlet-name>  
  34.     <url-pattern>/LogoutServlet</url-pattern>  
  35.   </servlet-mapping>  
  36. </web-app>  

Cookies in Servlet

cookies in servlet

  1. Non-persistent cookie
  2. Persistent cookie

How to create Cookie?


Cookie ck=new Cookie("user","Rajender");//creating cookie object  

response.addCookie(ck);//adding cookie in the response  
____________________________________________________________________

How to delete Cookie?

Cookie ck=new Cookie("user","");//deleting value of cookie  

ck.setMaxAge(0);//changing the maximum age to 0 seconds  

response.addCookie(ck);//adding cookie in the response  

__________________________________________________________________


How to get Cookies?



Cookie ck[]=request.getCookies();  

for(int i=0;i<ck.length;i++)

{  
 out.print("<br>"+ck[i].getName()+" "+ck[i].getValue());
//printing name and value of cookie  

_______________________________________________________________________

Sunday, 10 March 2019

Database Connection Code

New-> Java Project-> Java Class


DatabseConnectionSample.java:


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DatabseConnectionSample {


public static void main(String[] args) throws Exception {


Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
Statement st=con.createStatement();
st.executeQuery("CREATE TABLE AVANTHI(NAME VARCHAR2(10),EMAIL VARCHAR2(20))");
}

}

doGet Method



index.html

<!DOCTYPE html>
<html>
<head>

<title>Search</title>
</head>
<body>
<form action="google" method="get">

Google Search:<input type="text" name="search"></br>
<input type="submit" value="Search">

</form>

</body>

</html>





GoogleSearch Servlet File:


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class GoogleSearch extends HttpServlet {


protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

{

String name=req.getParameter("search");

res.setContentType("text/html");
PrintWriter pw=res.getWriter();

res.sendRedirect("https://www.google.com/#q="+name);



}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>GoogleSearch</display-name>
 
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 
  <servlet>
  <servlet-name>ss</servlet-name>
  <servlet-class>GoogleSearch</servlet-class>
  </servlet>
 
  <servlet-mapping>
  <servlet-name>ss</servlet-name>
  <url-pattern>/google</url-pattern>
  </servlet-mapping>
 
</web-app>




Sunday, 17 February 2019

XML and DTD

students.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE students SYSTEM "stu.dtd">
<students>
<student sid="a5" course="Java">

<name>Kavya</name>
<phone>25252525</phone>
</student>

<student sid="a6">

<name>Kavya</name>
<phone>25252525</phone>
</student>

</students>

std.dtd



<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT students (student+)>
<!ELEMENT student (name,phone)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT phone (#PCDATA)>
<!ATTLIST student sid ID #REQUIRED>
<!ATTLIST student course (Java | C | Python ) "Java">



schema.std:


<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2011 sp1 (http://www.altova.com)-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="students">
<xs:complexType>
<xs:sequence>
<xs:element ref="student" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="student">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="phone"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="phone">
<xs:simpleType>
<xs:restriction base="xs:long">
<xs:enumeration value="986695858"/>
<xs:enumeration value="9866986623"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Jyosthna"/>
<xs:enumeration value="Mohith"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>

Monday, 11 February 2019

Cookies Information Throughout the Project

cookie.php


<?php
$t=time();
echo $t;
setcookie('student','rajender',$t+1200);
?>


view.php

<?php

echo $_COOKIE['student'];

?>


for 1 day 86400
-1 day -86400 from server history