Reputation: 27
Which will take input username and password ans pass it to validation.jsp
login.jsp
<center>
<table border="1" cellpadding="5" cellspacing="2">
<thead>
<tr>
<th colspan="2">Login Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>Username</td>
<td><input type="text" name="username" required/></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" required/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" />
<input type="reset" value="Reset" />
</td>
</tr>
</tbody>
</table>
</center>
</form>
</body>
</html>
validation.jsp
It will validate the data from the database and return;
<%@ page import ="java.sql.*" %>
<%
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver"); // MySQL database connection
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tejas?" + "user=root&password=tejas");
PreparedStatement pst = conn.prepareStatement("Select user,pass from login where user=tejas and pass=te65");
pst.setString(1, username);
pst.setString(2, password);
ResultSet rs = pst.executeQuery();
if(rs.next())
out.println("Valid login credentials");
else
out.println("Invalid login credentials");
}
catch(Exception e){
out.println("Something went wrong !! Please try again");
}
%>`
This are both the codes saved under webapps folder webapps folder contains web-inf login.jsp validation.jsp
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
</web-app>
do i need to add something to web.xml please help...
Upvotes: 1
Views: 50
Reputation: 59950
First
You setString
without any ?
in your query, you have to use, so if you want something dynamique you have to use :
pst = conn.prepareStatement("Select user,pass from login where user=? and pass=?");
//------------------------------------------------------------------^----------^
pst.setString(1, username);
pst.setString(2, password);
If you want the username and password fix, then you don't need to use .setString
.
Upvotes: 2