实体类:
package entity; public class userfo { private int id;//id private String name;//用户名 private String password;//密码 private String sex;//性别 private String phone;//电话号 private String fdate;//日期 private String adress;//地址 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getFdate() { return fdate; } public void setFdate(String fdate) { this.fdate = fdate; } public String getAdress() { return adress; } public void setAdress(String adress) { this.adress = adress; } }
三层架构:
public class UserDaoImpl extends sqlser implements UserDao { Connection con = null; PreparedStatement st = null; ResultSet rs = null; /* * 登入方法 * (non-Javadoc) * @see dao.UserDao#Login(entity.userfo) */ public int Login(userfo user) { // TODO Auto-generated method stub int flag=0; con = this.getConnection(); String sql = "select * from userfo where name = ? and password = ?"; try { st = con.prepareStatement(sql); st.setString(1, user.getName()); st.setString(2, user.getPassword()); rs = st.executeQuery(); if(rs!=null && rs.next()){ flag=1; } } catch (SQLException e) { e.printStackTrace(); }finally { this.ShiFang(rs, st, con); } return flag; }
/** * 登入 * @author Administrator * */ public interface UserBiz { public int Login(userfo user); }
package BaseDao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class sqlser { //数据库初始化 String driver="oracle.jdbc.driver.OracleDriver"; String url="jdbc:oracle:thin:@localhost:1521:ORCL"; String user = "epet"; String pwd = "123456"; //获取连接 public Connection getConnection(){ try { Class.forName(driver); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Connection con =null; try { con =DriverManager.getConnection(url,user,pwd); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } //======释放连接======= public void ShiFang(ResultSet rs, Statement st,Connection con){ try { if(rs!=null){ rs.close(); }if(st!=null){ st.close(); }if(con!=null){ con.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置包信息 --> <package name="default" namespace="/" extends="struts-default"> <!-- 配置Action:关联Action JavaBean --> <action name="longinAct" class="Action.LoginAction"> <!-- 指定返回的视图 ;默认使用转发--> <result name="input">/error.jsp</result> <result name="success">/success.jsp</result> </action> </package> </struts>
package Action; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.ApplicationAware; import org.apache.struts2.interceptor.SessionAware; import biz.UserBiz; import biz.impl.UserBizImpl; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import dao.UserDao; import dao.UserDaoImpl; import entity.userfo; /** * * @author Administrator * */ public class LoginAction extends ActionSupport implements ModelDriven,SessionAware,ApplicationAware{ private userfo user=new userfo(); private Map<String,Object> sessionM; private Map<String,Object> applicationM; public Object getModel() { // TODO Auto-generated method stub return user; } public void setSession(Map<String, Object> arg0) { // TODO Auto-generated method stub sessionM=arg0; } public void setApplication(Map<String, Object> arg0) { // TODO Auto-generated method stub applicationM=arg0; } @Override public String execute() throws Exception{ UserDao dao=new UserDaoImpl(); // 通过ActionContext获取MAP类型的Session ActionContext ac=ActionContext.getContext(); sessionM=ac.getSession(); sessionM.put("name",user.getName()); sessionM.put("user",user); // 通过控制反转方法 获取MAP类型的Session sessionM.put("name", user.getName()); sessionM.put("password", user.getPassword()); sessionM.put("user", user); String name=(String)sessionM.get("name"); String pwd=(String)sessionM.get("password"); userfo a=new userfo(); a.setName(name); a.setPassword(pwd); int flag=dao.Login(a); if(flag>0){ if(applicationM.get("count")==null){ applicationM.put("count", 1); }else{ int count=Integer.parseInt(applicationM.get("count").toString()); applicationM.put("count", count+1); } return SUCCESS; }else{ return INPUT; } } }
登入页面、成功页面、失败页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Struts2Demo</title> </head> <body> <s:form action="longinAct" method="post"> <s:textfield name="name" label="用户名"></s:textfield> <s:password name="password" label="密码"></s:password> <s:submit value="登陆"> </s:submit> </s:form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'success.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <H1> 用户名: <s:property value="name" /> <br /> 密码: <s:property value="password" /> <br /> 登陆成功! <br> 从SESSION中获取值(jsp方式):用户名: <%=session.getAttribute("name")%><br> </H1> 从SESSION中获取值(struts方式):用户名: <s:property value="#session.name" /> <br> 访问次数: <s:property value="#application.count" /> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> 用户名: <s:property value="name" /> <br /> 密 码: <s:property value="password" /> <br /> <H1>登陆失败! </H1> </body> </html>