SSH框架整合之註冊登錄 .

上一篇介紹了三大框架的搭建,今天基於上一次的框架使用MVC模式完成註冊和登錄功能。

MVC模式:模型(Model),視圖(View)和控制Controller)。 MVC模式的目的就是實現Web系統的職能分工。 Model層實現系統中的業務邏輯,通常可以用JavaBean或EJB來實現。 View層用於與用戶的交互,通常用JSP來實現。 Controller層是Model與View之間溝通的橋樑,它可以分派用戶的請求並選擇恰當的視圖以用於顯示,同時它也可以解釋用戶的輸入並將它們映射爲模型層可執行的操作。

開發工具:1.MyEclipse6.5                2.mysql-5.0

 

1.創建數據庫 

這裏使用的是mysql5.0

數據庫名:mytest

表名:user

create database mytest;

use mytest;

create table user(

user_id int primary key auto_increment not null,

username varchar(50) not null,

password varchar(50) not null);

 最後注意要導入數據庫驅動jar包:mysql-connector-java-5.0.3-bin.jar

將mysql-connector-java-5.0.3-bin.jar放到WEB-INF的lib文件裏

2.導入Struts2.0所需要的jar包,刪除不需要的jar包(否則程序運行的時候會報錯)

導入6個jar包,刪除一個jar包

將以下Jar包直接複製到WEB-INF下的lib文件夾

 

刪除asm-2.2.3.jar

 

3. User.class和User.hbm.xml

新建Package:com.softeem.pojo

新建Class:User.class

新建XML:User.hbm.xml

User.class

  1. package com.softeem.pojo;  
  2.   
  3. public class User {    
  4.     private int user_id;    
  5.     private String username;    
  6.     private String password;    
  7.     public int getUser_id() {    
  8.         return user_id;    
  9.     }    
  10.     public void setUser_id(int user_id) {    
  11.         this.user_id = user_id;    
  12.     }    
  13.     public String getUsername() {    
  14.         return username;    
  15.     }    
  16.     public void setUsername(String username) {    
  17.         this.username = username;    
  18.     }    
  19.     public String getPassword() {    
  20.         return password;    
  21.     }    
  22.     public void setPassword(String password) {    
  23.         this.password = password;    
  24.     }    
  25.     
  26. }    
package com.softeem.pojo;

public class User {  
    private int user_id;  
    private String username;  
    private String password;  
    public int getUser_id() {  
        return user_id;  
    }  
    public void setUser_id(int user_id) {  
        this.user_id = user_id;  
    }  
    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;  
    }  
  
}  

 

User.hbm.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4.   
  5. <hibernate-mapping>  
  6.     <class name="com.softeem.pojo.User" table="user">  
  7.         <id name="user_id" type="java.lang.Integer" column="user_id">  
  8.         <!--  主鍵生成策略 -->  
  9.         <generator class="increment"></generator>  
  10.         </id>  
  11.         <property name="username" type="string" column="username" length="50"></property>  
  12.         <property name="password" type="string" column="password" length="50"></property>         
  13.     </class>  
  14.       
  15. </hibernate-mapping>  
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
	<class name="com.softeem.pojo.User" table="user">
		<id name="user_id" type="java.lang.Integer" column="user_id">
		<!--  主鍵生成策略 -->
		<generator class="increment"></generator>
		</id>
		<property name="username" type="string" column="username" length="50"></property>
		<property name="password" type="string" column="password" length="50"></property>		
	</class>
	
</hibernate-mapping>




4.UserDAO接口

新建Package:com.softeem.dao

新建Interface:UserDAO.class

  1. package com.softeem.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.softeem.pojo.User;  
  6.   
  7. public interface UserDAO {  
  8.     //聲明增加和查找方法   
  9.     public void saveUser(User user);  
  10.       
  11.     public User findUser(User user);  
  12. }  
package com.softeem.dao;

import java.util.List;

import com.softeem.pojo.User;

public interface UserDAO {
	//聲明增加和查找方法
	public void saveUser(User user);
	
	public User findUser(User user);
}



5.UserDAO接口的實現類UserDAOImpl.class

新建Package:com.softeem.dao.Impl

新建Class:UserDAOImpl.class

  1. package com.softeem.dao.Impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  6.   
  7. import com.softeem.dao.UserDAO;  
  8. import com.softeem.pojo.User;  
  9.   
  10.   
  11. public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{  
  12.     //增加用戶   
  13.     public void saveUser(User user){  
  14.         this.getHibernateTemplate().save(user);  
  15.     }  
  16.       
  17.     //查詢驗證用戶是否存在   
  18.     public User findUser(User user){  
  19.         User firstuser = new User();  
  20.         //HQL查詢語句   
  21.         String hql = "from User user where user.username='"  
  22.                 + user.getUsername() + "' and user.password= '"  
  23.                 + user.getPassword() + "'";  
  24.         //將查詢出的結果放到List   
  25.         List<User> userlist = this.getHibernateTemplate().find(hql);  
  26.         //判斷是否有查詢結果,換句話說就是判斷用戶是否存在   
  27.         if(userlist.size()>0){  
  28.         //取出查詢結果的第一個值,理論上數據庫是沒有重複的用戶信息   
  29.         firstuser = userlist.get(0);  
  30.         }  
  31.         return firstuser;  
  32.     }  
  33. }  
package com.softeem.dao.Impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.softeem.dao.UserDAO;
import com.softeem.pojo.User;


public class UserDAOImpl extends HibernateDaoSupport implements UserDAO{
	//增加用戶
	public void saveUser(User user){
		this.getHibernateTemplate().save(user);
	}
	
	//查詢驗證用戶是否存在
	public User findUser(User user){
		User firstuser = new User();
		//HQL查詢語句
		String hql = "from User user where user.username='"
				+ user.getUsername() + "' and user.password= '"
				+ user.getPassword() + "'";
		//將查詢出的結果放到List
		List<User> userlist = this.getHibernateTemplate().find(hql);
		//判斷是否有查詢結果,換句話說就是判斷用戶是否存在
		if(userlist.size()>0){
		//取出查詢結果的第一個值,理論上數據庫是沒有重複的用戶信息
		firstuser = userlist.get(0);
		}
		return firstuser;
	}
}


6.UserService接口

新建Package:com.softeem.service

新建Interface:UserService.class

  1. package com.softeem.service;  
  2.   
  3. import com.softeem.pojo.User;  
  4.   
  5. public interface UserService {  
  6.     //聲明增加和查找方法   
  7.     public void saveUser(User user);  
  8.       
  9.     public boolean findUser(User user);  
  10. }  
package com.softeem.service;

import com.softeem.pojo.User;

public interface UserService {
	//聲明增加和查找方法
	public void saveUser(User user);
	
	public boolean findUser(User user);
}


7.UserService接口的實現類UserServiceImpl.class

新建Package:com.softeem.service.Impl

新建Class:UserServiceImpl.class

  1. package com.softeem.service.Impl;  
  2.   
  3. import com.softeem.dao.UserDAO;  
  4. import com.softeem.pojo.User;  
  5. import com.softeem.service.UserService;  
  6.   
  7. public class UserServiceImpl implements UserService{  
  8.     //注入DAO,生成GET SET 方法   
  9.     private UserDAO userdao;  
  10.   
  11.     public UserDAO getUserdao() {  
  12.         return userdao;  
  13.     }  
  14.   
  15.     public void setUserdao(UserDAO userdao) {  
  16.         this.userdao = userdao;  
  17.     }  
  18.   
  19.     //保存用戶信息   
  20.     public void saveUser(User user){  
  21.         this.userdao.saveUser(user);  
  22.     }  
  23.     //查找驗證用戶信息   
  24.     public boolean findUser(User user){  
  25.         //   
  26.         User firstuser = this.userdao.findUser(user);  
  27.         //在UserDAO查詢中已經判斷了只有當用戶名和密碼都存在時才返回firstuser   
  28.         //所以在這裏只用判斷firstuser裏面用戶名或者密碼中的一個是否存在就可以了   
  29.         if(firstuser.getUsername()!=null){  
  30.             return true;  
  31.         }else{  
  32.             return false;  
  33.         }  
  34.     }  
  35. }  
package com.softeem.service.Impl;

import com.softeem.dao.UserDAO;
import com.softeem.pojo.User;
import com.softeem.service.UserService;

public class UserServiceImpl implements UserService{
	//注入DAO,生成GET SET 方法
	private UserDAO userdao;

	public UserDAO getUserdao() {
		return userdao;
	}

	public void setUserdao(UserDAO userdao) {
		this.userdao = userdao;
	}

	//保存用戶信息
	public void saveUser(User user){
		this.userdao.saveUser(user);
	}
	//查找驗證用戶信息
	public boolean findUser(User user){
		//
		User firstuser = this.userdao.findUser(user);
		//在UserDAO查詢中已經判斷了只有當用戶名和密碼都存在時才返回firstuser
		//所以在這裏只用判斷firstuser裏面用戶名或者密碼中的一個是否存在就可以了
		if(firstuser.getUsername()!=null){
			return true;
		}else{
			return false;
		}
	}
}

8.RegistAction

新建Package:com.softeem.action

新建Class:RegistAction.class

  1. package com.softeem.action;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  3. import com.softeem.pojo.User;  
  4. import com.softeem.service.UserService;  
  5.   
  6. public class RegistAction extends ActionSupport{  
  7.     private User user;  
  8.     //注入Service,生成SET GET方法   
  9.     private UserService userservice;  
  10.     public User getUser() {  
  11.         return user;  
  12.     }  
  13.     public void setUser(User user) {  
  14.         this.user = user;  
  15.     }  
  16.     public UserService getUserservice() {  
  17.         return userservice;  
  18.     }  
  19.     public void setUserservice(UserService userservice) {  
  20.         this.userservice = userservice;  
  21.     }  
  22.       
  23.     //execute方法   
  24.     @Override  
  25.     public String execute() throws Exception {  
  26.         this.userservice.saveUser(this.user);  
  27.         return SUCCESS;  
  28.     }  
  29.   
  30. }  
package com.softeem.action;
import com.opensymphony.xwork2.ActionSupport;
import com.softeem.pojo.User;
import com.softeem.service.UserService;

public class RegistAction extends ActionSupport{
	private User user;
	//注入Service,生成SET GET方法
	private UserService userservice;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserService getUserservice() {
		return userservice;
	}
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}
	
	//execute方法
	@Override
	public String execute() throws Exception {
		this.userservice.saveUser(this.user);
		return SUCCESS;
	}

}


 

9.LoginAction

新建Class:LoginAction.class

  1. package com.softeem.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. import com.softeem.pojo.User;  
  5. import com.softeem.service.UserService;  
  6.   
  7. public class LoginAction extends ActionSupport{  
  8.     private User user;  
  9.     //注入Service,生成Set和Get方法   
  10.     private UserService userservice;  
  11.     public User getUser() {  
  12.         return user;  
  13.     }  
  14.     public void setUser(User user) {  
  15.         this.user = user;  
  16.     }  
  17.     public UserService getUserservice() {  
  18.         return userservice;  
  19.     }  
  20.     public void setUserservice(UserService userservice) {  
  21.         this.userservice = userservice;  
  22.     }  
  23.       
  24.     @Override  
  25.     public String execute() throws Exception {  
  26.         boolean flag = userservice.findUser(user);  
  27.         if(flag){  
  28.             return SUCCESS;  
  29.         }else{  
  30.             return INPUT;  
  31.         }  
  32.           
  33.     }  
  34. }  
package com.softeem.action;

import com.opensymphony.xwork2.ActionSupport;
import com.softeem.pojo.User;
import com.softeem.service.UserService;

public class LoginAction extends ActionSupport{
	private User user;
	//注入Service,生成Set和Get方法
	private UserService userservice;
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public UserService getUserservice() {
		return userservice;
	}
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}
	
	@Override
	public String execute() throws Exception {
		boolean flag = userservice.findUser(user);
		if(flag){
			return SUCCESS;
		}else{
			return INPUT;
		}
		
	}
}


10.Spring配置文件

打開WebRoot--->WEB-INF--->applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  6.   
  7.   
  8. <!-- dbcp連接池 -->  
  9. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  10.     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>  
  11.     <property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>  
  12.     <property name="username" value="root"></property>  
  13.     <property name="password" value="root"></property>  
  14.     <!-- 最大連接數 -->  
  15.     <property name="maxActive" value="100"></property>  
  16.     <!-- 最大可空閒連接數 -->  
  17.     <property name="maxIdle" value="30"></property>  
  18.     <!-- 最大等待連接 -->  
  19.     <property name="maxWait" value="500"></property>  
  20.     <!-- 事務提交,true代表自動提交事物 -->  
  21.     <property name="defaultAutoCommit" value="true"></property>  
  22.       
  23. </bean>  
  24.   
  25. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  26.     <property name="dataSource" ref="dataSource"></property>  
  27.     <property name="hibernateProperties">  
  28.         <props>  
  29.             <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  30.             <prop key="hibernate.show_sql">true</prop>  
  31.         </props>  
  32.     </property>  
  33.     <property name="mappingResources">  
  34.         <list>  
  35.             <value>com/softeem/pojo/User.hbm.xml</value>  
  36.         </list>  
  37.     </property>  
  38. </bean>  
  39.   
  40.   
  41.   
  42. <bean id="UserDAO" class="com.softeem.dao.Impl.UserDAOImpl" scope="singleton">  
  43.     <property name="sessionFactory">  
  44.          <ref bean="sessionFactory"/>  
  45.     </property>  
  46. </bean>  
  47.   
  48. <bean id="UserService" class="com.softeem.service.Impl.UserServiceImpl">  
  49.     <property name="userdao" ref="UserDAO"></property>  
  50. </bean>  
  51.   
  52. <bean id="RegistAction" class="com.softeem.action.RegistAction" scope="prototype">  
  53.     <property name="userservice" ref="UserService"></property>  
  54. </bean>  
  55.   
  56. <bean id="LoginAction" class="com.softeem.action.LoginAction" scope="prototype">  
  57.     <property name="userservice" ref="UserService"></property>  
  58. </bean>  
  59.   
  60.   
  61.   
  62. </beans>  
<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">


<!-- dbcp連接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://localhost:3306/mytest"></property>
	<property name="username" value="root"></property>
	<property name="password" value="root"></property>
	<!-- 最大連接數 -->
	<property name="maxActive" value="100"></property>
	<!-- 最大可空閒連接數 -->
	<property name="maxIdle" value="30"></property>
	<!-- 最大等待連接 -->
	<property name="maxWait" value="500"></property>
	<!-- 事務提交,true代表自動提交事物 -->
	<property name="defaultAutoCommit" value="true"></property>
	
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			<prop key="hibernate.show_sql">true</prop>
		</props>
	</property>
	<property name="mappingResources">
		<list>
			<value>com/softeem/pojo/User.hbm.xml</value>
		</list>
	</property>
</bean>



<bean id="UserDAO" class="com.softeem.dao.Impl.UserDAOImpl" scope="singleton">
	<property name="sessionFactory">
		 <ref bean="sessionFactory"/>
	</property>
</bean>

<bean id="UserService" class="com.softeem.service.Impl.UserServiceImpl">
	<property name="userdao" ref="UserDAO"></property>
</bean>

<bean id="RegistAction" class="com.softeem.action.RegistAction" scope="prototype">
	<property name="userservice" ref="UserService"></property>
</bean>

<bean id="LoginAction" class="com.softeem.action.LoginAction" scope="prototype">
	<property name="userservice" ref="UserService"></property>
</bean>



</beans>

 

11.web.xml配置文件

打開WebRoot--->WEB-INF--->web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  6.   
  7.     <filter>  
  8.         <filter-name>struts2</filter-name>  
  9.         <filter-class>  
  10.             org.apache.struts2.dispatcher.FilterDispatcher  
  11.         </filter-class>  
  12.     </filter>  
  13.   
  14.     <filter-mapping>  
  15.         <filter-name>struts2</filter-name>  
  16.         <url-pattern>/*</url-pattern>  
  17.     </filter-mapping>  
  18.   
  19.     <welcome-file-list>  
  20.         <welcome-file>index.jsp</welcome-file>  
  21.     </welcome-file-list>  
  22.   
  23.     <listener>  
  24.         <listener-class>  
  25.             org.springframework.web.context.ContextLoaderListener  
  26.         </listener-class>  
  27.     </listener>  
  28.       
  29. </web-app>  
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.FilterDispatcher
		</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
</web-app>



12.Struts配置文件

在src目錄下新建XML文件

struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.   
  6. <struts>  
  7.     <package name="user" extends="struts-default">  
  8.         <!-- class="RegistAction"與applicationContext.xml中的id對應 -->  
  9.         <action name="regist" class="RegistAction">  
  10.             <result>/success.jsp</result>  
  11.         </action>  
  12.           
  13.         <action name="login" class="LoginAction">  
  14.             <result name="success">/success.jsp</result>  
  15.             <result name="input">/input.jsp</result>  
  16.         </action>  
  17.   
  18.     </package>  
  19. </struts>  
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<package name="user" extends="struts-default">
		<!-- class="RegistAction"與applicationContext.xml中的id對應 -->
		<action name="regist" class="RegistAction">
			<result>/success.jsp</result>
		</action>
		
		<action name="login" class="LoginAction">
			<result name="success">/success.jsp</result>
			<result name="input">/input.jsp</result>
		</action>

	</package>
</struts>


13.最後是JSP頁面

regist.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags"%>  
  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.     <title>regist</title>  
  13.     <script>  
  14.     function jumpToLogin(){  
  15.     document.location.href="login.jsp";  
  16.     }  
  17.     </script>  
  18.   </head>  
  19.     
  20.   <body>  
  21.  <s:form action="regist" method="post">  
  22.   <table>  
  23.     <tr><td>用戶<input type="text" name="user.username"/></td></tr>  
  24.     <tr><td>密碼<input type="password" name="user.password"/></td></tr>  
  25.     <tr><td><input type="submit" value="註冊"/></td><td><input type="button" value="返回登錄" onclick="jumpToLogin()"/></td></tr>  
  26.     </table>  
  27.   </s:form>  
  28.   </body>  
  29. </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>regist</title>
    <script>
    function jumpToLogin(){
    document.location.href="login.jsp";
    }
    </script>
  </head>
  
  <body>
 <s:form action="regist" method="post">
  <table>
    <tr><td>用戶<input type="text" name="user.username"/></td></tr>
    <tr><td>密碼<input type="password" name="user.password"/></td></tr>
    <tr><td><input type="submit" value="註冊"/></td><td><input type="button" value="返回登錄" onclick="jumpToLogin()"/></td></tr>
    </table>
  </s:form>
  </body>
</html>


login.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="s" uri="/struts-tags"%>  
  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.     <title>login</title>  
  13.     <script>  
  14.     function jumpToRegist(){  
  15.     document.location.href="regist.jsp";  
  16.     }  
  17.     </script>  
  18.   </head>  
  19.     
  20.   <body>  
  21.  <s:form action="login" method="post">  
  22.   <table>  
  23.     <tr><td>用戶<input type="text" name="user.username"/></td></tr>  
  24.     <tr><td>密碼<input type="password" name="user.password"/></td></tr>  
  25.     <tr><td><input type="submit" value="登錄"/></td><td><input type="button" value="註冊" onclick="jumpToRegist()"/></td></tr>  
  26.     </table>  
  27.   </s:form>  
  28.   </body>  
  29. </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>login</title>
    <script>
    function jumpToRegist(){
    document.location.href="regist.jsp";
    }
    </script>
  </head>
  
  <body>
 <s:form action="login" method="post">
  <table>
    <tr><td>用戶<input type="text" name="user.username"/></td></tr>
    <tr><td>密碼<input type="password" name="user.password"/></td></tr>
    <tr><td><input type="submit" value="登錄"/></td><td><input type="button" value="註冊" onclick="jumpToRegist()"/></td></tr>
    </table>
  </s:form>
  </body>
</html>


success.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'success.jsp' starting page</title>  
  13.   
  14.   
  15.   </head>  
  16.     
  17.   <body>  
  18.    SUCCESS~ <br>  
  19.   </body>  
  20. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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>


  </head>
  
  <body>
   SUCCESS~ <br>
  </body>
</html>


input.jsp

  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'input.jsp' starting page</title>  
  13.   
  14.   </head>  
  15.     
  16.   <body>  
  17.     登錄失敗! <br>  
  18.   </body>  
  19. </html>  
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'input.jsp' starting page</title>

  </head>
  
  <body>
    登錄失敗! <br>
  </body>
</html>


 14.通過以上操作,SSH整合之登錄註冊已經完成

需要注意的是SSH框架的正確搭建和jar包的正確導入,對於新手這可能並不簡單。

框架搭建可以看上一篇文章   SSH框架整合之搭建

找不到jar包的直接下載項目吧    SSH整合之登陸註冊

稍後會放出SSH框架整合之增刪查改操作。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章