Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <a href="#"><button id="cus_login" class="info" type="button">Login</button></a>
- $(document).ready(function() {
- $('#cus_login').click(function() {
- var username = $('#cus_name').val();
- var pw = $('#cus_password').val();
- var method = "one";
- $.ajax({
- type: "POST",
- data: {
- method: method,
- username: username,
- pw: pw
- },
- url: 'LoginServlet',
- success: function(result) {
- if(result) {
- window.location = 'cust_profile.html'
- console.log(result);
- }
- else{
- alert("Login failure, please check and
- try again");
- }
- },
- error, function(result) {
- console.log(result);
- alert("Connection could not be established.
- Please try again.");
- }
- });
- });
- });
- import util.DBUtil;
- @WebServlet("/LoginServlet")
- public class LoginServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- DBUtil datasrc = new DBUtil();
- Connection conn = datasrc.getConn();
- protected void doPost(HttpServletRequest request, HttpServletResponse
- response) throws ServletException, IOException {
- response.setContentType("text/plain");
- System.out.println("--------------------------/nDSConn
- established, doGet method called");
- String method = request.getParameter("method");
- String username = request.getParameter("username");
- String pw = request.getParameter("pw");
- boolean finalstatus = false;
- boolean finalstatus1 = false;
- if(usernameCheck(username) == true && passwordCheck(pw) ==
- true) {
- switch(method) {
- case "1":
- finalstatus = custLogin(username, pw);
- case "2":
- finalstatus1 = busLogin(username, pw);
- }
- }
- if(finalstatus) {
- HttpSession session = request.getSession();
- session.setAttribute("username", username);
- response.sendRedirect("cust_profile.jsp");;
- }
- else if(finalstatus1) {
- HttpSession session = request.getSession();
- session.setAttribute("username", username);
- response.sendRedirect("bus_profile.jsp");
- }
- else {
- response.sendError(001, "Unsuccessful");
- }
- }
- private boolean usernameCheck(String username) {
- if(username.matches("^[0-9]*$") && username.length() == 8) {
- return true;
- }
- else return false;
- }
- private boolean passwordCheck(String pw) {
- if(pw.length() <= 15) {
- return true;
- }
- else return false;
- }
- private boolean custLogin(String un, String pw) {
- PreparedStatement cps = null;
- try {
- String custsql = ("select * from fyp where cust_id = "
- + un + " password = " + pw);
- cps = conn.prepareStatement(custsql);
- ResultSet rs = cps.executeQuery();
- while(rs.next()) {
- System.out.println("Login successful");
- return true;
- }
- }
- catch (Exception e) {
- System.out.println("Login failure: ");
- }
- return false;
- }
- }
- public class DBUtil extends HttpServlet {
- static DataSource ds=null;
- static {
- try {
- System.out.println("----------------------------------------------"
- + "/n DSConn initialised - method called to estabish DS");
- Context initContext = new InitialContext();
- Context envContext = (Context) initContext.lookup("java:/comp/env");
- ds = (DataSource) envContext.lookup("jdbc/fyp");
- System.out.println(ds + " DS has been established via JDNI lookup");
- }
- catch(Exception e) {
- System.out.println("Problem establishing DS");
- e.printStackTrace();
- }
- }
- public Connection getConn() {
- try {
- return ds.getConnection();
- }
- catch (SQLException e) {
- System.out.println("error");
- }
- return null;
- }
Add Comment
Please, Sign In to add comment