自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

J2EE學(xué)習(xí)筆記——Struts2多方法實(shí)現(xiàn)

開發(fā) 后端
當(dāng)Web容器收到 請求(HttpServletRequest)它將請求傳遞給一個標(biāo)準(zhǔn)的的過濾鏈包括 流程(ActionContextCleanUp)過濾器,然后經(jīng)過Other filters...

實(shí)現(xiàn)  登陸 驗(yàn)證 和注冊 驗(yàn)證在一個  LoginAction  類中:

Login.jsp:

  1. <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%> 
  2. <%@ taglib uri="/struts-tags"prefix="s" %> 
  3.    
  4. <%   
  5. String path = request.getContextPath();   
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  7. %> 
  8.    
  9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  10. <html> 
  11.   <head> 
  12.     <basehrefbasehref="<%=basePath%>"> 
  13.        
  14.     <title>Login</title> 
  15.     <metahttp-equivmetahttp-equiv="pragma"content="no-cache"> 
  16.     <metahttp-equivmetahttp-equiv="cache-control"content="no-cache"> 
  17.     <metahttp-equivmetahttp-equiv="expires"content="0"> 
  18.     <metahttp-equivmetahttp-equiv="keywords"content="keyword1,keyword2,keyword3"> 
  19.     <metahttp-equivmetahttp-equiv="description"content="This is my page"> 
  20.    
  21.   </head> 
  22.      
  23.   <body> 
  24.        
  25.      <s:formactions:formaction="loginAction"method="post"> 
  26.         
  27.         <s:textfieldnames:textfieldname="username"label="用戶名"/><br> 
  28.            
  29.          <s:passwordnames:passwordname="password"label="密碼"/><br> 
  30.             
  31.            <inputtypeinputtype="submit"value="登陸"><br> 
  32.         
  33.      </s:form> 
  34.        
  35.        
  36.   </body> 
  37. </html> 

LoginAction

  1. package xuyan.com.action;   
  2.    
  3. import java.sql.Connection;   
  4. import java.sql.PreparedStatement;   
  5. import java.sql.ResultSet;   
  6. import java.sql.SQLException;   
  7.    
  8. import xuyan.com.model.User;   
  9. import xuyan.com.model.UserDAO;   
  10.    
  11. import com.opensymphony.xwork2.ActionSupport;   
  12. import com.opensymphony.xwork2.ModelDriven;   
  13.    
  14. publicclass LoginAction  extends ActionSupport implements ModelDriven<User>{   
  15.    
  16.        
  17.     /**  
  18.      *   
  19.      */ 
  20.     privatestaticfinallong serialVersionUID = 1L;   
  21.        
  22.      User user=new User();   
  23.    
  24.     public User getUser() {   
  25.         return user;   
  26.     }   
  27.     publicvoid setUser(User user) {   
  28.         this.user = user;   
  29.     }   
  30.     private Connection con=null;   
  31.     private ResultSet rs=null;   
  32.     private PreparedStatement psmt=null;   

  33.        
  34.     /**  
  35.      * 用戶注冊  
  36.      *   
  37.      */ 
  38.         
  39.     public String Login()     
  40.     {   
  41.         System.out.println(user.getUsername()+"1111");   
  42.         System.out.println(user.getPassword()+"1111");            
  43.            
  44.         UserDAO dao=new UserDAO();   
  45.            
  46.         con=dao.getConnection();   
  47.            
  48.         try {   
  49.             psmt =con.prepareStatement("insert into  userinfo (username,password) values (?,?) ");   
  50.             psmt.setString(1, user.getUsername());   
  51.             psmt.setString(2, user.getPassword());   
  52.                
  53.             int a=psmt.executeUpdate();   
  54.                
  55.             if(a>0)   
  56.             {   
  57.                 System.out.println(user.getUsername()+"第2222次");   
  58.                 System.out.println(user.getPassword()+"第2222次");   
  59.                    
  60.                 return  SUCCESS;   
  61.             }   
  62.             else 
  63.             {   
  64.                 return  ERROR;   
  65.             }   
  66.                
  67.         } catch (SQLException e) {   
  68.                
  69.             e.printStackTrace();   
  70.             return  ERROR;   
  71.         }   
  72.            
  73.         finally 
  74.         {   
  75.             if(con != null){   
  76.                 try {   
  77.                     con.close();   
  78.                 } catch (SQLException e) {   
  79.                     e.printStackTrace();   
  80.                 }   
  81.             }   
  82.             if(psmt != null){   
  83.                 try {   
  84.                     psmt.close();   
  85.                 } catch (SQLException e) {   
  86.                     e.printStackTrace();   
  87.                 }   
  88.             }   
  89.             if(rs != null){   
  90.                 try {   
  91.                     rs.close();   
  92.                 } catch (SQLException e) {   
  93.                     e.printStackTrace();   
  94.                 }   
  95.             }   
  96.         }   
  97.            
  98.     }   
  99.             
  100.     @Override 
  101.     public String execute()  {   
  102.            
  103.         System.out.println(user.getUsername()+"1111");   
  104.         System.out.println(user.getPassword()+"1111");   
  105.            
  106.            
  107.         UserDAO dao=new UserDAO();   
  108.            
  109.         con=dao.getConnection();   
  110.            
  111.         try {   
  112.             psmt =con.prepareStatement("select * from userinfo where username=? and password=?");   
  113.             psmt.setString(1, user.getUsername());   
  114.             psmt.setString(2, user.getPassword());   
  115.                
  116.             rs=psmt.executeQuery();   
  117.                
  118.             if(rs.next())   
  119.             {   
  120.                 System.out.println(user.getUsername()+"第2222次");   
  121.                 System.out.println(user.getPassword()+"第2222次");   
  122.                    
  123.                 return  SUCCESS;   
  124.             }   
  125.             else 
  126.             {   
  127.                 return  ERROR;   
  128.             }   
  129.                
  130.         } catch (SQLException e) {   
  131.                
  132.             e.printStackTrace();   
  133.             return  ERROR;   
  134.         }   
  135.            
  136.         finally 
  137.         {   
  138.             if(con != null){   
  139.                 try {   
  140.                     con.close();   
  141.                 } catch (SQLException e) {   
  142.                     e.printStackTrace();   
  143.                 }   
  144.             }   
  145.             if(psmt != null){   
  146.                 try {   
  147.                     psmt.close();   
  148.                 } catch (SQLException e) {   
  149.                     e.printStackTrace();   
  150.                 }   
  151.             }   
  152.             if(rs != null){   
  153.                 try {   
  154.                     rs.close();   
  155.                 } catch (SQLException e) {   
  156.                     e.printStackTrace();   
  157.                 }   
  158.             }   
  159.         }   
  160.            
  161.            
  162.     }   
  163.     public User getModel() {   
  164.         // TODO Auto-generated method stub 
  165.         return user;   
  166.     }   
  167. }   

#p#

Struts.xml

  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 
  3. <struts> 
  4.    
  5.    
  6.      <packagenamepackagename="default"extends="struts-default"> 
  7.          
  8.         <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction"> 
  9.             <resultnameresultname="success">/Success.jsp</result> 
  10.              <resultnameresultname="error">/Login.jsp</result> 
  11.                   
  12.         </action> 
  13.            
  14.            
  15.         <actionnameactionname="regAction"class="xuyan.com.action.LoginAction"method="Login"> 
  16.             <resultnameresultname="success">/RegSuccess.jsp</result> 
  17.              <resultnameresultname="error">/reg.jsp</result> 
  18.                   
  19.         </action> 
  20.            
  21.     </package> 
  22.    
  23.    
  24.    
  25.    
  26. </struts> 

Reg.jsp:

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%@ taglib uri="/struts-tags" prefix="s" %> 
  3. <
  4. String path = request.getContextPath(); 
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  6. %> 
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10.   <head> 
  11.     <base href="<%=basePath%>"> 
  12.      
  13.     <title>My JSP 'reg.jsp' starting page</title> 
  14.      
  15.     <meta http-equiv="pragma" content="no-cache"> 
  16.     <meta http-equiv="cache-control" content="no-cache"> 
  17.     <meta http-equiv="expires" content="0">     
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  19.     <meta http-equiv="description" content="This is my page"> 
  20.      
  21.  
  22.   </head> 
  23.    
  24.   <body> 
  25.       <s:form  action="regAction" method="post"> 
  26.       
  27.         <s:textfield  name="username" label="用戶名" /> <br> 
  28.          
  29.          <s:password name="password"  label="密碼"/> <br> 
  30.           
  31.            <input type="submit" value="注冊"> <br> 
  32.       
  33.      </s:form> 
  34.   </body> 
  35. </html> 

注意:  

  1.  <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction"> 
  2.  
  3.  loginAction與login. JSP頁面中的<s:form  action="loginAction" method="post">對應(yīng) 
  4.  
  5.  
  6. <action name="regAction" class="xuyan.com.action.LoginAction" method="Login"> 
  7.  
  8.    regAction與reg.JSP 頁面中的 <s:form  action="regAction" method="post">對應(yīng) 
  9.  
  10.  
  11.    loginAction  類中public String Login()方法必須在Struts.xml中聲明 
  12.  
  13.  <action name="regAction" class="xuyan.com.action.LoginAction" method="Login"> 
  14.          <result name="success">/RegSuccess.jsp</result> 
  15.           <result name="error">/reg.jsp</result> 
  16.              
  17.      </action> 

原文鏈接:http://blog.csdn.net/skyxuyan/article/details/8982776

責(zé)任編輯:陳四芳 來源: CSDN博客
相關(guān)推薦

2011-05-16 14:07:58

J2EE

2009-06-16 11:14:00

Hibernate+SJ2EE應(yīng)用開發(fā)

2009-06-19 17:03:44

J2EE學(xué)習(xí)

2009-06-23 08:06:46

J2EE體系架構(gòu)J2EE模型J2EE設(shè)計(jì)模式

2009-06-10 14:10:23

J2EE學(xué)習(xí)J2EE是什么

2009-06-11 17:06:11

J2EE歷史Java EE概述

2009-06-10 13:37:06

J2EE可伸縮性J2EE靈活性J2EE維護(hù)

2009-06-19 17:29:12

Struts常見錯誤J2EE

2009-06-23 16:48:26

J2EE常見問題J2EE平臺

2011-11-25 14:59:36

JavaJ2EE框架

2009-06-18 15:54:57

J2EE下使用JNDI

2009-06-23 08:12:48

J2EE調(diào)用存儲過程

2009-06-22 17:34:40

J2EE架構(gòu)

2009-06-18 16:13:14

J2EE開發(fā)

2009-06-22 16:21:02

J2EE線程

2009-06-22 17:05:41

Java EEJava企業(yè)應(yīng)用

2009-07-09 16:06:10

JDK J2EE J2

2009-06-08 21:34:09

J2EEJ2SEJ2ME

2009-06-11 17:19:47

J2EE設(shè)計(jì)模式Template

2009-06-22 11:04:00

Jdbc存儲過程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號