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>

Recommended Answers

All 4 Replies

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

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..
}

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.?

yes,
check this:

if(rs.next())
		{
                  response.sendRedirect("someUrl");
		}
                else
                {
                  //no record found.
                }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.