JSP1:
<h1>
Struts框架的登陆
</h1><br />
<form method="post" action="untitled1Action.do">
<br><br>
<input type="text" name="userid" /><br />
<input type="password" name="password" /><br />
<input type="submit" name="Submit" value="提交">
<input type="reset" value="重置">
JSP2:
<center>
<h1>
恭喜登陆成功!
</h1><br />
<a href="jsp1.jsp">返回</a>
</center>
JSP3:
<center>
<h1>
登陆失败!
</h1><br />
<a href="jsp1.jsp">返回</a>
</center>
Conclass:
package untitled4;
import java.sql.*;
public class Conclass {
Connection con;
public Conclass() {
Connection con;
}
public boolean s(String userid,String password){
boolean k=false;
//String userId=userid;
//String passWord=password;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:li");
Statement st = con.createStatement();
String sql = "select * from userId where userid='" + userid +
"' and password='" + password+"'";
ResultSet rs = st.executeQuery(sql);
if (rs.next()) {
k = true;
} else {
k = false;
}
} catch (SQLException ex) {
} catch (ClassNotFoundException ex) {
}
return k;
}
}
Untitled1Action:
Untitled1ActionForm f = (Untitled1ActionForm) form;
Conclass ccs=new Conclass();
String userid=f.getUserid();
String password=f.getPassword();
boolean a=ccs.s(userid,password);
if(a){
System.out.println("chengong");
return mapping.findForward("jsp2");
}
else{
System.out.println("shibai");
return mapping.findForward("jsp3");
}
注意:在JSP里action="untitled1Action.do"提交的时候默认是action="/untitled1Action.do"形式,要把"/"去掉,不然untitled1Action.do页面白屏,不生成提交.还有sql语句的时候
正确:
String sql = "select * from userId where userid='" + userid +
"' and password='" + password+"'";
错误:
String sql = "select * from userId where userid=" + userid +
"'and password=" + password;