<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="suibian" namespace="/" extends="struts-default">
<action name="login" class="com.hao.login.Login" method="login">
<result name="success">/WEB-INF/page/success.jsp</result>
<result name="failed">/WEB-INF/page/failed.jsp</result>
</action>
<action name="register" class="com.hao.login.Login" method="register">
<result name="success1">/WEB-INF/page/success1.jsp</result>
<result name="failed1">/WEB-INF/page/failed1.jsp</result>
</action>
</package>
</struts>
package com.hao.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DaoHelp {
private String url="jdbc:oracle:thin:@localhost:1521:orcl";
private String driver="oracle.jdbc.driver.OracleDriver";
private String user="scott";
private String password="tiger";
private Connection conn=null;
public static DaoHelp dao;
private DaoHelp(){
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw (new RuntimeException(e));
}
}
public static DaoHelp getDao(){
if(dao==null){
dao = new DaoHelp();
}
return dao;
}
public boolean login(String name,String pass){
try {
conn=DriverManager.getConnection(url, user, password);
PreparedStatement ps=conn.prepareStatement("select * from usermessage where username=? and password=?");
//记录数据
ps.setString(1, name);
ps.setString(2, pass);
ResultSet rs=ps.executeQuery();
while(rs.next()){
return true;
}
return false;
} catch (SQLException e) {
throw (new RuntimeException(e));
}
}
public void resgiter(String name,String pass){
try {
conn=DriverManager.getConnection(url, user, password);
PreparedStatement ps=conn.prepareStatement("insert into usermessage values(?,?)");
ps.setString(1, name);
ps.setString(2, pass);
ps.execute();
} catch (SQLException e) {
throw (new RuntimeException(e));
}
}
}
package com.hao.login;
import com.hao.dao.DaoHelp;
public class Login {
private String userName;
private String password;
public String getUsername() {
return userName;
}
public void setUsername(String username) {
this.userName = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login(){
DaoHelp dh=DaoHelp.getDao();
String name=getUsername();
String pass=getPassword();
if(dh.login(name, pass)){
return "success";
}else{
return "failed";
}
}
public String register(){
DaoHelp dh=DaoHelp.getDao();
String name=getUsername();
String pass=getPassword();
dh.resgiter(name, pass);
return "success1";
}
}
-->
<script type="text/javascript">
function jump(ac) {
form1=document.getElementById("form1");
form1.action=ac;
form1.submit();
}
</script>
</head>
<body>
<form id="form1" action="" method="post">
用户名:<input type="text" name="username" ><br>
密 码 :<input type="password" name="password"><br>
<input type="button" value="登录" οnclick="jump('login')">
<input type="submit" value="注册" οnclick="jump('register.jsp')">
</form>
</body>
<body>
<form action="register" method="post">
<h1>注册用户</h1>
用户名:<input type="text" name="username" ><br>
密 码 :<input type="password" name="password"><br>
<input type="submit" value="提交">
</form>
</body>
<body>
欢迎您${username}登录成功!!!
</body>