How to redirect to a jsp page from a servlet by using Database values



  • well I have a jsp page with a login form, i'm using a servlet, if the email and password are correct the servlet redirects the user to another page else it redirects him to the login page again with alert.

    when i log in with a correct email and password i'm redirected perfectly to index.jsp
    but when i put a wrong email or password in the form when i click on the submit button the page redirecting to the index.jsp. How can i sort out this

    here is the servlet

    package Company;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.*;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    /**
     * Servlet implementation class LogServlet
     */
    @WebServlet("/LogServlet")
    public class CompanyLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    
    
    
    public CompanyLoginServlet() {
        super();
    
    }
    
    
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.setContentType("text/html");
    
    
        String email=request.getParameter("email");
        String password=request.getParameter("pass");
    
        PreparedStatement ps = null;
        ResultSet rs = null;
    
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/javaee?useSSL=true", "root", "");
            ps = (PreparedStatement) conn.prepareStatement("select emp_Email,Emp_Password from employer where emp_Email = ? and Emp_Password = ?");
    
            ps.setString(1, email);
            ps.setString(2, password);
            rs=ps.executeQuery();
    
    
    
        try {
            while(rs.next()){
            if(email.equals(rs.getString(3)) && password.equals(rs.getString(4))){
                response.sendRedirect("../../Company/index/index.jsp");
            }
            else{
                response.sendRedirect("CompanyLogin.jsp");
    
            }}
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    
        }
        System.out.close();
    
    
        }
        catch(Exception e){e.printStackTrace();}
    
    
    }
    }
    

    here is my JSP

    <%-- 
        Document   : CompanyLogin.jsp
        Created on : Feb 24, 2019, 9:42:17 PM
        Author     : Akash
    --%>
    
    <%@page import="java.sql.Statement"%>
    <%@page import="java.sql.Connection"%>
    <%@page import="java.sql.ResultSet"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<title>Login V18</title>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1">
    <!--===============================================================================================-->	
    	<link rel="icon" type="image/png" href="images/icons/favicon.ico"/>
    <!--===============================================================================================-->
    	<link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
    <!--===============================================================================================-->
    	<link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
    <!--===============================================================================================-->
    	<link rel="stylesheet" type="text/css" href="fonts/Linearicons-Free-v1.0.0/icon-font.min.css">
    <!--===============================================================================================-->
    	<link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
    <!--===============================================================================================-->	
    	<link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
    <!--===============================================================================================-->
    	<link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">
    <!--===============================================================================================-->
    	<link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
    <!--===============================================================================================-->	
    	<link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">
    <!--===============================================================================================-->
    	<link rel="stylesheet" type="text/css" href="css/util.css">
    	<link rel="stylesheet" type="text/css" href="css/main.css">
    <!--===============================================================================================-->
    </head>
    <body style="background-color: #666666;">
    	
        <div class="limiter">
    	<div class="container-login100">
                <div class="wrap-login100">
                    <form class="login100-form validate-form" method="POST" action="../../Company/index/index.jsp ">
                        <span class="login100-form-title p-b-43">
    			Login to continue
                        </span>
    					
    					
                        <div class="wrap-input100 validate-input" data-validate = "Valid email is required: [email protected]">
    			<input class="input100" type="text" name="email">
                            <span class="focus-input100"></span>
                            <span class="label-input100">Email</span>
                        </div>
    					
    					
                        <div class="wrap-input100 validate-input" data-validate="Password is required">
    			<input class="input100" type="password" name="pass">
    			<span class="focus-input100"></span>
                            <span class="label-input100">Password</span>
                        </div>
    
                        <div class="flex-sb-m w-full p-t-3 p-b-32">
    			<div class="contact100-form-checkbox">
                                <input class="input-checkbox100" id="ckb1" type="checkbox" name="remember-me">
                                <label class="label-checkbox100" for="ckb1">
    				Remember me
                                </label>
    			</div>
    
    			<div>
                                <a href="#" class="txt1">
    				Forgot Password?
                                </a>
    			</div>
                        </div>
    			
    
                        <div class="container-login100-form-btn">
    			<button class="login100-form-btn">
                                Login
    			</button>
                        </div>
    		</form>
    
    		<div class="login100-more" style="background-image: url('images/bg-01.jpg');">
    		</div>
                </div>
    	</div>
        </div>
        
    
    	
    
    	
    	
    <!--===============================================================================================-->
    	<script src="vendor/jquery/jquery-3.2.1.min.js"></script>
    <!--===============================================================================================-->
    	<script src="vendor/animsition/js/animsition.min.js"></script>
    <!--===============================================================================================-->
    	<script src="vendor/bootstrap/js/popper.js"></script>
    	<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
    <!--===============================================================================================-->
    	<script src="vendor/select2/select2.min.js"></script>
    <!--===============================================================================================-->
    	<script src="vendor/daterangepicker/moment.min.js"></script>
    	<script src="vendor/daterangepicker/daterangepicker.js"></script>
    <!--===============================================================================================-->
    	<script src="vendor/countdowntime/countdowntime.js"></script>
    <!--===============================================================================================-->
    	<script src="js/main.js"></script>
    
    </body>
    </html>
    

  • Web Development

    This post is deleted!

  • Web Development

    @akashmanujaya mchan getString(3) function ekata table column name eka deela balana

    Ex:

    getString("user_password")
    

Log in to reply