I just want to write a sample jsp, the first jsp allow the user to input their name and the password. If the name and the password is found, then the second jsp will display the annual leave balance.
I use the cookies method to do that. The first jsp searchs the database to find their name and the password. The first jsp works fine. But the second jsp cannot search the other database, it seems that the cookie cannot contain the right parameter (name). So the jsp cannot seek the right record base on the parameter (name).
Sometime the above jsp work fine, but sometimes the second jsp cannot find the record in the database.
below pls find the jsp
login.jsp
<%@ page import='java.sql.*' %>
<html>
<body>
<%
if (request.getParameter("userid" !=null) {
String uid = request.getParameter("userid"
String pin = request.getParameter("userpin"
// Connect ODBC Access
//Specify the Driver type: JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
//Make the connection through ODBC DSN
Connection conn = DriverManager.getConnection("jdbcdbc:userinfo"
//check user and password
//Prepare a Statement: allow move the record point up and down freely
Statement stmt = conn.createStatement();
//Execute a Query and get the ResultSet
String sqlstat = "select * from userinfo where login = '" + uid + "' and password = '"+pin + "'";
ResultSet rs = stmt.executeQuery(sqlstat);
if (rs.next()) {
out.println("user = " + rs.getString("login");
out.println("<p> </p>"
out.println("password = " + rs.getString("password");
out.println("<p> </p>"
out.println("detail = " + rs.getString("detail");
session.putValue("username", uid);
Cookie cookie = new Cookie("v_1",uid);
cookie.setMaxAge(30);
response.addCookie(cookie);
response.sendRedirect("second.jsp"
}
else { out.println("<h1>login failure</h1>"
}
}
else {
%>
<h2> login checking </h2>
<hr/>
<form method="post">
name :<input type="text" name="userid" size = "30" /> <br/>
<br/>
pin:<input type ="password" name ="userpin" size="10" /> <br/>
<br/>
<input type ="submit" value="login"/>
</form>
<% } %>
</body>
</html>
second jsp
<%@ page import='java.sql.*' %>
<html>
<title> get cookie </title>
<body>
<%@page errorPage ="error.jsp"
contentType= "text/html;charset=Big5"
%>
<%
Cookie cookies[] = request.getCookies();
int i;
String Idx;
String Value = " ";
int sickbal;
if (cookies == null)
{
out.println("<H1> Please go to login.jsp</H1>"
throw new Exception ("Please go to login.jsp"
}
int count = cookies.length;
if (count < 1) {
out.println("<H1> Please go to login.jsp</H1>"}
else
{out.println("<H1> Login success</H1>"
String ipaddress = request.getRemoteAddr();
out.println("IP " + ipaddress);
for (i=0;i<cookies.length;i++)
{
Idx = cookies.getName();
if (Idx.equals("v_1")
{
out.println("<BR>"
out.println(i);
Value = cookies.getValue();
out.println("<BR>"
out.println(">"+Value+"<"
}
}
/* String title[]= session.getValueNames();
int varcount = title.length;
for (int j ; j <= varcount -1; j++)
{
Object data = session.getValue(title[j]);
if (title[j] == "username"
{Object test = data;}
}
*/
//Specify the Driver type: JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
//Make the connection through ODBC DSN
Connection conn = DriverManager.getConnection("jdbcdbcersonal"
//check user and password
//Prepare a Statement: allow move the record point up and down freely
Statement stmt = conn.createStatement();
//Execute a Query and get the ResultSet
String sqlstat = "select * from personal where name = '" + Value + "'";
ResultSet rs = stmt.executeQuery(sqlstat);
if (rs.next()) {
out.println("<BR>"
out.println("get personal ok"
out.println("<BR>"
sickbal = rs.getInt("sickbal"
out.println(sickbal + 1 );
}
else
{out.println("get personal not ok"}
}
%>
</body>
</html>
I use the cookies method to do that. The first jsp searchs the database to find their name and the password. The first jsp works fine. But the second jsp cannot search the other database, it seems that the cookie cannot contain the right parameter (name). So the jsp cannot seek the right record base on the parameter (name).
Sometime the above jsp work fine, but sometimes the second jsp cannot find the record in the database.
below pls find the jsp
login.jsp
<%@ page import='java.sql.*' %>
<html>
<body>
<%
if (request.getParameter("userid" !=null) {
String uid = request.getParameter("userid"
String pin = request.getParameter("userpin"
// Connect ODBC Access
//Specify the Driver type: JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
//Make the connection through ODBC DSN
Connection conn = DriverManager.getConnection("jdbcdbc:userinfo"
//check user and password
//Prepare a Statement: allow move the record point up and down freely
Statement stmt = conn.createStatement();
//Execute a Query and get the ResultSet
String sqlstat = "select * from userinfo where login = '" + uid + "' and password = '"+pin + "'";
ResultSet rs = stmt.executeQuery(sqlstat);
if (rs.next()) {
out.println("user = " + rs.getString("login");
out.println("<p> </p>"
out.println("password = " + rs.getString("password");
out.println("<p> </p>"
out.println("detail = " + rs.getString("detail");
session.putValue("username", uid);
Cookie cookie = new Cookie("v_1",uid);
cookie.setMaxAge(30);
response.addCookie(cookie);
response.sendRedirect("second.jsp"
}
else { out.println("<h1>login failure</h1>"
}
}
else {
%>
<h2> login checking </h2>
<hr/>
<form method="post">
name :<input type="text" name="userid" size = "30" /> <br/>
<br/>
pin:<input type ="password" name ="userpin" size="10" /> <br/>
<br/>
<input type ="submit" value="login"/>
</form>
<% } %>
</body>
</html>
second jsp
<%@ page import='java.sql.*' %>
<html>
<title> get cookie </title>
<body>
<%@page errorPage ="error.jsp"
contentType= "text/html;charset=Big5"
%>
<%
Cookie cookies[] = request.getCookies();
int i;
String Idx;
String Value = " ";
int sickbal;
if (cookies == null)
{
out.println("<H1> Please go to login.jsp</H1>"
throw new Exception ("Please go to login.jsp"
}
int count = cookies.length;
if (count < 1) {
out.println("<H1> Please go to login.jsp</H1>"}
else
{out.println("<H1> Login success</H1>"
String ipaddress = request.getRemoteAddr();
out.println("IP " + ipaddress);
for (i=0;i<cookies.length;i++)
{
Idx = cookies.getName();
if (Idx.equals("v_1")
{
out.println("<BR>"
out.println(i);
Value = cookies.getValue();
out.println("<BR>"
out.println(">"+Value+"<"
}
}
/* String title[]= session.getValueNames();
int varcount = title.length;
for (int j ; j <= varcount -1; j++)
{
Object data = session.getValue(title[j]);
if (title[j] == "username"
{Object test = data;}
}
*/
//Specify the Driver type: JDBC-ODBC bridge driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"
//Make the connection through ODBC DSN
Connection conn = DriverManager.getConnection("jdbcdbcersonal"
//check user and password
//Prepare a Statement: allow move the record point up and down freely
Statement stmt = conn.createStatement();
//Execute a Query and get the ResultSet
String sqlstat = "select * from personal where name = '" + Value + "'";
ResultSet rs = stmt.executeQuery(sqlstat);
if (rs.next()) {
out.println("<BR>"
out.println("get personal ok"
out.println("<BR>"
sickbal = rs.getInt("sickbal"
out.println(sickbal + 1 );
}
else
{out.println("get personal not ok"}
}
%>
</body>
</html>