Question
I need help to modify these code on netbeans,so when it can get data from database,for instance ,i use the id and password i need
I need help to modify these code on netbeans,so when it can get data from database,for instance ,i use the id and password i need to be able to login,id is admin,password is 1234,attach a screenshot of final output,i have the login.html and the loginservlet.java
Login Here
Customer ID: Password:
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
/** * Processes requests for both HTTP GET
and POST
* methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.println("LoginServlet Running ");
String id = request.getParameter("id"); String pass = request.getParameter("password");
if (checkUser(id, pass)) { out.println("Valid Login"); RequestDispatcher rs = request.getRequestDispatcher("welcome.html"); rs.forward(request, response); } else { out.println("InValid Login "); RequestDispatcher rs = request.getRequestDispatcher("Login.html"); rs.include(request, response); }
} }
/** * Handles the user validation method. * * @param id String user id request * @param pass user password request * @return true if valid user, otherwise false */ public static boolean checkUser(String id, String pass) { boolean valid = false; try { //loading drivers for mysql Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:ChattBankMDB"); PreparedStatement ps = con.prepareStatement("SELECT * FROM customers WHERE UserID=? and Passwd=?"); ps.setString(1, id); ps.setString(2, pass); ResultSet rs = ps.executeQuery(); if (rs.next()) { valid = rs.next(); }
} catch (ClassNotFoundException | SQLException e) { System.out.println("connected to database" + e); } return valid; }
// GET
method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
/** * Handles the HTTP POST
method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); }
/** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }//
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started