动力节点旗下在线教育品牌  |  咨询热线:400-8080-105 学Java全栈,上蛙课网
首页 > 文章

实例实现SpringMVC用户登录页面

08-17 17:46 104浏览
举报 T字号
  • 大字
  • 中字
  • 小字

无论是哪个前端框架,基本上都需要开发用户登录页面,当然SpringMVC框架也不例外。SpringMVC用户登录页面的实现是SpringMVC框架的基本功能实现,也是SpringMVC框架的基础知识。

1.首先我们需要建立Dynamic Web Project

项目结构可以参考下图:

2.然后配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.wkctocom/2001/XMLSchema-instance
					http://www.springmodules.org/schema/cache/springmodules-cache.xsd					http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd" 
		 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>mybatis06-spring-springmvc</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
<!-- 对spring的支持   -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:beans.xml</param-value>
  </context-param>
    	<!-- 过滤器 -->
  <filter> 
       <filter-name>characterEncodingFilter</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> 
       <init-param> 
           <param-name>forceEncoding</param-name> 
           <param-value>true</param-value> 
       </init-param> 
   </filter> 
   <filter-mapping> 
       <filter-name>characterEncodingFilter</filter-name> 
       <url-pattern>/*</url-pattern> 
   </filter-mapping> 
   
  	<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
  
<!-- 对springmvc的支持   -->
    <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>
  	
  	<load-on-startup>1</load-on-startup>
  	<async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

3.新建用户登录界面,Login.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.wkcto.com/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>用户登录</title>
</head>
<body>
<center>
    <h2>用户登录</h2>
    <form action="LoginConf" method="post">
    <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="username"></td>
        </tr>
        <tr>
            <td>密&nbsp;&nbsp;码:</td>
            <td><input type="password" name="userpassword"></td>
        </tr>
        <tr>
            <td colspan="2">
            <input type="submit" value="登录">
            <input type="reset" value="重置">
            </td>
        </tr>
    </table>
    </form>
</center>
</body>
</html>

4.创建用户登录成功页面,Login_success.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.wkcto.com/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录成功</title>
</head>
<body>
<center>
    <%
        if(session.getAttribute("login")!=null && session.getAttribute("login").equals("true")){
    %>
    <h2>登录成功</h2>
    <%
        }else{
    %>
        <jsp:forward page="Login.jsp"></jsp:forward>
    <%
        }
    %>
</center>   
</body>
</html>

5.创建用户登录失败页面,Login_failure.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.wkcto.com/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录失败</title>
</head>
<body>
<center>
    <h2>登录失败</h2>
</center>
</body>
</html>

6.新建业务逻辑组件,用来负责业务逻辑的处理,LoginCheck.java

package web_project;

public class LoginCheck {

    //判断是否为合法用户

    public boolean isLogin(String username,String userpassword){

        if("zch".equals(username)&&"1234".equals(userpassword)){

            System.out.print("access"+username+userpassword);

            return true;

        }else{

            System.out.print("denied:"+username+userpassword);

            return false;

        }

    }

 

}

7.创建Servlet控制器,LoginConf.java

负责接收用户参数,并调用业务逻辑组件进行判断,最后还负责页面的跳转。Servlet控制器是整合MVC设计模式的核心。

 

package web_project;

 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

 

/**

 * Servlet implementation class LoginConf

 */

 

public class LoginConf extends HttpServlet {

 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 接收用户参数

        String username = request.getParameter("username");

        String userpassword = request.getParameter("userpassword");

 

        //调试查看参数是否传进来

        System.out.print(username+userpassword);

 

        //获得session对象

        HttpSession session = request.getSession();

        //new一个LoginCheck对象

        LoginCheck lc= new LoginCheck();

        //调用业务逻辑组件的判断功能来判断

        if(lc.isLogin(username,userpassword)){

            //如果为合法用户,在request范围中添加属性login,其属性值为true,并跳转到登录成功页

            session.setAttribute("login", "true");

            request.getRequestDispatcher("Login_success.jsp").forward(request, response);

 

        }else{

            //如果非法用户跳转到登录失败页

            request.getRequestDispatcher("Login_failure.jsp").forward(request, response);

        }

    }

 

 

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request, response);

    }

 

}

以上实现SpringMVC用户登录页面实例,你学到了吗?当然,每个人使用SpringMVC框架开发出的用户登录页面都是不一样的,本文只是给小伙伴们介绍了一个大方向。具体的功能和细节可能因人而异,需要具体考究。你也可以根据自己的风格设计出独树一帜的用户登录页面!

 

 

 

0人推荐
共同学习,写下你的评论
0条评论
代码小兵1268
程序员代码小兵1268

11篇文章贡献53362字

作者相关文章更多>

推荐相关文章更多>

DOM渲染的详细过程

QCode09-04 14:38

CSS水平和垂直居中技巧大梳理

Code大师09-04 14:50

mui的input框在IOS系统下无法聚焦或点击多次才能聚焦

不写代码你养我啊08-23 11:14

推荐的-视.频播放器以及在线客服

不写代码你养我啊09-17 18:02

谈谈java多线程的三大特性

要学习了06-18 18:13

发评论

举报

0/150

取消