Advertisement
Guest User

Untitled

a guest
May 11th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. // Login
  2. import java.io.*;
  3.  
  4. import java.util.*;
  5.  
  6. import javax.servlet.*;
  7.  
  8. import javax.servlet.http.*;
  9.  
  10.  
  11.  
  12. public class Login extends HttpServlet {
  13.  
  14.  
  15.  
  16.     public void doPost(HttpServletRequest request,
  17.  
  18.             HttpServletResponse response)
  19.  
  20.             throws ServletException, IOException
  21.  
  22.     {
  23.  
  24.         response.setContentType("text/html");
  25.  
  26.         PrintWriter out = response.getWriter();
  27.  
  28.         out.println("<title>Example</title>" +
  29.  
  30.                      "<body bgcolor=FFFFFF>");
  31.  
  32.  
  33.  
  34.         out.println("<h2>Finish</h2>");
  35.  
  36.  
  37.  
  38.         String User = request.getParameter("username");
  39.  
  40.         String Pass = request.getParameter("password");
  41.  
  42.        
  43.  
  44.         if(User != "" && Pass != ""){
  45.  
  46.             try
  47.  
  48.             {
  49.  
  50.                 String temp;
  51.  
  52.                 int found = 0;
  53.  
  54.                 String [] tmp = null;
  55.  
  56.                 FileInputStream input;
  57.  
  58.                 DataInputStream data;
  59.  
  60.                 input = new FileInputStream( "c:\\" + "database.txt");
  61.  
  62.                 data = new DataInputStream(input);
  63.  
  64.                 while (data.available() !=0)
  65.  
  66.                 {
  67.  
  68.                     temp = data.readLine();
  69.  
  70.                     tmp = temp.split(":");
  71.  
  72.                     if(User.compareTo(tmp[0]) == 0)
  73.  
  74.                     {
  75.  
  76.                         if(Pass.compareTo(tmp[1]) == 0)
  77.  
  78.                             out.println("<P>Logged in");
  79.  
  80.                         else
  81.  
  82.                             out.println("<P>Password is incorrect");
  83.  
  84.                         found = 1;
  85.  
  86.                         break;
  87.  
  88.                     }
  89.  
  90.                 }
  91.  
  92.                 if(found != 1)
  93.  
  94.                     out.println("<P>User not found");
  95.  
  96.                 data.close();
  97.  
  98.             }
  99.  
  100.             catch (Exception e)
  101.  
  102.             {
  103.  
  104.                 out.println("<P>Error reading from database");
  105.  
  106.             }
  107.  
  108.         }
  109.  
  110.         else   
  111.  
  112.             out.println("<P>Need to fill in all fields");
  113.  
  114.  
  115.  
  116.         out.println("<P>Return to <A HREF=../loginpage.html>Form</A>");
  117.  
  118.         out.close();
  119.  
  120.     }  
  121.  
  122.  
  123.  
  124. }
  125.  
  126.  
  127.  
  128. // Registration
  129.  
  130. import java.io.*;
  131.  
  132. import java.util.*;
  133.  
  134. import javax.servlet.*;
  135.  
  136. import javax.servlet.http.*;
  137.  
  138.  
  139.  
  140.  
  141. public class Registration extends HttpServlet {
  142.  
  143.  
  144.  
  145.     public void doPost(HttpServletRequest request,
  146.  
  147.             HttpServletResponse response)
  148.  
  149.             throws ServletException, IOException
  150.  
  151.     {
  152.  
  153.         response.setContentType("text/html");
  154.  
  155.         PrintWriter out = response.getWriter();
  156.  
  157.         out.println("<title>Example</title>" +
  158.  
  159.                      "<body bgcolor=FFFFFF>");
  160.  
  161.  
  162.  
  163.         out.println("<h2>Finish</h2>");
  164.  
  165.  
  166.  
  167.         String User = request.getParameter("username");
  168.  
  169.         String Pass = request.getParameter("password");
  170.  
  171.         String RePass = request.getParameter("repassword");
  172.  
  173.        
  174.  
  175.         String temp;
  176.  
  177.         int found = 0;
  178.  
  179.         String [] tmp = null;
  180.  
  181.         FileInputStream input;
  182.  
  183.         DataInputStream data;
  184.  
  185.         input = new FileInputStream( "c:\\" + "database.txt");
  186.  
  187.         data = new DataInputStream(input);
  188.  
  189.         while (data.available() !=0)
  190.  
  191.         {
  192.  
  193.             temp = data.readLine();
  194.  
  195.             tmp = temp.split(":");
  196.  
  197.             if(User.compareTo(tmp[0]) == 0)
  198.  
  199.                 {
  200.  
  201.                     found = 1;
  202.  
  203.                     break;
  204.  
  205.                 }
  206.  
  207.         }
  208.  
  209.         data.close();
  210.  
  211.        
  212.  
  213.         if(found == 1)
  214.  
  215.             out.println("<P>Username is already taken, choose a different username");
  216.  
  217.         else if(Pass.compareTo(RePass) != 0)
  218.  
  219.             out.println("<P>Passwords do not match");
  220.  
  221.         else if(User != "" && Pass != ""){
  222.  
  223.             try
  224.  
  225.             {
  226.  
  227.                 FileOutputStream output;
  228.  
  229.                 PrintStream p;
  230.  
  231.        
  232.  
  233.                 output = new FileOutputStream( "c:\\" + "database.txt", true);
  234.  
  235.                 p = new PrintStream(output);
  236.  
  237.        
  238.  
  239.                 p.println(User + ":" + Pass);
  240.  
  241.                 p.close();
  242.  
  243.             }
  244.  
  245.             catch (Exception e)
  246.  
  247.             {
  248.  
  249.                 out.println("<P>Error writing to database");
  250.  
  251.             }
  252.  
  253.             out.println("<P>User registered");
  254.  
  255.         }
  256.  
  257.         else   
  258.  
  259.             out.println("<P>Need to fill in all fields");
  260.  
  261.  
  262.  
  263.         out.println("<P>Return to <A HREF=../registrationpage.html>Form</A>");
  264.  
  265.         out.close();
  266.  
  267.     }  
  268.  
  269.  
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement