用户登入成效完毕,轻便案列完美化解Mvc设计方

来源:http://www.smjxgs.com 作者:王中王鉄算盘 人气:69 发布时间:2019-08-08
摘要:jspgt;“New”-"Other"-输入“Maven”,新建一个“MavenProject”,如下图所示: 2.请勾选“Create a simpleproject”,创建一个简单的项目,不使用模板。也可以使用模板,选择WebApp,不过这里就不

jsp&servlet初体验——用户登录功能实现,

  近来无事,复习复习Spring相关知识,从《Spring企业应用开发实战》这本书开始。

一个小列子搞定Mvc模式,包括数据库以及如何提高用户体验度

数据库准备—创建db_login数据库  t_user表

 

  本文为学习《Spring企业应用开发实战》总结,著作权归原作者。

1.首先来web.xml配置servlet的访问路径:

1、创建web工程

一、用Maven建立web项目

  此处参考 张果 博客,在此表示感谢。原文地址:

1.点击“File”->“New”->"Other"->输入“Maven”,新建一个“Maven Project”,如下图所示:

4887王中王鉄算盘奖结果 1

2.请勾选“Create a simple project”,创建一个简单的项目,不使用模板。也可以使用模板,选择WebApp,不过这里就不应该勾选。如下图所示:

4887王中王鉄算盘奖结果 2

 

3.填写好包名、项目名,选择打包类型为:war,如下图所示:

4887王中王鉄算盘奖结果 3

 

4.项目创建好后可能会发现有错误,选择项目,右键“属性properties”->"层面Project Facets"->"Java"修改版本号为1.8,默认为1.5;点击“Ok”保存后关闭。如下图所示:

4887王中王鉄算盘奖结果 4

 

5.重复上一个步骤,反勾Dynamic Web Module,将项目暂时变成非Web项目。点击“Ok”保存后关闭。

4887王中王鉄算盘奖结果 5

6.重复上一步骤,再进层面属性,勾选“Dynamic Web Module”选择Version为3.0。点击左下角的超链接“Further Configuration available...“。

4887王中王鉄算盘奖结果 6

7.勾选“Generate web.xml deployment descriptor”生成web.xml部署描述文件。点击“Ok”保存后关闭。

4887王中王鉄算盘奖结果 7

 

8.将生成的WebContent目录下的两个文件夹“META-INF”与“WEB-INF”复制到src/main/webapp目录下。

4887王中王鉄算盘奖结果 8

 

9.删除WebContent目录。

4887王中王鉄算盘奖结果 9

 

10.删除后会发现项目的pom.xml文件报错,是因为找不到指定位置的web.xml文件引起的。再进入项目的属性,选择“Deployment Assembly”项目部署项,删除“src/test/java”、“src/test/resources”与“WebContent”目录,因为这三项不需要部署出去。

4887王中王鉄算盘奖结果 10

 

11.点击“Add添加”后选择“Folder文件夹”为项目的最终部署结果指定Web内容根文件夹。

4887王中王鉄算盘奖结果 11

 

12.选择srcmainwebapp目录为目标目录,点击“Finish完成”保存并关闭。

4887王中王鉄算盘奖结果 12

 

13.如果此时项目还报错,随便修改pom.xml文件后保存后应该错误会消失。

14.此后项目运行需要添加tomcat运行环境就不赘述了。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation=" " id="WebApp_ID" version="2.5">
<display-name>HeadFirstJspServletChap05</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--配置servlet的名字--->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.java.filter.LoginFilter</filter-class>
</filter>
<!--配置servlet的访问路径--->
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

2、创建用户model   user.java

 1 package com.gxy.model;
 2 
 3 public class User {
 4     private int id;
 5     private String userName;
 6     private String password;
 7     
 8     public User() {
 9         super();
10     }
11     
12     
13     public User(String userName, String password) {
14         super();
15         this.userName = userName;
16         this.password = password;
17     }
18 
19 
20     public int getId() {
21         return id;
22     }
23     public void setId(int id) {
24         this.id = id;
25     }
26     public String getUserName() {
27         return userName;
28     }
29     public void setUserName(String userName) {
30         this.userName = userName;
31     }
32     public String getPassword() {
33         return password;
34     }
35     public void setPassword(String password) {
36         this.password = password;
37     }
38 
39 }

 二、类包规划,添加依赖,数据库准备

1.类包规划

4887王中王鉄算盘奖结果 13

2.pom.xml中添加lib依赖

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.adam</groupId>
  5     <artifactId>Chapter2</artifactId>
  6     <version>0.0.1-SNAPSHOT</version>
  7     <packaging>war</packaging>
  8     <properties>
  9         <!-- springframe 版本控制 -->
 10         <spring.version>4.1.1.RELEASE</spring.version>
 11     </properties>
 12 
 13     <dependencies>
 14         <!-- springframe start -->
 15         <dependency>
 16             <groupId>org.springframework</groupId>
 17             <artifactId>spring-core</artifactId>
 18             <version>${spring.version}</version>
 19         </dependency>
 20 
 21         <dependency>
 22             <groupId>org.springframework</groupId>
 23             <artifactId>spring-web</artifactId>
 24             <version>${spring.version}</version>
 25         </dependency>
 26 
 27         <dependency>
 28             <groupId>org.springframework</groupId>
 29             <artifactId>spring-oxm</artifactId>
 30             <version>${spring.version}</version>
 31         </dependency>
 32 
 33         <dependency>
 34             <groupId>org.springframework</groupId>
 35             <artifactId>spring-tx</artifactId>
 36             <version>${spring.version}</version>
 37         </dependency>
 38 
 39         <dependency>
 40             <groupId>org.springframework</groupId>
 41             <artifactId>spring-jdbc</artifactId>
 42             <version>${spring.version}</version>
 43         </dependency>
 44 
 45         <dependency>
 46             <groupId>org.springframework</groupId>
 47             <artifactId>spring-webmvc</artifactId>
 48             <version>${spring.version}</version>
 49         </dependency>
 50 
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-aop</artifactId>
 54             <version>${spring.version}</version>
 55         </dependency>
 56 
 57         <dependency>
 58             <groupId>org.springframework</groupId>
 59             <artifactId>spring-context-support</artifactId>
 60             <version>${spring.version}</version>
 61         </dependency>
 62 
 63         <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-test</artifactId>
 66             <version>${spring.version}</version>
 67         </dependency>
 68         <!-- springframe end -->
 69 
 70         <!--mysql数据库驱动 -->
 71         <dependency>
 72             <groupId>mysql</groupId>
 73             <artifactId>mysql-connector-java</artifactId>
 74             <version>5.1.38</version>
 75         </dependency>
 76         <!--log4j日志包 -->
 77         <dependency>
 78             <groupId>org.apache.logging.log4j</groupId>
 79             <artifactId>log4j-core</artifactId>
 80             <version>2.6.1</version>
 81         </dependency>
 82         <!-- JUnit单元测试工具 -->
 83         <dependency>
 84             <groupId>junit</groupId>
 85             <artifactId>junit</artifactId>
 86             <version>4.10</version>
 87         </dependency>
 88         <!-- aspectJ AOP 织入器 -->
 89         <dependency>
 90             <groupId>org.aspectj</groupId>
 91             <artifactId>aspectjweaver</artifactId>
 92             <version>1.8.9</version>
 93         </dependency>
 94         <!-- jstl -->
 95         <dependency>
 96             <groupId>javax.servlet</groupId>
 97             <artifactId>jstl</artifactId>
 98             <version>1.2</version>
 99         </dependency>
100     </dependencies>
101 
102     <build>
103         <plugins>
104             <!-- define the project compile level -->
105             <plugin>
106                 <groupId>org.apache.maven.plugins</groupId>
107                 <artifactId>maven-compiler-plugin</artifactId>
108                 <version>2.3.2</version>
109                 <configuration>
110                     <source>1.8</source>
111                     <target>1.8</target>
112                 </configuration>
113             </plugin>
114         </plugins>
115     </build>
116 </project>

  如果eclise没有自动下载jar包,右键“属性properties”->"Maven"->"Update Project..",强制更新。

3.数据库准备

  建立数据库sampledb,添加两张表,结构如下:

4887王中王鉄算盘奖结果 14

4887王中王鉄算盘奖结果 15

  t_user为用户信息表,t_login_log为用户登录日志表。表的引擎要指定InnoDB,因为该引擎支持事务。

4887王中王鉄算盘奖结果 16

  初始化一条数据,用户名和密码用于测试登录。

mvc及三层架构编程,模型城(M)、控制层(C)、显示层(V)

3、创建util包 Dbutil.java

 1 package com.gxy.util;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 
 6 public class Dbutil {
 7     private String dbUrl ="jdbc:mysql://localhost:3306/db_login";
 8     private String jdbcName="com.mysql.jdbc.Driver";
 9     private String dbUserName="root";
10     private String dbpassword="123456";
11     
12     public Connection getcon() throws Exception{
13         Class.forName(jdbcName);
14         Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbpassword);
15         return con;
16     }
17     
18     public void closeCon(Connection con) throws Exception{
19         con.close();
20     }
21 
22 }

 三、持久层

1.建立领域对象(实体类)

User.java

 1 package com.adam.domain;
 2 
 3 import java.io.Serializable;
 4 import java.util.Date;
 5 
 6 public class User implements Serializable {
 7     private int userId;
 8     private String userName;
 9     private String password;
10     private int credits;
11     private String lastIp;
12     private Date lastVisit;
13 
14     public int getUserId() {
15         return userId;
16     }
17 
18     public void setUserId(int userId) {
19         this.userId = userId;
20     }
21 
22     public String getUserName() {
23         return userName;
24     }
25 
26     public void setUserName(String userName) {
27         this.userName = userName;
28     }
29 
30     public String getPassword() {
31         return password;
32     }
33 
34     public void setPassword(String password) {
35         this.password = password;
36     }
37 
38     public int getCredits() {
39         return credits;
40     }
41 
42     public void setCredits(int credits) {
43         this.credits = credits;
44     }
45 
46     public String getLastIp() {
47         return lastIp;
48     }
49 
50     public void setLastIp(String lastIp) {
51         this.lastIp = lastIp;
52     }
53 
54     public Date getLastVisit() {
55         return lastVisit;
56     }
57 
58     public void setLastVisit(Date lastVisit) {
59         this.lastVisit = lastVisit;
60     }
61 
62 }

LoginLog.java

 1 package com.adam.domain;
 2 
 3 import java.io.Serializable;
 4 import java.util.Date;
 5 
 6 public class LoginLog implements Serializable {
 7     private int loginLogId;
 8     private int userId;
 9     private String ip;
10     private Date loginDate;
11 
12     public int getLoginLogId() {
13         return loginLogId;
14     }
15 
16     public void setLoginLogId(int loginLogId) {
17         this.loginLogId = loginLogId;
18     }
19 
20     public int getUserId() {
21         return userId;
22     }
23 
24     public void setUserId(int userId) {
25         this.userId = userId;
26     }
27 
28     public String getIp() {
29         return ip;
30     }
31 
32     public void setIp(String ip) {
33         this.ip = ip;
34     }
35 
36     public Date getLoginDate() {
37         return loginDate;
38     }
39 
40     public void setLoginDate(Date loginDate) {
41         this.loginDate = loginDate;
42     }
43 
44 }

4887王中王鉄算盘奖结果 17

2.数据处理对象

UserDao.java

 1 package com.adam.dao;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.jdbc.core.JdbcTemplate;
 8 import org.springframework.jdbc.core.RowCallbackHandler;
 9 import org.springframework.stereotype.Repository;
10 
11 import com.adam.domain.User;
12 
13 @Repository
14 public class UserDao {
15     @Autowired
16     private JdbcTemplate jdbcTemplate;
17 
18     public int getMatchCount(String name, String password) {
19         String sql = "SELECT count(*) FROM t_user WHERE user_name = ? and password = ?";
20         return jdbcTemplate.queryForInt(sql, new Object[] { name, password });
21     }
22 
23     public User findUserByName(final String name) {
24         String sql = "select user_id,user_name,credits from t_user where user_name = ?";
25         final User user = new User();
26         jdbcTemplate.query(sql, new Object[] { name }, new RowCallbackHandler() {
27 
28             public void processRow(ResultSet rs) throws SQLException {
29                 user.setUserId(rs.getInt("user_id"));
30                 user.setUserName(name);
31                 user.setCredits(rs.getInt("credits"));
32             }
33         });
34         return user;
35     }
36 
37     public void updateLoginInfo(User user) {
38         String sql = "update t_user set last_visit = ?,last_ip = ?, credits = ? where user_id = ?";
39         jdbcTemplate.update(sql,
40                 new Object[] { user.getLastVisit(), user.getLastIp(), user.getCredits(), user.getUserId() });
41     }
42 }

LoginLogDao.java

 1 package com.adam.dao;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.jdbc.core.JdbcTemplate;
 5 import org.springframework.stereotype.Repository;
 6 
 7 import com.adam.domain.LoginLog;
 8 
 9 @Repository
10 public class LoginLogDao {
11     @Autowired
12     private JdbcTemplate jdbcTemplate;
13 
14     public void insertLoginLog(LoginLog loginLog) {
15         String sql = "insert into t_login_log(user_id, ip,login_datatime) values(?,?,?)";
16         Object[] pra = { loginLog.getUserId(), loginLog.getIp(), loginLog.getLoginDate() };
17         jdbcTemplate.update(sql, pra);
18     }
19 }

3.在spring中装配Dao

  在Dao中并没有打开或释放数据库连接,那么Dao是如何访问数据库的?而且在Dao中使用的JdbcTemplate是从哪来的?这都是spring帮我们实现的,所以需要在spring中装配Dao。

  创建spring配置文件applicationContext.xml,结构如下

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:tx="http://www.springframework.org/schema/tx"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context
 9         http://www.springframework.org/schema/context/spring-context-4.3.xsd
10         http://www.springframework.org/schema/aop
11         http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
12         http://www.springframework.org/schema/tx
13         http://www.springframework.org/schema/tx/spring-tx.xsd">
14     ...
15 </beans>

  在其中加入配置信息

 1 <!-- 扫描包,应用注解 -->
 2     <context:component-scan base-package="com.adam.dao" />
 3 <!--定义一个jdbc数据源,创建一个驱动管理数据源的bean -->
 4     <bean id="jdbcDataSource"
 5         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 6         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 7         <property name="url" value="jdbc:mysql://localhost:3306/sampledb" />
 8         <property name="username" value="root" />
 9         <property name="password" value="AdamJin" />
10     </bean>
11 <!-- 定义jdbc模板Bean -->
12     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
13         p:dataSource-ref="jdbcDataSource" />

2.后台分包放代码,是代码看起来更有结构性

4、创建dao包  UserDao.java

 1 package com.gxy.dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 
 7 import com.gxy.model.User;
 8 
 9 public class UserDao {
10     public User login(Connection con,User user) throws Exception{
11         User resultUser=null;
12         String sql="select * from t_user where userName=? and passWord=?";
13         PreparedStatement pst=con.prepareStatement(sql);
14         pst.setString(1, user.getUserName());
15         pst.setString(2,user.getPassword());
16         ResultSet rs=pst.executeQuery();
17         if(rs.next()){
18             resultUser=new User();
19             resultUser.setUserName(rs.getString("userName"));
20             resultUser.setPassword(rs.getString("passWord"));
21         }
22         return resultUser;
23     }
24 
25 }

四、业务层

1.建立业务处理类

UserService.java

 1 package com.adam.service;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Service;
 5 
 6 import com.adam.dao.LoginLogDao;
 7 import com.adam.dao.UserDao;
 8 import com.adam.domain.LoginLog;
 9 import com.adam.domain.User;
10 
11 @Service
12 public class UserService {
13     @Autowired
14     private UserDao userdao;
15     @Autowired
16     private LoginLogDao loginLogDao;
17 
18     public boolean hasMatchUser(String name, String password) {
19         int matchCount = userdao.getMatchCount(name, password);
20         return matchCount > 0;
21     }
22 
23     public User findUserByUsername(String username) {
24         return userdao.findUserByName(username);
25     }
26 
27     public void loginSuccess(User user) {
28         user.setCredits(user.getCredits()   5);
29         LoginLog loginLog = new LoginLog();
30         loginLog.setUserId(user.getUserId());
31         loginLog.setIp(user.getLastIp());
32         loginLog.setLoginDate(user.getLastVisit());
33         userdao.updateLoginInfo(user);
34         loginLogDao.insertLoginLog(loginLog);
35     }
36 }

4887王中王鉄算盘奖结果 18

2.在spring中装配Service

  打开刚建立的applicationContext.xml,在其中添加

 1 <!-- 扫描包,应用注解 -->
 2     <context:component-scan base-package="com.adam.service" />
 3 <!-- 配置事务管理器 -->
 4     <bean id="transactionManager"
 5         class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
 6         p:dataSource-ref="jdbcDataSource" />
 7 
 8 <!-- 通过AOP配置事务增强,让service包下所有Bean的所有方法拥有事务 -->
 9     <aop:config proxy-target-class="true">
10         <aop:pointcut expression="execution(* com.adam.service..*(..))"
11             id="serviceMethod" />
12         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
13     </aop:config>
14     <tx:advice id="txAdvice" transaction-manager="transactionManager">
15         <tx:attributes>
16             <tx:method name="*"></tx:method>
17         </tx:attributes>
18     </tx:advice>

  至此后端就算完成了,可以进行单元测试了。

2-1.model.存放java实体类:

5、创建servlet包  loginServlet

 1 package com.gxy.servlet;
 2 
 3 import java.io.IOException;
 4 import java.sql.Connection;
 5 
 6 import javax.servlet.RequestDispatcher;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 
13 import com.gxy.dao.UserDao;
14 import com.gxy.model.User;
15 import com.gxy.util.Dbutil;
16 
17 public class loginServlet extends HttpServlet{
18 
19     /**
20      * 
21      */
22     private static final long serialVersionUID = 1L;
23 
24     Dbutil dbutil=new Dbutil();
25     UserDao userDao=new UserDao();
26     @Override
27     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
28             throws ServletException, IOException {
29         this.doPost(req, resp);
30     }
31 
32     @Override
33     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
34             throws ServletException, IOException {
35         String userName=req.getParameter("userName");
36         String passWord=req.getParameter("passWord");
37         
38         Connection con=null;
39         try {
40             User user=new User(userName,passWord);
41             con=dbutil.getcon();
42             User resultUser=userDao.login(con, user);
43             if(resultUser==null){
44                 System.out.println("no");
45             }else{
46                 HttpSession session=req.getSession();
47                 session.setAttribute("userName", resultUser.getUserName());
48                 session.setAttribute("passWord", resultUser.getPassword());
49                 resp.sendRedirect("target.jsp");
50             }
51         } catch (Exception e) {
52             // TODO Auto-generated catch block
53             e.printStackTrace();
54         }
55         
56     }
57 
58 }

五、单元测试

  右击New->Other选择如图,创建单元测试类

4887王中王鉄算盘奖结果 19

TestUserService.java

 1 package com.adam.test;
 2 import static org.junit.Assert.*;
 3 
 4 import java.util.Date;
 5 
 6 import org.junit.Test;
 7 import org.junit.runner.RunWith;
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.test.context.ContextConfiguration;
10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11 
12 import com.adam.domain.User;
13 import com.adam.service.UserService;
14 
15 @RunWith(SpringJUnit4ClassRunner.class)
16 @ContextConfiguration(locations = { "/applicationContext.xml" })
17 public class TestUserService {
18     @Autowired
19     private UserService userService;
20 
21     @Test
22     public void hasMatchedUser() {
23         boolean b1 = userService.hasMatchUser("admin", "123456");
24         boolean b2 = userService.hasMatchUser("admin", "1236");
25 
26         assertTrue(b1);
27         assertTrue(!b2);
28     }
29     
30     @Test
31     public void findUserByUsername(){
32         User user = userService.findUserByUsername("admin");
33         assertEquals(user.getUserName(), "admin");
34         user.setLastIp("192.168.11.188");
35         user.setLastVisit(new Date());
36         userService.loginSuccess(user);
37     }
38     
39 }

 

Run As -> JUnit Test得到以下结果,便测试成功,数据库也会多出登陆日志数据。

4887王中王鉄算盘奖结果 20

package com.java.model;

6、用户登录界面  login.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 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26   <form action="login" method="post">
27     <table>
28         <tr>
29             <td>用户名:</td>
30             <td><input type="text" id="userName" name="userName"></td>
31         </tr>
32         <tr>
33             <td>密码:</td>
34             <td><input type="password" id="passWord" name="passWord"></td>
35         </tr>
36         <tr>
37             <td colspan="2"><input type="submit" value="提交" ></td>
38         </tr>
39     </table>
40    </form>
41   </body>
42 </html>

六、展示层

  业务层和持久层开发任务结束,需要界面提供展示。

1.配置Spring MVC框架

 修改web.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 5     id="WebApp_ID" version="3.0">
 6     <!-- 从类路径下载spring配置文件 -->
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>
10             classpath:applicationContext.xml
11         </param-value>
12     </context-param>
13     <!-- 负责启动spring容器监听器,将引用上面的上下文参数获取spring配置文件地址 -->
14     <listener>
15         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16     </listener>
17     <!-- springMVC主控制servlet -->
18     <servlet>
19         <servlet-name>adam</servlet-name>
20         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
21         <init-param>  
22             <param-name>contextConfigLocation</param-name>  
23             <param-value>classpath:adam-servlet.xml</param-value>  
24         </init-param> 
25         <load-on-startup>2</load-on-startup>
26     </servlet>
27     <!-- springMVC处理的url -->
28     <servlet-mapping>
29         <servlet-name>adam</servlet-name>
30         <url-pattern>*.html</url-pattern>
31     </servlet-mapping>
32 </web-app>

注意:上面代码23行若不配置,spring会到WEB-INF下找*-servle.xml,*是servlet配置的name。

adam-servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6         http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context
 8         http://www.springframework.org/schema/context/spring-context.xsd">
 9 
10     <!-- 扫描包,应用注解 -->
11     <context:component-scan base-package="com.adam.web" />
12 
13     <!-- 配置视图解析器 -->
14     <bean
15         class="org.springframework.web.servlet.view.InternalResourceViewResolver"
16         p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/"
17         p:suffix=".jsp" />
18 
19 </beans>

2.控制器类

LoginController.java

 1 package com.adam.web;
 2 
 3 import java.util.Date;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 
 7 import org.springframework.beans.factory.annotation.Autowired;
 8 import org.springframework.stereotype.Controller;
 9 import org.springframework.web.bind.annotation.RequestMapping;
10 import org.springframework.web.servlet.ModelAndView;
11 
12 import com.adam.domain.User;
13 import com.adam.service.UserService;
14 
15 @Controller
16 public class LoginController {
17     @Autowired
18     private UserService userService;
19 
20     @RequestMapping(value = "/index.html")
21     public String loginPage() {
22         return "login";
23     }
24     
25     @RequestMapping(value = "/loginCheck.html")
26     public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand){
27         boolean isVaildUser = userService.hasMatchUser(loginCommand.getUsername(), loginCommand.getPassword());
28         if(!isVaildUser){
29             return new ModelAndView("login", "error", "用户名或密码错误");
30         }else{
31             User user = userService.findUserByUsername(loginCommand.getUsername());
32             user.setLastIp(request.getLocalAddr());
33             user.setLastVisit(new Date());
34             userService.loginSuccess(user);
35             request.getSession().setAttribute("user", user);
36             return new ModelAndView("main");
37         }
38     }
39 }

其中需要一个表单pojo

LoginCommand.java

 1 package com.adam.web;
 2 
 3 public class LoginCommand {
 4     private String username;
 5     private String password;
 6 
 7     public String getUsername() {
 8         return username;
 9     }
10 
11     public void setUsername(String username) {
12         this.username = username;
13     }
14 
15     public String getPassword() {
16         return password;
17     }
18 
19     public void setPassword(String password) {
20         this.password = password;
21     }
22 
23 }

3.jsp视图页面

  在WEB-INF下建立文件夹jsp,并新建以下文件。

login.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <html>
 5 <head>
 6 <title>登陆</title>
 7 </head>
 8 <body>
 9     <c:if test="${!empty error}">
10         <font color="red"><c:out value="${error}" /></font>
11     </c:if>
12     <form action="<c:url value="/loginCheck.html"/>" method="post">
13         用户名: <input type="text" name="username"> <br /> 密&nbsp;&nbsp;&nbsp;码: <input
14             type="password" name="password"> <br /> <input type="submit"
15             value="提交"> <input type="reset" value="重置">
16     </form>
17 </body>
18 </html>

main.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 4 <html>
 5 <head>
 6 <title>首页</title>
 7 </head>
 8 <body>${user.userName},欢迎您,您的积分是${user.credits}。
 9 </body>
10 </html>

到此,整个实例就完成了。

public class User {//创建user实体类

跳转界面  target.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 'target.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <h1>用户名:<%=session.getAttribute("userName") %></h1>
27     <h1>密码:<%=session.getAttribute("passWord") %></h1>
28   </body>
29 </html>

七、运行

Run As -> Run on Server,输入地址 便会看到

4887王中王鉄算盘奖结果 21

输入正确的信息即可登录成功

4887王中王鉄算盘奖结果 22

private int id;//id
private String userName;//用户名
private String password;//密码

7、运行结果

 

数据库准备—创建db_login数据库 t_user表 1、创建web工程 2、创建用户model user.java 1 package com.gxy.model...

八、源码下载

 点击下载

public User() {
super();
}

public User(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = 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;
}
}

2-2dao层存放操作数据库的类

package com.java.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.java.model.User;

public class UserDao {

public User login(Connection con,User user)throws Exception{
User resultUser=null;
String sql="select * from t_user where userName=? and password=?";//sql语句
PreparedStatement pstmt=con.prepareStatement(sql);//预编译处理
pstmt.setString(1, user.getUserName());//为预编译设置参数
pstmt.setString(2, user.getPassword());
ResultSet rs=pstmt.executeQuery();//执行sql语句并将结果返回到resultSet结果集中
if(rs.next()){//如果结果集不为空
resultUser=new User();//封装user
resultUser.setUserName(rs.getString("userName"));
resultUser.setPassword(rs.getString("password"));
}
return resultUser;
}
}

2-3.控制层存放servlet

package com.java.web;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.java.dao.UserDao;
import com.java.model.User;
import com.java.util.DbUtil;

public class LoginServlet extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 1L;

DbUtil dbUtil=new DbUtil();
UserDao userDao=new UserDao();

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName=request.getParameter("userName");//获取登陆页面传来的值
String password=request.getParameter("password");

Connection con=null;
try {
User user=new User(userName,password);
con=dbUtil.getCon();
User currentUser=userDao.login(con, user);
if(currentUser==null){//登陆的逻辑处理
request.setAttribute("error", "用户名或密码错误");
request.setAttribute("userName", userName);
request.setAttribute("password", password);
request.getRequestDispatcher("login.jsp").forward(request, response);//跳转到登陆页面,为了更友好,我们把用户名和面传回登录页面,然后通过el表达式取出来
}else{
HttpSession session=request.getSession();
session.setAttribute("currentUser", currentUser);
response.sendRedirect("main.jsp");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

2-4.util存放我们的工具类:

package com.java.util;

import java.sql.Connection;
import java.sql.DriverManager;

public class DbUtil {

private String dbUrl="jdbc:mysql://localhost:3306/db_jsp";//数据库连接地址
private String dbUserName="root";//用户名
private String dbPassword="56";//密码
private String jdbcName="com.mysql.jdbc.Driver";//驱动
public Connection getCon()throws Exception{
Class.forName(jdbcName);
Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);//得到连接
return con;
}

public void closeCon(Connection con)throws Exception{//关闭连接
if(con!=null){
con.close();
}
}

public static void main(String[] args) {//测试连接
DbUtil dbUtil=new DbUtil();
try {
dbUtil.getCon();
System.out.println("连接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

到目前为止我们的后台模块就搭建完成,接下来我们搭建数据库和页面

3.我们创建数据库:

/*
SQLyog 企业版 - MySQL GUI v8.14
MySQL - 5.1.49-community : Database - db_jsp
*********************************************************************
*/

/*!40101 SET NAMES utf8 */;

/*!40101 SET SQL_MODE=''*/;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/`db_jsp` /*!40100 DEFAULT CHARACTER SET utf8 */;

USE `db_jsp`;

/*Table structure for table `t_user` */

DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(20) DEFAULT NULL,
`password` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

/*Data for the table `t_user` */

insert into `4887王中王鉄算盘奖结果,t_user`(`id`,`userName`,`password`) values (1,'java','56');

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

4.我们搭建前台的页面:

4-1.登陆页面:login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ";
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login" method="post">
<table>
<tr>
<th colspan="2">用户登录</th>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" id="userName" name="userName" value="${userName }"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" id="password" name="password" value="${password }"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>

<!---此处为el表达式我们后面在学习哈,看看效果---->
<td><font color="red">${error }</font></td>
</tr>
</table>
</form>
</body>
</html>

 

4-2.我们登陆成功后跳转的页面:main.jsp

 

本文由4887王中王鉄算盘奖结果发布于王中王鉄算盘,转载请注明出处:用户登入成效完毕,轻便案列完美化解Mvc设计方

关键词:

最火资讯