0

Hi all,
I've this following piece of code. It is a simple hands on exercise for my learning.
I retrieve data from a table called cat and what i would like to do is , go to the action page only if there is a record for that username and password.
Also i get an error while trying to use textbox names or id's inside java tags of JSP. Is there a way to access those elements.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.io.*,javax.servlet.ServletConfig,javax.servlet.ServletException,javax.servlet.http.HttpServlet,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse"%>
<!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=ISO-8859-1">
<title>Employee Login Page</title>
</head>
<body>
<form method = "POST" action = "http://localhost:8080/EmployeeSite/FetchEmployee">
User Name  <input type = text name = UName id = UName/><br /><br />
 Password  <input type = password name = Pswd id = Pswd /><br /><br />
 <button type = Submit>Submit </button>
 <button type = Submit>Cancel </button>
 <%
 try{
	 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		Connection cn=DriverManager.getConnection("jdbc:odbc:unisys");
		String s1="select * from Cat where cid='"+Pswd+"' and cname ='"+UName+"'";
		Statement st=cn.createStatement();
		ResultSet rs= st.executeQuery(s1);
		if(rs.next())
		{
                  //validates it since there is a record.
		}
                else
                {
                  //no record found.
                }

 }
 catch(Exception e)
 {
	 e.printStackTrace();
 }%>
</form>
</body>
</html>

Edited by adarshcu: n/a

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by Shanti C
0

Also can i provide a different action in case Cancel button is selected .?

0

Also i get an error while trying to use textbox names or id's inside java tags of JSP. Is there a way to access those elements.

you have to write like this:

String pword=request.getParameter("Pswd");
String uname=request.getParameter("UNam");

then your query should be like:

String s1="select * from Cat where cid='"+pword+"' and cname ='"+uname+"'";

and

I retrieve data from a table called cat and what i would like to do is , go to the action page only if there is a record for that username and password.

see this:

if(rs.next())
{
// redirect to action page..
}

Edited by Shanti C: n/a

0

Ok, i thought the request object could be used only in the next redirected page.

According to your second direction, you mean to say the form action tag should not be used and i use a request.forward method of request dispatcher object.?

Edited by adarshcu: n/a

0

yes,
check this:

if(rs.next())
		{
                  response.sendRedirect("someUrl");
		}
                else
                {
                  //no record found.
                }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.