簡單的登陸註冊頁面
1.配置JDBC驅動連線資料庫
2. 配置struts2框架
3. 利用1 2完成登入頁面, 注意做到不耦合,即servlet Api和控制器完全脫離)
4. 利用1 2 製作註冊頁面,判斷資料庫是否存在註冊賬號,若存在,則提示賬號存在,不存在則跳轉註冊成功頁面
建立專案命名為chapter04 找到下載好的JDBC驅動jar檔案複製貼上到WebRoot-WEB-INF下的lib資料夾下,如圖
2配置struts2框架
在網上下載struts2框架,開啟struts2資料夾中的lib找到如下的jar檔案 同樣複製到WebRoot-WEB-INF下的lib資料夾下
接下來在web.xml中配置前端控制器,具體程式碼如下
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
最後在Struts.xml中配置Action分發給對應的JSP 程式碼如下
<struts> <constant name="struts.devMode" value="true"></constant> <package name="loginpkg" extends="struts-default" namespace="/"> <action name="succes" class="loginAction.login" method="logins"> <result name="succes">/succes.jsp</result> <result name="defeat">/defeat.jsp</result> </action> <action name="register" class="loginAction.register" method="registers"> <result name="succes">/index.jsp</result> <result name="defeat">/defeat.jsp</result> </action> </package> </struts>
JSP頁面的有 index.jsp register.jsp defeat.jsp success.jsp對應分別是首頁 註冊頁面 失敗跳轉頁面 成功登入頁面這裡只介紹index.jsp介面,其他比較簡單自己寫 index.jsp程式碼如下
<body> <form action="/chapter04/succes"> 使用者:<input name="name" style="margin-left:10px;"><br> 密碼:<input name="password" style="margin-left:10px;"><br> <input type="submit" value="登入" style="position: absolute; left: 50px; top:60px;"> </form> <a style="position: absolute; left: 110px; top:60px;" href='/chapter04/register.jsp' >註冊</a> </body>
在src下建立一個包 名為jdbcDAO ,在包內建立名為Conn的jdbc連線資料庫類,程式碼如下;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Conn { public static Connection con; public static Connection getconnection() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String url="jdbc:mysql://127.0.0.1:3306/userdb?characterEncoding=utf8"; String user="root"; String password="123456"; try { con=DriverManager.getConnection(url, user, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con}
新建查詢資料庫功能類 read 程式碼如下
package jdbcDAO; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.PreparedStatement; public class read { public static boolean isCheck(String name,String password){ Connection con=Conn.getconnection(); String sql="select * from user"; PreparedStatement ps=null; ResultSet rs=null; try { ps=(PreparedStatement) con.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ String name1=rs.getString("name"); String password1=rs.getString("password"); if(name1.equals(name)&&password1.equals(password)){ return true; } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } public static boolean isCheck1(String name){ Connection con=Conn.getconnection(); String sql="select * from user"; PreparedStatement ps=null; ResultSet rs=null; try { ps=(PreparedStatement) con.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()){ String name1=rs.getString("name"); if(name1.equals(name)){ return true; } } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } }
建立新增使用者功能類 Add程式碼如下
package jdbcDAO; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import com.mysql.jdbc.PreparedStatement; public class Add { public static boolean Adds(String name,String password){ Connection con=Conn.getconnection(); boolean falg=false; PreparedStatement ps=null; String sql="insert into user(name,password)values(?,?)"; try { ps=(PreparedStatement) con.prepareStatement(sql); ps.setString(1, name); ps.setString(2, password); int i=ps.executeUpdate(); if(i==1){ falg=true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return falg;
建立longAction包
在longAction包中建立兩個類login和register類
login類程式碼如下
package loginAction; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.Servlet; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.config.entities.ActionConfig; import jdbcDAO.read; import userBean.User; public class login extends ActionSupport { private String name; private String password; private boolean falg=false; private static final long serialVersionUID = 1L; public String logins(){ ServletActionContext sac=null; name=sac.getRequest().getParameter("name"); password=sac.getRequest().getParameter("password"); falg=read.isCheck(name, password); if(falg){ return "succes"; }else{ return "defeat"; } } }
Register類程式碼如下
package loginAction; import org.apache.struts2.ServletActionContext; import jdbcDAO.Add; import jdbcDAO.read; public class register { private boolean falg=false; private String name; private String password; public String registers(){ ServletActionContext sac=null; name=sac.getRequest().getParameter("name"); password=sac.getRequest().getParameter("password"); falg=read.isCheck1(name); //真 if(!falg){ falg=Add.Adds(name, password); if(falg){ return "succes"; } else{ return "defeat"; } }else{ return "defeat"; } } }