Guest User

Untitled

a guest
Jun 8th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. //GetSet.java <-----------used as beans
  2. package thisPackage;
  3.  
  4. public class GetSet {
  5.  
  6.  
  7.  
  8. private String uname;
  9. private String pword;
  10. public boolean ifvalid;
  11.  
  12.  
  13.  
  14. public String getUname () {
  15.  
  16. return uname;
  17.  
  18. }
  19.  
  20. public void setUname (String myuname) {
  21.  
  22.  
  23. this.uname=myuname;
  24. }
  25.  
  26. public String getPword () {
  27.  
  28. return pword;
  29.  
  30. }
  31.  
  32.  
  33. public void setPword (String mypword) {
  34.  
  35.  
  36. this.pword=mypword;
  37. }
  38.  
  39.  
  40. public boolean isValid() {
  41.  
  42.  
  43. return ifvalid;
  44. }
  45.  
  46. public void setisValid (boolean validity)
  47. {
  48.  
  49. this.ifvalid=validity;
  50.  
  51. }
  52.  
  53. package thisPackage;
  54.  
  55. import java.sql.Connection;
  56. import java.sql.DriverManager;
  57.  
  58. public class konekMoko {
  59.  
  60.  
  61. static Connection con;
  62. static String url;
  63.  
  64. public static Connection getCon () throws Exception {
  65. try {
  66.  
  67.  
  68.  
  69. String driver = "com.mysql.jdbc.Driver";
  70. String url = "jdbc:mysql://localhost:3306/csdb";
  71. String username= "root";
  72. String password = "root";
  73. Class.forName(driver);
  74.  
  75. //System.out.println("HAHAHAHA");
  76. Connection conn= DriverManager.getConnection(url, username, password);
  77. System.out.println("You are now Connected!!");
  78. return conn; // Return if it is successfully connected!
  79.  
  80.  
  81.  
  82. }
  83. catch (Exception e)
  84. {
  85. e.printStackTrace();
  86. //System.out.println("Connection not Established! "+e.getMessage());
  87. }
  88.  
  89.  
  90. return null; // Return if unsuccessful
  91. }
  92.  
  93. }
  94.  
  95.  
  96. /*String url = "jdbc:mysql://localhost:3306/csdb";
  97.  
  98. Class.forName("jdbc:mysql://localhost:3306/csdb");
  99.  
  100. try {
  101.  
  102. con = DriverManager.getConnection(url, "username", "password");
  103.  
  104. }
  105. catch (Exception e) {
  106.  
  107. e.printStackTrace();
  108.  
  109. }
  110.  
  111. }
  112.  
  113. catch (Exception e)
  114. {
  115. e.printStackTrace();
  116.  
  117. }
  118. return con;*/
  119.  
  120. //LoginServlet.java
  121.  
  122. package thisPackage;
  123.  
  124. import java.io.IOException;
  125. import java.io.PrintWriter;
  126.  
  127. import javax.servlet.ServletException;
  128. import javax.servlet.http.HttpServlet;
  129. import javax.servlet.http.HttpServletRequest;
  130. import javax.servlet.http.HttpServletResponse;
  131. import javax.servlet.http.HttpSession;
  132.  
  133. /**
  134. * Servlet implementation class LoginServlet
  135. */
  136. public class LoginServlet extends HttpServlet {
  137. private static final long serialVersionUID = 1L;
  138.  
  139. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  140.  
  141. response.setContentType("text/html");
  142. PrintWriter post = response.getWriter();
  143.  
  144. try
  145. {
  146.  
  147. GetSet gsup = new GetSet();
  148.  
  149. gsup.setUname(request.getParameter("username"));
  150. gsup.setPword(request.getParameter("password"));
  151.  
  152. //System.out.println("HAHAHAHAHA");
  153.  
  154. gsup = myDAO.login(gsup);
  155.  
  156. if (gsup.isValid())
  157. {
  158.  
  159. post.print("Welcome" + gsup);
  160. HttpSession mysession = request.getSession(true);
  161. mysession.setAttribute("thiscurrentsuser", gsup);
  162. mysession.setMaxInactiveInterval(120); // 2 mins stay on the page
  163. response.sendRedirect("loggedin.jsp"); // the user will be redirected to the loggedin page if true
  164.  
  165.  
  166. }
  167.  
  168. else
  169. {
  170.  
  171. response.sendRedirect("thisinvaliduser.jsp"); //if it is not valid it will throw you to the invalid log page
  172.  
  173. }
  174.  
  175. }
  176.  
  177. catch (Throwable e){
  178.  
  179. System.out.println(e);
  180.  
  181.  
  182. // TODO Auto-generated method stub
  183. doGet(request, response);
  184. }
  185.  
  186. }
  187. }
  188.  
  189. //MyDAO.java
  190.  
  191.  
  192. package thisPackage;
  193.  
  194. import java.sql.Connection;
  195. import java.sql.PreparedStatement;
  196. import java.sql.ResultSet;
  197. //import java.sql.Statement;
  198.  
  199. public class myDAO {
  200.  
  201. //static Connection con = null;
  202. //static ResultSet rs = null;
  203.  
  204.  
  205. public static GetSet login (GetSet bean) {
  206.  
  207. Connection con = null;
  208. PreparedStatement ps = null;
  209. ResultSet rs =null;
  210. String uname = bean.getUname();
  211. String pword = bean.getPword();
  212.  
  213. String searchQuery = "select *from csusers where username=? AND password=?";
  214. //String searchQuery = "select username,password *from csusers";
  215.  
  216.  
  217.  
  218.  
  219. System.out.println("Your username is: " + uname);
  220. System.out.println("Your password is: " + pword);
  221. //System.out.println("Your query is: " + searchQuery);
  222.  
  223. try {
  224.  
  225. con = konekMoko.getCon();
  226. ps = con.prepareStatement(searchQuery);
  227. rs = ps.executeQuery();
  228.  
  229. ps.setString(1, uname);
  230. ps.setString(2, pword);
  231. boolean tochek =rs.next();
  232.  
  233. if (!tochek) // checks if username already existing
  234. {
  235.  
  236. System.out.println("Username does not exists!");
  237. bean.setisValid(false);
  238.  
  239. }
  240.  
  241. else if (tochek) //if already existing
  242. {
  243.  
  244. System.out.println("Welcome" + uname);
  245. bean.setUname(uname);
  246. bean.setPword(pword);
  247. bean.setisValid(true);
  248.  
  249. }
  250. rs.close();
  251. ps.close();
  252. con.close();
  253.  
  254. }
  255.  
  256. catch (Exception e) {
  257.  
  258.  
  259. }
  260. return bean;
  261.  
  262. }
  263.  
  264.  
  265. /*public static Connection getCon() throws Exception {
  266. try {
  267.  
  268. String driver = "com.mysql.jdbc.Driver";
  269. String url = "jdbc:mysql://localhost:3306/mydb";
  270. String username= "root";
  271. String password = "root";
  272. Class.forName(driver);
  273.  
  274. Connection conn= DriverManager.getConnection(url, username, password);
  275. System.out.println("You are now Connected!!");
  276. return conn; // Return if it is successfully connected!
  277.  
  278.  
  279.  
  280. }
  281. catch (Exception e)
  282. {
  283.  
  284. System.out.println("Connection not Established!"+e.getMessage());
  285. }
  286.  
  287.  
  288. return null; // Return if unsuccessful
  289. }
  290. */
  291.  
  292.  
  293. }
Add Comment
Please, Sign In to add comment