Full Java User Login code
Login. Web effects phttp://www.111cn.net/web effects p.html target=_blank >jsp Tutorial login, enter the database tutorial in the saved user name and password why not jump to the login_success.jsp page?
There are three fields in the person list: Id,name,password.
LOGIN.JSP:
<%@ page contenttype= "text/html;charset=gb2312"%>
<body>
<center>
<form action= "login_conf.jsp" method= "POST" >
<table width= "50%" border= "1" >
<tr>
<TD colspan= "2" > User login </td>
</tr>
<tr>
<td> User name:</td>
<td><input type= "text" name= "id" ></td>
</tr>
<tr>
<td> Secret Code:</td>
<td><input type= "password" name= "password" ></td>
</tr>
<tr>
<TD colspan= "2" >
<input type= "Submit" value= "Login" >
<input type= "reset" value= "reset" >
</td>
</tr>
</table>
</form>
</body>
LOGIN_CONF.JSP:
<%@ page contenttype= "text/html;charset=gb2312"%>
<%@ page import= "java.sql.*"%>
<body>
<% out.print ("oksssss");
String dbdriver = "Oracle.jdbc.driver.oracledriver";
String dburl = "Jdbc:oracle:thin: @localhost: 1521:js";
String dbuser = "Scott";
String Dbpassword = "Tiger";
Connection conn =null;
PreparedStatement PS Tutorial TMT =null;
ResultSet Rs =null;
%>
<%
Boolean flag = false;
String id=request.getparameter ("id");
String password = reques T.getparameter ("password");
%>
<%
String sql = select name from person values id =? and password =? ";
//Out.print (flag);
Try
{
class.forname (dbdriver);
Conn=drivermanager.getconnection (Dburl,dbuser, Dbpassword);
pstmt=conn.preparestatement (SQL);
Pstmt.setstring (1,id);
Pstmt.setstring (2,password);
Rs=pstmt.executequery ();
if (Rs.next ())
{
Flag=true;
Session.setattribute ("Uname", rs.getstring (1));
}
Else
{
Request.setattribute ("Err", "wrong username or password!") ");
}
Rs.close ();
Pstmt.close ();
Conn.close ();
}
catch (Exception e)
{
}
%>
<%
if (flag)
{
%>
<jsp:forward page= "login_success.jsp"/>
<%
}
Else
{
%>
<jsp:forward page= "login.jsp"/>
<%
}
%>
</body>
LOGIN_SUCCESS.JSP:
<%@ page contenttype= "text/html;charset=gb2312"%>
<body>
<%
if (Session.getattribute ("uname")!=null);
{
%>
<font color= "#ff0000" size= "a" >
<%=session.getattribute ("uname")%>
</font>
Visit MLDN message!
<%
}
Else
{
Response.setheader ("Refresh", "2;url=login.jsp");
%>
You have not logged in, please <a href= "login.jsp" > Login! </a>
<%
}
%>
</body>