SpringMVC实现简单登录

1.基本环境:eclipse+jdk1.8+Tomcat9.0+springmvc各种jar

2.把jar导入到lib下,在build path中添加这些jar

3.创建一个server,作为运行时用。

4.目录如下:

SpringMVC实现简单登录_第1张图片

index.jsp

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




Insert title here


	


succ.jsp

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




成功


	
<% String name = (String)request.getAttribute("username"); String pwd = (String)request.getAttribute("password"); %>

用户名:<%=name %>

密码:<%=pwd %>


applicationContext.xml



springmvc.xml



  
    
    
        
        
     


    
       

web.xml



  ThirdPage
  
    index.jsp
  
 
  
	org.springframework.web.context.ContextLoaderListener      
  
  
  
        contextConfigLocation
        classpath*:applicationContext.xml
  
  
  
      springmvc
      org.springframework.web.servlet.DispatcherServlet
      
          contextConfigLocation
          /WEB-INF/classes/springmvc.xml
      
      1
  
  
  
       springmvc
        *.do
  

Usercontroller.java

package com.chunyan.spring;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class UserController{
	@RequestMapping("/login")
	public String login(String username, String password, HttpServletRequest request) {
		System.out.println("用户名:"+username+"\t密码:"+password); 
		request.setAttribute("username", username);
		request.setAttribute("password", password);
		
		return "succ";
	}
}

运行结果:

SpringMVC实现简单登录_第2张图片

SpringMVC实现简单登录_第3张图片

你可能感兴趣的:(SpringMVC)