Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //GetSet.java <-----------used as beans
- package thisPackage;
- public class GetSet {
- private String uname;
- private String pword;
- public boolean ifvalid;
- public String getUname () {
- return uname;
- }
- public void setUname (String myuname) {
- this.uname=myuname;
- }
- public String getPword () {
- return pword;
- }
- public void setPword (String mypword) {
- this.pword=mypword;
- }
- public boolean isValid() {
- return ifvalid;
- }
- public void setisValid (boolean validity)
- {
- this.ifvalid=validity;
- }
- package thisPackage;
- import java.sql.Connection;
- import java.sql.DriverManager;
- public class konekMoko {
- static Connection con;
- static String url;
- public static Connection getCon () throws Exception {
- try {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/csdb";
- String username= "root";
- String password = "root";
- Class.forName(driver);
- //System.out.println("HAHAHAHA");
- Connection conn= DriverManager.getConnection(url, username, password);
- System.out.println("You are now Connected!!");
- return conn; // Return if it is successfully connected!
- }
- catch (Exception e)
- {
- e.printStackTrace();
- //System.out.println("Connection not Established! "+e.getMessage());
- }
- return null; // Return if unsuccessful
- }
- }
- /*String url = "jdbc:mysql://localhost:3306/csdb";
- Class.forName("jdbc:mysql://localhost:3306/csdb");
- try {
- con = DriverManager.getConnection(url, "username", "password");
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return con;*/
- //LoginServlet.java
- package thisPackage;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- /**
- * Servlet implementation class LoginServlet
- */
- public class LoginServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter post = response.getWriter();
- try
- {
- GetSet gsup = new GetSet();
- gsup.setUname(request.getParameter("username"));
- gsup.setPword(request.getParameter("password"));
- //System.out.println("HAHAHAHAHA");
- gsup = myDAO.login(gsup);
- if (gsup.isValid())
- {
- post.print("Welcome" + gsup);
- HttpSession mysession = request.getSession(true);
- mysession.setAttribute("thiscurrentsuser", gsup);
- mysession.setMaxInactiveInterval(120); // 2 mins stay on the page
- response.sendRedirect("loggedin.jsp"); // the user will be redirected to the loggedin page if true
- }
- else
- {
- response.sendRedirect("thisinvaliduser.jsp"); //if it is not valid it will throw you to the invalid log page
- }
- }
- catch (Throwable e){
- System.out.println(e);
- // TODO Auto-generated method stub
- doGet(request, response);
- }
- }
- }
- //MyDAO.java
- package thisPackage;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- //import java.sql.Statement;
- public class myDAO {
- //static Connection con = null;
- //static ResultSet rs = null;
- public static GetSet login (GetSet bean) {
- Connection con = null;
- PreparedStatement ps = null;
- ResultSet rs =null;
- String uname = bean.getUname();
- String pword = bean.getPword();
- String searchQuery = "select *from csusers where username=? AND password=?";
- //String searchQuery = "select username,password *from csusers";
- System.out.println("Your username is: " + uname);
- System.out.println("Your password is: " + pword);
- //System.out.println("Your query is: " + searchQuery);
- try {
- con = konekMoko.getCon();
- ps = con.prepareStatement(searchQuery);
- rs = ps.executeQuery();
- ps.setString(1, uname);
- ps.setString(2, pword);
- boolean tochek =rs.next();
- if (!tochek) // checks if username already existing
- {
- System.out.println("Username does not exists!");
- bean.setisValid(false);
- }
- else if (tochek) //if already existing
- {
- System.out.println("Welcome" + uname);
- bean.setUname(uname);
- bean.setPword(pword);
- bean.setisValid(true);
- }
- rs.close();
- ps.close();
- con.close();
- }
- catch (Exception e) {
- }
- return bean;
- }
- /*public static Connection getCon() throws Exception {
- try {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://localhost:3306/mydb";
- String username= "root";
- String password = "root";
- Class.forName(driver);
- Connection conn= DriverManager.getConnection(url, username, password);
- System.out.println("You are now Connected!!");
- return conn; // Return if it is successfully connected!
- }
- catch (Exception e)
- {
- System.out.println("Connection not Established!"+e.getMessage());
- }
- return null; // Return if unsuccessful
- }
- */
- }
Add Comment
Please, Sign In to add comment