i want to design one form with username and password,login...if i click login the jsp check the database table with all username and password field if its find means the link goes to my personalinfo.html...i am confused how to check enterted username with all username fields and how to transfer link to another html if i find..ple guide me
1)login.jsp:
<html> <script> function validate(){ var username=document.form.user.value; var password=document.form.pass.value; if(username==""){ alert("Enter Username!"); return false; } if(password==""){ alert("Enter Password!"); return false; } return true; } </script> <form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();"> <table> <tr><td>Username:</td><td><input type="text" name="user"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html> <%String msg=request.getParameter("msg"); if(msg!=null){ %> <label><font color="red"><%=msg%></font></label> <% } %>
2)check.jsp:
<%@page import="java.sql.*"%> <% String user=request.getParameter("user"); String pass=request.getParameter("pass"); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'"); int count=0; while(rs.next()) { count++; } if(count>0) { out.println("welcome "+user); } else { response.sendRedirect("login.jsp?msg=Invalid Username or Password"); } %>
1)login.jsp:
<html> <script> function validate(){ var username=document.form.user.value; var password=document.form.pass.value; if(username==""){ alert("Enter Username!"); return false; } if(password==""){ alert("Enter Password!"); return false; } return true; } </script> <form name="form" method="post" action="http://localhost:8080/examples/Login" onsubmit="javascript:return validate();"> <table> <tr><td>Username:</td><td><input type="text" name="user"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
2)Login.java:
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Login extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ response.setContentType("text/html"); PrintWriter out=response.getWriter(); String user=request.getParameter("user"); String pass=request.getParameter("pass"); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'"); int count=0; while(rs.next()) { count++; } if(count>0) { out.println("welcome "+user); } else { response.sendRedirect("/examples/jsp/login.jsp"); } } catch (Exception e) { e.printStackTrace(); } } }