ssm框架整合+Ajax異步驗證

SSM框架是目前企業比較經常使用的框架之一,它的靈活性、安全性相對於SSH有必定的優點。說到這,談談SSM和SSH的不一樣點,這也是企業常考初級程序員的面試題之一。說到這兩套框架的不一樣,主要是持久層框架Hibernate和MyBatis的不一樣和控制層框架SpringMVC和Struts2的不一樣。javascript

 

Hibernate和MyBatis的不一樣主要體現這麼幾點:php

1.自動化和半自動化:Hibernate的SQL語句自動生成不須要程序員編寫,而MyBatis須要編寫。html

2.學習上:Hibernate入門比較難,而MyBatis入門很是容易。java

3.可移植性:Hibernate可移植性好,對應不一樣的數據庫經過改變方言能夠直接用,而MyBatis可移植性差,對應不一樣的數據庫須要書寫不一樣的SQL語句mysql

4.關係維護上:Hibernate映射關係複雜,而MyBatis相對簡單。jquery

5.緩存:Hibernate有更好的二級緩存,能夠使用第三方緩存,而MyBatis自己緩存就很差。程序員

 

 

SpringMVC和Struts2的不一樣點以下:web

1.入口不一樣:SpringMVC的入口是Servlet,Struts的入口是Filter。面試

2.性能上:spring3 mvc是方法級別的攔截,攔截到方法後根據參數上的註解,把request數據注入進去,在spring3 mvc中,一個方法對應一個request上下文。而struts2框架是類級別的攔截,每次來了請求就建立一個Action,而後調用setter getter方法把request中的數據注入;struts2其實是通 setter getter方法與request打交道的;struts2中,一個Action對象對應一個request上下文。  spring

3.攔截器實現機制上,Struts2有以本身的interceptor機制,SpringMVC用的是獨立的AOP方式,這樣致使Struts2的配置文件量仍是比SpringMVC大。

4. 設計思想上,Struts2更加符合OOP的編程思想, SpringMVC就比較謹慎,在servlet上擴展。

5.SpringMVC集成了Ajax,使用很是方便,只需一個註解@ResponseBody就能夠實現,而後直接返回響應文本便可,而Struts2攔截器集成了Ajax,在Action中處理時通常必須安裝插件或者本身寫代碼集成進去,使用起來也相對不方便。

6.Spring MVC和Spring是無縫的。從這個項目的管理和安全上也比Struts2高(固然Struts2也能夠經過不一樣的目錄結構和相關配置作到SpringMVC同樣的效果,可是須要xml配置的地方很多)。

第一步:導包(這一步只要成功,能夠說成功了80%)

aopalliance.jar
aspectjweaver-1.5.4.jar
commons-fileupload-1.3.1.jar
commons-io-2.4.jar
commons-logging-1.1.3.jar
fastjson-1.2.13.jar
jstl-1.2.jar
mail.jar
mybatis-3.3.1.jar
mybatis-spring-1.2.4.jar
mysql-connector-java-5.1.26-bin.jar
spring-aop-4.2.3.RELEASE.jar
spring-aspects-4.2.3.RELEASE.jar
spring-beans-4.2.3.RELEASE.jar
spring-context-4.2.3.RELEASE.jar
spring-context-support-4.2.3.RELEASE.jar
spring-core-4.2.3.RELEASE.jar
spring-expression-4.2.3.RELEASE.jar
spring-instrument-4.2.3.RELEASE.jar
spring-instrument-tomcat-4.2.3.RELEASE.jar
spring-jdbc-4.2.3.RELEASE.jar
spring-jms-4.2.3.RELEASE.jar
spring-messaging-4.2.3.RELEASE.jar
spring-orm-4.2.3.RELEASE.jar
spring-oxm-4.2.3.RELEASE.jar
spring-test-4.2.3.RELEASE.jar
spring-tx-4.2.3.RELEASE.jar
spring-web-4.2.3.RELEASE.jar
spring-webmvc-4.2.3.RELEASE.jar
spring-webmvc-portlet-4.2.3.RELEASE.jar
spring-websocket-4.2.3.RELEASE.jar

 

第二步:寫Spring主配置文件以及MyBatis主配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 起別名 -->
<typeAliases>
<package name="com.blog.entity"/>
</typeAliases>
  
</configuration>

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<!-- 自動掃描註解 -->
 <context:component-scan base-package="com.blog.service"></context:component-scan> 

<!-- 配置鏈接池 -->
<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/blog_002"></property>
<property name="username" value="root"></property>
<property name="password" value="1234"></property>
</bean>

<!-- 配置mybatis工廠 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>

<!-- 註解事務 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 開啓註解事務 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 動態掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.blog.mapper"></property>

</bean>

</beans>

 

第三步:建立實體類

package com.blog.entity;

public class User {

	
	private Integer Id;
	private String email;
	private String userName;
	private String password;
	private Integer power;
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	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 Integer getPower() {
		return power;
	}
	public void setPower(Integer power) {
		this.power = power;
	}

	
	
}

 第四步:建立接口以及SQL映射文件(經過動態代理的方式綁定)

package com.blog.mapper;

import org.apache.ibatis.annotations.Param;

import com.blog.entity.User;

public interface UserMapper {

	
	User Login(String email);
	
	User Login2(@Param("email") String email,@Param("password") String password);
}

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.blog.mapper.UserMapper">
 
 <select id="Login" parameterType="String" resultType="User">
 select * from b_user where email=#{email}
 </select>
 
  <select id="Login2" resultMap="logins">
 select * from b_user where email=#{email} and password=#{password}
 </select>
 <resultMap type="User" id="logins">
 	 <id column="Id" property="Id"/>
	 <result column="email" property="email"/>
	<result column="password" property="password"/>
 </resultMap>
 
 
 </mapper>

第五步:建立Service接口以及實現類

package com.blog.service;

import com.blog.entity.User;

public interface UserService {

	
	User Login(String email);

	User Login2(String email,String password);
	
	
}

 

package com.blog.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.blog.entity.User;
import com.blog.mapper.UserMapper;
import com.blog.service.UserService;

@Service
public class UserServiceImpl implements UserService{

    @Resource
    private UserMapper userMapper;
    
    
    @Override
    public User Login(String email) {
        // TODO Auto-generated method stub
        return userMapper.Login(email);
    }


    @Override
    public User Login2(String email, String password) {
        // TODO Auto-generated method stub
        return userMapper.Login2(email, password);
    }

    
    
}

第六步:寫測試類測試上述方法

package com.blog.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.blog.entity.User;
import com.blog.mapper.UserMapper;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class testMapper {

    @Resource
    private UserMapper userMapper;
    
    
    
    @Test
    public void testName() throws Exception {
        
        User user=userMapper.Login("1933108196@qq.com");
        System.out.println(user);
         
        
        
        
    }
    
    @Test
    public void testName2() throws Exception {
        
        User user=userMapper.Login2("1933108196@qq.com", "kangri123");
        System.out.println(user.getUserName());
        
        
        
        
    }
    
}

第七步:測試成功後,開始設置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Demo_Model</display-name>
  
  <!--全局配置 -->  
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!-- 該監聽器主要做用是隨tomcat的啓動,而加載context中的全局配置文件 -->  
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 該過濾器主要做用是處理字符亂碼 ,可攔截全部請求,處理全部亂碼-->
 <filter>
 <filter-name>CharacterEncoding</filter-name>
 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 <init-param>
 <param-name>encoding</param-name>
 <param-value>UTF-8</param-value>
 </init-param>
 </filter>
 
 <filter-mapping>
 <filter-name>CharacterEncoding</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>
 
  
  
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springmvc.xml</param-value>
  </init-param>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>*.do</url-pattern>
  <!-- 
  *.do攔截全部帶do的請求,對靜態資源放行
  / 攔截全部帶.jsp的請求,同時對靜態資源攔截
  /* 攔截全部
   -->
  </servlet-mapping>
  
  
  
  
  <welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第八步:配置springmvc.xml(與Spring框架無縫整合)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
<!--掃描Controller層 -->
<context:component-scan base-package="com.blog.controller"></context:component-scan>

<!-- 開啓註解 -->
<mvc:annotation-driven/>

<!-- 配置視圖解析器 --> 	    
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

配置完畢後記得添加Controller 相關的類

package com.blog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.blog.entity.User;
import com.blog.service.UserService;

@Controller
public class UserController {

	@Resource
	private UserService userService;
	
	@RequestMapping(value="emailCheck2.do",method=RequestMethod.POST)
	public void emailCheck2(String email,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model) throws IOException{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();

		User user=userService.Login(email);
		if(user!=null){
			out.println("郵箱已經存在");
		}else{
			out.println("郵箱能夠使用");
		}
		
		out.flush();
		out.close();
		
	}
	
	
	@RequestMapping(value="emailCheck.do",method=RequestMethod.POST)
	public void emailCheck(String email,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model) throws IOException{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();

		User user=userService.Login(email);
		if(user!=null){
			out.println("郵箱正確");
		}else{
			out.println("郵箱錯誤");
		}
		
		out.flush();
		out.close();
		
	}
	
	@RequestMapping(value="passCheck.do",method=RequestMethod.POST)
	public void emailCheck(String email,String password,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model) throws IOException{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();

		User user=userService.Login2(email,password);
		if(user!=null){
			out.println("密碼正確");
	        
		}else{
			out.println("密碼錯誤");
		}
		
		out.flush();
		out.close();
		
	}
	
     @RequestMapping(value="LoginCheck.do",method=RequestMethod.POST)
	 public String LoginCheck(String email,String password,HttpServletResponse response,HttpServletRequest request,HttpSession session,Model model){
    	 User user=userService.Login2(email, password);
    	 if(user==null){
    		 request.setAttribute("error", "用戶名或密碼不能爲空");
    		 return "Login";
    	 }else{
    		 
    		 model.addAttribute("user", user);
    		 return "index";
    	 }
    	 
     }
	
	
}

 

 

第十步:開始啓動tomcat,若是控制檯無報錯信息,說明配置整合成功,反之失敗,因此整合過程當中必定要仔細

十月 28, 2017 12:06:40 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Demo_Model' did not find a matching property.
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version:        Apache Tomcat/7.0.82
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built:          Sep 29 2017 12:23:15 UTC
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number:         7.0.82.0
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name:               Windows 8
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version:            6.2
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture:          amd64
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home:             D:\jdk1.7.0_51\jre
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version:           1.7.0_51-b13
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor:            Oracle Corporation
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE:         D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME:         D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\wtpwebapps
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\endorsed
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=GBK
十月 28, 2017 12:06:40 上午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\jdk1.7.0_51\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:/jdk1.7.0_51/bin/../jre/bin/server;D:/jdk1.7.0_51/bin/../jre/bin;D:/jdk1.7.0_51/bin/../jre/lib/amd64;D:\jdk1.7.0_51\bin;D:\Maven\apache-maven-3.3.9\bin;C:\Program Files\Git\cmd;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;D:\Program Files\MySQL\MySQL Server 5.7\bin;C:\Program Files\VisualSVN Server\bin;C:\Program Files\TortoiseSVN\bin;D:\Program Files\mongodb-win32-x86_64-2.4.3\bin;C:\Users\tracholar\AppData\Roaming\cabal\bin;C:\Users\tracholar\AppData\Roaming\npm;D:\Program Files\userbin;D:\wamp64\bin\php\php5.6.16;D:\texlive\2015\bin\win32;D:\Program Files\eclipse;;.
十月 28, 2017 12:06:40 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-bio-8080"]
十月 28, 2017 12:06:40 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-bio-8009"]
十月 28, 2017 12:06:40 上午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1592 ms
十月 28, 2017 12:06:40 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
十月 28, 2017 12:06:41 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.82
十月 28, 2017 12:06:46 上午 org.apache.catalina.startup.TldConfig execute
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
十月 28, 2017 12:06:46 上午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
十月 28, 2017 12:06:46 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
十月 28, 2017 12:06:46 上午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization started
十月 28, 2017 12:06:46 上午 org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
信息: Refreshing Root WebApplicationContext: startup date [Sat Oct 28 00:06:46 CST 2017]; root of context hierarchy
十月 28, 2017 12:06:46 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
十月 28, 2017 12:06:49 上午 org.springframework.web.context.ContextLoader initWebApplicationContext
信息: Root WebApplicationContext: initialization completed in 2875 ms
十月 28, 2017 12:06:49 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\docs
十月 28, 2017 12:06:49 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\docs has finished in 234 ms
十月 28, 2017 12:06:49 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\examples
十月 28, 2017 12:06:51 上午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: contextInitialized()
十月 28, 2017 12:06:51 上午 org.apache.catalina.core.ApplicationContext log
信息: SessionListener: contextInitialized()
十月 28, 2017 12:06:51 上午 org.apache.catalina.core.ApplicationContext log
信息: ContextListener: attributeAdded('org.apache.jasper.compiler.TldLocationsCache', 'org.apache.jasper.compiler.TldLocationsCache@a8e6df5')
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\examples has finished in 1,568 ms
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\host-manager
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\host-manager has finished in 421 ms
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\manager
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\manager has finished in 141 ms
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\ROOT
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deployment of web application directory D:\Program Files\apache-tomcat-7.0.82-windows-x64\apache-tomcat-7.0.82\webapps\ROOT has finished in 94 ms
十月 28, 2017 12:06:51 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
十月 28, 2017 12:06:51 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
十月 28, 2017 12:06:51 上午 org.apache.catalina.startup.Catalina start
信息: Server startup in 11060 ms

 

 接下來開始寫登陸頁面作異步驗證,在此以前AJax所需的jQuery插件必定要記得導,導入後,寫個alert彈框測試一下,以保證在Ajax和JQuery交互的過程當中不會由於插件的問題而報錯

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陸</title>
<script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
<script>
    function m1() {

        $(document).ready(function() {

            var email = $("#email").val();

            if (email == null || email == "") {
                $("#nameDiv1").html("郵箱不能爲空");

            } else if (email.indexOf("@") == -1) {
                $("#nameDiv1").html("郵箱格式不正確,必須包含@");

            } else if (email.indexOf(".") == -1) {
                $("#nameDiv1").html("郵箱格式不正確,必須包含.");
            } else {
                $.post("${pageContext.request.contextPath}/emailCheck.do", {
                    "email" : email
                }, function(data) {
                    $("#nameDiv1").html(data);
                }, "text");

            }

        });

    }
    
    function m2(){
        $(document).ready(function(){
            var email=$("#email").val();
            var password=$("#password").val();
            if(password==null||password==""){
                $("#nameDiv2").html("密碼不能爲空");
            }else if(password.length<6){
                $("#nameDiv2").html("密碼長度不能小於六位");
            }else if(password.length>18){
                $("#nameDiv2").html("密碼長度已經超過18位,不符合給定要求");
            }else{
                $.post(
                "${pageContext.request.contextPath}/passCheck.do",
                {"email":email,"password":password},
                function(data){
                    $("#nameDiv2").html(data);
                },
                "text"                
                );
                
            }
            
            
            
        });
    }
</script>


</head>
<body>
    <div align="center">
        <form action="LoginCheck.do" method="post">
           <h2 align="center">${error}</h2>
            <table align="center">
                <tr>
                    <td>Email:<input type="text" name="email" id="email" onblur="m1()"
                        style="width: 200px;" /></td>
                    <td><span id="nameDiv1" style="color: red; font-size: 15px;"></span></td>
                </tr>
                <tr>
                    <td>Password:<input type="password" name="password" id="password" onblur="m2()"
                        style="width: 200px;" /></td>
                    <td><span id="nameDiv2" style="color: red; font-size: 15px;"></span></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Login" /></td>
                
                </tr>
            </table>
        </form>

    </div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${user.userName}
</body>
</html>
相關文章
相關標籤/搜索