/*生成博客目录的CSS*/ #uprightsideBar{ font-size:12px; font-family:Arial, Helvetica, sans-serif; text-align:left; position:fixed;/*将div的位置固定到距离top:50px,right:0px的位置,这样div就会处在最右边的位置,距离顶部50px*/ top:50px; right:0px; width: auto; height: auto; } #sideBarTab{ /* float:left; */ width:100px; border:1px solid #e5e5e5; border-right:none; text-align:center; background:#ffffff; cursor:pointer; } #sideBarContents{ float:left; overflow:auto; overflow-x:hidden;!important; width:300px; min-height:108px; max-height:460px; border:1px solid #e5e5e5; border-right:none; background:#ffffff; } #sideBarContents dl{ margin:0; padding:0; } #sideBarContents dt{ margin-top:5px; margin-left:5px; } #sideBarContents dd, dt { cursor: pointer; } #sideBarContents dd:hover, dt:hover { color:#A7995A; } #sideBarContents dd{ margin-left:20px; } #sideBarContents dd.indent3{ margin-left:40px; } #sideBarContents dd.indent4{ margin-left:60px; } #sideBarContents dd.indent5{ margin-left:80px; } #sideBarContents dd.indent6{ margin-left:100px; }

Java Web高级应用

基于MVC模式的用户登陆

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

  • 任务描述

本次实训我们要实现一个MVC的登陆功能:

本关我们要实现的是登陆功能的第一步:编写登陆表单,在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>登录</title>
</head>
<body>
    <form action="login" method="get">
    <table>
        <tr>
            <td>username:</td>
            <td><input type="text" name="userName"></td>
        </tr>
        <tr>
            <td>password:</td>
            <td><input type="password" name="password"></td>
        </tr>
    </table>
</body>
</html>

第2关:登录验证

package chapter9;

import java.io.IOException;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginServlet extends HttpServlet {

	/**
	 * 数据库相关
	 */
    private static final String url = "jdbc:mysql://127.0.0.1:3306/university?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true";
    private static final String db_user = "root";
    private static final String db_password = "123123";

    private static Connection conn = null;
    private static PreparedStatement stmt = null;
    private static ResultSet rs = null;

	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");

            try{
                // 加载驱动程序
                Class.forName("com.mysql.jdbc.Driver");
                // 连接数据库
                conn = DriverManager.getConnection(url, db_user, db_password);
                String sql = "select * from student where user_name=? and password=?";
                stmt = conn.prepareStatement(sql);
                stmt.setString(1, userName);
                stmt.setString(2, password);

                rs = stmt.executeQuery();

                HttpSession session = req.getSession();

                if(rs.next()){
                    StudentBean student = new StudentBean();
                    
                    student.setUserName(rs.getString("user_name"));
                    student.setPassword(rs.getString("password"));
                    student.setStudentId(rs.getInt("student_id"));
                    student.setSex(rs.getString("sex"));
                    student.setAge(rs.getInt("age"));
                    student.setDept(rs.getString("dept"));

                    session.setAttribute("account", student);
                    resp.sendRedirect("success.jsp");
                }else{
                    StudentBean student = new StudentBean();
                    student.setUserName(userName);
                    student.setPassword(password);

                    session.setAttribute("account", student);
                    resp.sendRedirect("fail.jsp");
                }
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }catch(SQLException e){
                e.printStackTrace();
            }

			/********* End *********/
	}
}

posted @ 2023-10-13 21:02  你的脑子能压几个栈  阅读(112)  评论(0)    收藏  举报