Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package p;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.*;
  6.  
  7. import javax.servlet.*;
  8. import javax.servlet.http.*;
  9.  
  10. public class LoginServlet extends HttpServlet {
  11.     private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
  12.  
  13.     public void init(ServletConfig config) throws ServletException {
  14.         super.init(config);
  15.     }
  16.  
  17.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  18.        
  19.         response.setContentType(CONTENT_TYPE);
  20.         PrintWriter out = response.getWriter();
  21.         String connectionURL = "jdbc:mysql://localhost/userinfo";
  22.         Connection connection = null;
  23.         String username = request.getParameter("username");
  24.         String password = request.getParameter("pword");
  25.         String dbuid = "root";
  26.         String dbpw = "ppoo11";
  27.         //next try is for database driver and connection
  28.         try{
  29.             //load the database driver
  30.             Class.forName("com.mysql.jdbc.Driver");
  31.             //start connection
  32.             connection = DriverManager.getConnection
  33.                 (connectionURL, dbuid,dbpw);
  34.             //create a statement object
  35.             Statement stmt = connection.createStatement();
  36.             // exectue a query and return results to rs
  37.             //this query selects the password field from the user
  38.             ResultSet rs = stmt.executeQuery(
  39.                 "SELECT password FROM user WHERE iduser = "
  40.                 + username);
  41.            
  42.             if ((password = rs.getString("password"))) {
  43.                 //start session tracking here
  44.                 //login successful
  45.                 //redirect to correct pages
  46.             } else {
  47.                 //error message
  48.             }
  49.         }
  50.         catch (ClassNotFoundException e) {
  51.            
  52.         }
  53.         catch (SQLException e) {
  54.        
  55.         }
  56.    
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement