Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package p;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class LoginServlet extends HttpServlet {
- private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
- public void init(ServletConfig config) throws ServletException {
- super.init(config);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType(CONTENT_TYPE);
- PrintWriter out = response.getWriter();
- String connectionURL = "jdbc:mysql://localhost/userinfo";
- Connection connection = null;
- String username = request.getParameter("username");
- String password = request.getParameter("pword");
- String dbuid = "root";
- String dbpw = "ppoo11";
- //next try is for database driver and connection
- try{
- //load the database driver
- Class.forName("com.mysql.jdbc.Driver");
- //start connection
- connection = DriverManager.getConnection
- (connectionURL, dbuid,dbpw);
- //create a statement object
- Statement stmt = connection.createStatement();
- // exectue a query and return results to rs
- //this query selects the password field from the user
- ResultSet rs = stmt.executeQuery(
- "SELECT password FROM user WHERE iduser = "
- + username);
- if ((password = rs.getString("password"))) {
- //start session tracking here
- //login successful
- //redirect to correct pages
- } else {
- //error message
- }
- }
- catch (ClassNotFoundException e) {
- }
- catch (SQLException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement