hinata
hinata

Reputation: 385

Redirect from JSP

I Created login page to retrieve MySQL database login info and check then with the user entered data. Then I want to redirect to home page if the entered data is in the database else create and error message with in the login page(or alert message). How can I implement this ?

I coded this far for the program; but It isn't complete;

<form   method="post" name="Login_Form" class="form-signin">       
    <h3 class="form-signin-heading">City Pharmacy</h3>
    <hr class="colorgraph"><br>
    <p> Login As Supplier </p>

    <input type="text" class="form-control" name="Username" placeholder="Username" required="" autofocus="" />
    <input type="password" class="form-control" name="Password" placeholder="Password" required=""/>          

    <button class="btn btn-lg btn-primary btn-block"  name="Submit" value="Login" type="Submit">Login</button>
    <br>
    <a href ="index2.jsp"> Login As Admin  </a>
    <%
    String user = request.getParameter("Username");
    session.putValue("Username",user);
    String pwd = request.getParameter("Password");
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost/pmsdb","dbuser","dbpassword");
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("select * from webappligin where username='"+user+"' and type = Supplier");
    if(rs.next()){
        if(rs.getString("password").equals(pwd)){
            //wantto go to home page
        }else{
            // create an error alert
        }

    }
    %>
</form>

Upvotes: 1

Views: 623

Answers (4)

Piyush Mittal
Piyush Mittal

Reputation: 1890

try following:
for alert message

        <%
            if(rs.getString("password").equals(pwd))
           {
               response.sendRedirect("homepage.jsp");  
            }else
                {
        %>
        <script language="JavaScript">
            alert("Incorrect Login Details");
        </script>
        <%
        }

        %>

for Error page:

          if(rs.getString("password").equals(pwd))
               response.sendRedirect("homepage.jsp");  
            else
                response.sendRedirect("errorpage.jsp");  

Upvotes: 0

wawek
wawek

Reputation: 1597

I think something like this should work:

    <%
    if(rs.getString("password").equals(pwd)) {
    %>
        <script language="javascript">
        window.location.replace("http://example.com");
        </script>
    <%
    } else { 
        <script language="javascript">
        alert("Password incorrect");
        </script>
    %>
    }

Upvotes: 1

Kiran
Kiran

Reputation: 159

Writing java code in JSP is very poor kind of programming according to my experience. Please try to make use of servlets along with jsp to separate your view from your logic. Although you are a beginner you should start with good examples and progarmming technics. Please go through this link.And if you want to use any conditions in jsp you should make use of JSTL.I would suggest you to go through this servlet tutorials.

Upvotes: 1

Anand Dwivedi
Anand Dwivedi

Reputation: 1500

No need to Do this much Coding only you define Action name on inside Form tag . so when you click on Login your control will forward to Homepage.here is the simple format to pass action inside form tag.

 <form action="Homepage.jsp" method="post">

or you can send the response as well from JSP Page using

response.sendRedirect("homepage.jsp");

you can validate your from field Using JS or you can use required="" tag for making all the fields as a mandatory

Happy to Help !

Upvotes: 2

Related Questions