Koushik Reddy Koushik Reddy -4 years ago 97
MySQL Question

My questions is how to direct from one jsp page to another jsp page?

This is my login.jsp page

<%@ page import ="java.sql.*" %>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<!DOCTYPE html>
<html>
<style>
/* Full-width input fields */
input[type=text], input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}

/* Set a style for all buttons */
button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}

/* Extra styles for the cancel button */
.cancelbtn {
padding: 14px 20px;
background-color: #f44336;
}

/* Float cancel and signup buttons and add an equal width */
.cancelbtn,.signupbtn {
float: left;
width: 50%;
}

/* Add padding to container elements */
.container {
padding: 16px;
}

/* Clear floats */
.clearfix::after {
content: "";
clear: both;
display: table;
}

/* Change styles for cancel button and signup button on extra small screens */
@media screen and (max-width: 300px) {
.cancelbtn, .signupbtn {
width: 100%;
}
}
</style>
<head>
<meta charset="ISO-8859-1">
<title>Registration Form</title>
</head>
<form method="post" action="success.jsp">

<div class="container">
<h2>Login Form</h2>
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="Username" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="Password" required>
<div class="clearfix">
<button type="submit" value="submit">Login</button>

</div>
</div>
</html>
<%
String Username= request.getParameter("Username");
String Password = request.getParameter("Password");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
Statement st = con.createStatement();
ResultSet rs;
rs=st.executeQuery("SELECT * FROM customers WHERE Username='" + Username + "' and Password='" + Password + "'");
if (rs.next()) {
session.setAttribute("Username",Username);
// out.println("Welcome " + Username);
//out.println("<a href='logout.jsp'>Log out</a>");
response.sendRedirect("success.jsp");
}
else
{
out.println("Invalid Username or password Please try again");
}
%>
I am using eclipse,tomcat,mysql db.When i run login.jsp page it is asking me validation details and total code works fine but in output page i am getting
O/P:Welcome null.It is throwing me null value.And also it is directly going to else loop and giving me output in the output page like this "Invalid Username or Password Please try again".I used both HTML and java code in jsp file is that correct???

success.jsp:

<%
if ((session.getAttribute("Username") == "null") || (session.getAttribute("Password") =="null")) {
%>
You have not logged in<br/>
<a href="login.jsp">Please Login</a>
<%} else {
%>
Welcome <%=session.getAttribute("Username")%>
<a href='logout.jsp'>Log out</a>
<%
}
%>


Welcome <%=session.getAttribute("Username")%>

//Expected output Welcome rohan542
The login.jsp has direct from login.jsp to success.jsp.In other words it has to direct from login page to success page.There is no stacktrace for this, iam getting output but not what i am expecting.I think the code has little bit mistakes please correct it.I am using sessions in java.The code has to check for whether the Username and Password are correct if they are correct then it has to validate and go to success page and beside that i provided a logout link to directly logout there

Answer Source

You can use

<c:redirect url = "your-url"/>

tag to redirect to a newer url.

As your code is in the same page, your action should not point to success.jsp page. It should point the same login page. Please modify the question description to avoid confusion.

Please change the following line.

 <form method="post" action="success.jsp"> to
 <form method="post" action="login.jsp">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download