jsp第六周作业

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>register</title>
<style type="text/css" ></style>
</head>
<body>
 
<form action="success1.jsp" method="post">
    用户名:<input type="text" name="uname"><br>
    密   码:<input type="password" name="upwd"><br>
    验证码: 
          <input type="text" name="code_text" size="3" /> 
          <img src="image/yzm.jpeg"
            id="img_src" width=80 heighe=40/> 
          <a href="image/date.jpg">换一张</a> <br>
    <input type="submit" value="提交">
</form>
     
</body>
</html>

success.jsp

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import=" javax.servlet.http.HttpSession"
    %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
 HttpSession s = request.getSession();

 String username = request.getParameter("uname");
 String password1 = request.getParameter("upwd");
     Class clazz = Class.forName("com.mysql.jdbc.Driver");
    // 2.提供另外三个连接的基本信息
    String url = "jdbc:mysql://localhost:3306/school";
    String user = "root";
    String password = "root";
    Connection conn = DriverManager.getConnection(url, user, password);
    PreparedStatement ps = conn.prepareStatement("select uname,upwd from user where uname = ? and upwd = ?");
    ps.setString(1, username);
    ps.setString(2, password1);
    ResultSet rs = ps.executeQuery();
    if(rs.next()){
         session.setAttribute("username", username);
         session.setAttribute("password", password1);
         response.sendRedirect("success1.jsp");
    }else{
        response.sendRedirect("login.jsp");
    }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

success1.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"
    import=" javas.servlet.http.HttpSession"
    %>
<!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>
<body>
<%
HttpSession s = request.getSession();
String username = s.getAttribute("uname").toString();
String password = s.getAttribute("upwd").toString();

%>
    用户名 : <%=username %><br/>
    密码  : <%=password %>
</body>
</html>

posted @ 2021-04-10 21:37  white_poplar  阅读(40)  评论(0编辑  收藏  举报