javaweb学习(九)----MVC案例:登录

MVC案例 <登录>

模型图

在这里插入图片描述

代码

  • 目录
    在这里插入图片描述
  • 代码
    index.jsp
<%@ 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>
<style>
.box{
    position: absolute;/*生成绝对定位的元素,元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。(此处相对于body)*/
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);/* 实现块元素百分比下居中*/
    width: 400px;
    padding: 40px;/*设置四个内边距*/
    background: rgba(0,0,0,0.8);/*函数的前三个变量为颜色(黑),后一个为透明度(1为不透明)*/
    box-sizing: border-box;/*使实际所占宽高度 = 设置的高度(height)/ 设置的宽度(width)+ 外边距(margin)*/
    box-shadow: 0 15px 25px rgba(0,0,0,.5);/*产生盒子阴影:x轴偏移,y轴偏移,模糊度,透明色*/
    border-radius: 10px;/*使矩形四边圆滑*/
}
.box h2{
    margin: 0 0 30px;/*上、左右、下*/
    padding: 0;
    color: #ffffff;/*设置字体颜色*/
    text-align: center;/*文字居中*/
}
.box .inputBox{
    position: relative;/*相对定位*/
}
.box .inputBox input{
    width: 100%;
    padding: 10px 0;/*上下、左右*/
    font-size: 16px;/*字体大小(可用于调节输入框高)*/
    color: #ffffff;/*输入框颜色*/
    letter-spacing: 1px;/*设置对象中的文字之间的间隔*/
    margin-bottom: 30px;/*设置下外距*/
    border: none;/*定义无边框*/
    border-bottom: 1px solid #ffffff;/*底边框:大小1PX 实线 颜色#ffffff */
    outline: none;/*设置无外边框*/
    background: transparent;/*背景设置为透明*/
}
.box .inputBox label{
    position: absolute;
    top: 0;
    left: 0;
    letter-spacing: 1px;
    padding: 10px 0;
    font-size: 16px;
    color: #ffffff;
    pointer-events:none;/*阻止区块被点击,使得点击穿透*/
    transition: 1s;/*动画运行时间*/
}
/*产生对应操作时,label发生的样式变化*/
.box .inputBox input:focus~label,
.box .inputBox input:valid~label
{
    top:-18px;
    left: 0;
    color: #03a9f4;
    font-size: 12px;
}
.box input[type="submit"]{
   background: transparent;
    border: none;
    outline: none;
    color: #ffffff;
    background: #03a9f4;
    margin: 0px 80px;
    padding: 10px 30px;
    cursor: pointer;/*鼠标指针变成手的形状*/
    border-radius: 15px;/*调节矩形弧度*/
}
</style>

<body>
	<div class="box">
        <h2>Usermanage</h2>
        <!--创建供用户输入的 HTML 表单-->
        <form action="LoginServlet" method="post">
            <div class="inputBox">
                <input type="text" name="uname" required="">
                <label>用户名</label>
            </div>
            <div class="inputBox">
                <input type="password" name="upwd" required="">
                <label>密码</label>
            </div>
            <div align="center">
                <input type="submit" name="" value="登录">
            </div>
            

        </form>
    </div>
</body>
</html>

welcome.jsp

<%@ 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>
</head>
<body>
	登陆成功!
</body>
</html>

LoginServlet.java

package com.zr.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.dao.LoginDao;
import com.zr.entity.Login;

//控制器层:接收view请求,并分发给Model处理
public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//处理登录
		//数据组装
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("uname");
		String pwd = request.getParameter("upwd");
		Login login=new Login(name,pwd);
		//调用模型层的登录功能
		int result = LoginDao.login(login);
		if(result>0) {//成功
			response.sendRedirect("welcome.jsp");
		}else {//登陆失败
			response.sendRedirect("index.jsp");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

LoginDao.java

package com.zr.dao;

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

import com.zr.entity.Login;

//模型层,用于处理登录(查询数据库)--功能类
public class LoginDao {
	public static int login(Login login) {
		int flag=-1;//-1:系统异常;0:用户名或密码有误;1:登陆成功
		int result=-1;
		Connection connection=null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			//加载驱动
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			//连接
			connection = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=用户注册库","sa","1234567");
			String sql="select count(*) from tb_user where username=? and password=?";
			pstmt = connection.prepareStatement(sql);
			pstmt.setString(1, login.getUsername());
			pstmt.setString(2, login.getPassword());
			rs = pstmt.executeQuery();
			if(rs.next()) {
				result=rs.getInt(1);
			}
			if(result>0) {
				return 1;
			}else {
				return 0;
			}
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return -1;
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return -1;
		}catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
			return -1;
		}finally {
			if(rs!=null)
				try {
					if(rs!=null) rs.close();
					if(pstmt!=null) pstmt.close();
					if(connection!=null) connection.close();
					
				} catch (SQLException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			
		}
	}
}

Login.java

package com.zr.entity;
//实体类
public class Login {
	private String username;
	private String password;
	private String sex;
	private String question;
	private String answer;
	private String 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 String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getQuestion() {
		return question;
	}
	public void setQuestion(String question) {
		this.question = question;
	}
	public String getAnswer() {
		return answer;
	}
	public void setAnswer(String answer) {
		this.answer = answer;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Login(String username, String password, String sex, String question, String answer, String email) {
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.question = question;
		this.answer = answer;
		this.email = email;
	}
	public Login(String username, String password) {
		this.username = username;
		this.password = password;
	}
	public Login() {
		
	}
	
}

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>MVCsample</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</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>
  <servlet-name>LoginServlet</servlet-name>
  <servlet-class>com.zr.servlet.LoginServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>LoginServlet</servlet-name>
  <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
</web-app>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值