基于MVC模式的用户登录

第1关:编写用户登录页面。

本关我们要实现的是登陆功能的第一步:编写登陆表单,在jsp中编写表单,设置用户名字段userName和用户密码字段password,并设置请求的servlet路径为login

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form class="form" action="login" method="post">
		
		<div>
		username:<input type="text" name="userName"><br> 
		password:<input type="password" name="password"><br>
		<span><input type="submit" value="提交"></span> 
		
		</div>
	</form>
    
</body>
</html>

第2关:登录验证

借助JDBC在库名university中完成对数据表student数据的查询,来验证前台传过来的用户名称字段userName和密码字段password是否匹配;127.0.0.1:3306mysql服务器地址及端口 数据库编码格式设置为utf-8MySQL的用户名为root,密码为123123,我们需要根据这些属性来连接数据库。

package chapter9;

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

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

public class LoginServlet extends HttpServlet {

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

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

	@Override
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
			/********* Begin *********/
			String username=req.getParameter("userName");
            String password=req.getParameter("password");
            StudentBean stu=new StudentBean();
            stu.setUserName(username);
            stu.setPassword(password);
            Connection con=null;
            try{
                Class.forName("com.mysql.jdbc.Driver");
                String url="jdbc:mysql://127.0.0.1:3306/university?characterEncoding=UTF-8";
                String sqlusername="root";
                String sqlpassword="123123";
                con=DriverManager.getConnection(url,sqlusername,sqlpassword);
            }catch(ClassNotFoundException | SQLException e){
                e.printStackTrace();
            }
           
		    PreparedStatement pstmt = null;
		    ResultSet rs = null;
            int judge=0;
		    try {
			    String sql = "select * from student where USER_NAME='"+username+"'"+"and PASSWORD='"+password+"'";
			    pstmt = con.prepareStatement(sql);
			    rs = pstmt.executeQuery();//
                
			    while(rs.next()) {
                    judge=1;
				    stu.setStudentId(rs.getInt(1));
                    stu.setSex(rs.getString(4));
                    stu.setAge(rs.getInt(5));
                    stu.setDept(rs.getString(6));
			    }
			
            }catch(Exception e){
                e.printStackTrace();
            }
            if(judge==1){
                req.getSession().setAttribute("account", stu);
                resp.sendRedirect("success.jsp");
            }
            if(judge==0){
                 req.getSession().setAttribute("account", stu);
                 resp.sendRedirect("fail.jsp");
            }
            judge=0;
			
			/********* End *********/
		
		
	}
}

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值