Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I just want to write a sample jsp,

Status
Not open for further replies.

hksr

MIS
Sep 26, 2003
30
HK
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(&quot;userid&quot;) !=null) {
String uid = request.getParameter(&quot;userid&quot;);
String pin = request.getParameter(&quot;userpin&quot;);


// Connect ODBC Access

//Specify the Driver type: JDBC-ODBC bridge driver
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);

//Make the connection through ODBC DSN
Connection conn = DriverManager.getConnection(&quot;jdbc:eek:dbc:userinfo&quot;);

//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 = &quot;select * from userinfo where login = '&quot; + uid + &quot;' and password = '&quot;+pin + &quot;'&quot;;
ResultSet rs = stmt.executeQuery(sqlstat);

if (rs.next()) {
out.println(&quot;user = &quot; + rs.getString(&quot;login&quot;));
out.println(&quot;<p> </p>&quot;);
out.println(&quot;password = &quot; + rs.getString(&quot;password&quot;));
out.println(&quot;<p> </p>&quot;);
out.println(&quot;detail = &quot; + rs.getString(&quot;detail&quot;));

session.putValue(&quot;username&quot;, uid);
Cookie cookie = new Cookie(&quot;v_1&quot;,uid);
cookie.setMaxAge(30);
response.addCookie(cookie);






response.sendRedirect(&quot;second.jsp&quot;);

}
else { out.println(&quot;<h1>login failure</h1>&quot;);
}
}
else {
%>

<h2> login checking </h2>
<hr/>
<form method=&quot;post&quot;>
name :<input type=&quot;text&quot; name=&quot;userid&quot; size = &quot;30&quot; /> <br/>
<br/>
pin:<input type =&quot;password&quot; name =&quot;userpin&quot; size=&quot;10&quot; /> <br/>
<br/>
<input type =&quot;submit&quot; value=&quot;login&quot;/>
</form>

<% } %>
</body>
</html>


second jsp

<%@ page import='java.sql.*' %>
<html>
<title> get cookie </title>
<body>
<%@page errorPage =&quot;error.jsp&quot;
contentType= &quot;text/html;charset=Big5&quot;
%>
<%
Cookie cookies[] = request.getCookies();
int i;
String Idx;
String Value = &quot; &quot;;
int sickbal;

if (cookies == null)

{
out.println(&quot;<H1> Please go to login.jsp</H1>&quot;);
throw new Exception (&quot;Please go to login.jsp&quot;);
}

int count = cookies.length;

if (count < 1) {
out.println(&quot;<H1> Please go to login.jsp</H1>&quot;);}
else
{out.println(&quot;<H1> Login success</H1>&quot;);
String ipaddress = request.getRemoteAddr();
out.println(&quot;IP &quot; + ipaddress);

for (i=0;i<cookies.length;i++)
{

Idx = cookies.getName();

if (Idx.equals(&quot;v_1&quot;))

{
out.println(&quot;<BR>&quot;);
out.println(i);
Value = cookies.getValue();
out.println(&quot;<BR>&quot;);
out.println(&quot;>&quot;+Value+&quot;<&quot;);
}


}




/* String title[]= session.getValueNames();
int varcount = title.length;
for (int j ; j <= varcount -1; j++)
{
Object data = session.getValue(title[j]);
if (title[j] == &quot;username&quot;)
{Object test = data;}


}

*/


//Specify the Driver type: JDBC-ODBC bridge driver
Class.forName(&quot;sun.jdbc.odbc.JdbcOdbcDriver&quot;);

//Make the connection through ODBC DSN
Connection conn = DriverManager.getConnection(&quot;jdbc:eek:dbc:personal&quot;);

//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 = &quot;select * from personal where name = '&quot; + Value + &quot;'&quot;;
ResultSet rs = stmt.executeQuery(sqlstat);

if (rs.next()) {

out.println(&quot;<BR>&quot;);
out.println(&quot;get personal ok&quot;);
out.println(&quot;<BR>&quot;);
sickbal = rs.getInt(&quot;sickbal&quot;);
out.println(sickbal + 1 );
}

else
{out.println(&quot;get personal not ok&quot;);}


}

%>

</body>
</html>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top