Sir i 3 jsp pages...first one home.jsp
form name="form" class="sf-menu ul" method="post" action="check.jsp"> <table border="10"> <tr><td background="orange">Username:</td><td><input type="text" name="username"></td></tr> <tr><td>Password:</td><td><input type="password" name="password"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> <a href="register.jsp"><img alt="doit" src="button_jointoday.png" height="60",width="60"></a> <marquee class="c">this is naveen website</marquee> </form> </body> </html>
then **check.jsp"
<%@page import="java.sql.*" import =" javax.servlet.RequestDispatcher; "%> <% String username=request.getParameter("username"); String password=request.getParameter("password"); Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/naveen","root","naveen"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from myaccount where username='"+username+"' and password='"+password+"'"); int count=0; while(rs.next()) { count++; } if(count>0) { response.sendRedirect("detail.jsp"); } else { response.sendRedirect("home.jsp?msg=Invalid Username or Password"); } %>
and third one is details.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import ="java.sql.*" import = "java.io.*" import=" javax.servlet.RequestDispatcher; "%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String username=request.getParameter("username"); String password=request.getParameter("password"); PrintWriter pw = response.getWriter(); pw.println(username); pw.println(password);
now the problem is if i enter username and password the values are going to check.jsp due to action command ...but the values i entered is not able to get my details.jsp..
in check.jsp itself i can able to print my username and password but i want to display in third page only ple help me out...
ple how can i get values from firstjsppage to thirdjsp but action is mentioned for second jsp...any method is there any idea ple help me..both are in same package
You can use request.setAttribute()or session.setAttribute().
In check.jsp:
<%@page import="java.sql.*" %> <%@import ="javax.servlet.RequestDispatcher"%> <% String username=request.getParameter("username"); String password=request.getParameter("password"); Class.forName("com.mysql.jdbc.Driver"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/naveen","root","naveen"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from myaccount where username='"+username+"' and password='"+password+"'"); int count=0; while(rs.next()){ count++; } if(count>0){ request.setAttribute("user",username); request.setAttribute("pass",password); RequestDispatcher dispatcher = request.getRequestDispatcher("detail.jsp"); if (dispatcher != null){ dispatcher.forward(request, response); } } else { response.sendRedirect("home.jsp?msg=Invalid Username or Password"); } %>
In details.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import ="java.sql.*" import = "java.io.*" import="javax.servlet.RequestDispatcher;"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String username=request.getAttribute("user"); String password=request.getParameter("pass"); PrintWriter pw = response.getWriter(); pw.println(username); pw.println(password);
%>
For more information, visit the following links: