JSP第十周作业

数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)

1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
<script type="text/javascript">
        function validate(){
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="dologin.jsp" method="post">
        
    用户名:<input type="text" name="uname" value="aa"><br> 
    密码: <input  type="password" name="upwd"  value="aa">
    
        <input type="button" value="登录" onClick="validate()">    
        <a href="zhuce.jsp">点击注册</a>

    </form>
</body>
</html>
<%@page import="com.nn.Dao.StuDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
<%
    StuDao sd=new StuDao();
    request.setCharacterEncoding("utf-8");
    String uname = request.getParameter("uname");
    String upwd = request.getParameter("upwd");
    if (sd.login(uname, upwd)){
        session.setAttribute("uname", uname);
        request.getRequestDispatcher("main.jsp").forward(request, response);
    }else{
        out.print("登陆失败,即将跳回登陆页.....");
        response.setHeader("refresh", "5;url=login.jsp");
    }

 %>

</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
</head>

<body>
<script type="text/javascript">
        function validate(){
            if(loginForm.stuid.value==""){
                alert("id不能为空!");
                return;
            }
            if(loginForm.uname.value==""){
                alert("账号不能为空!");
                return;
            }
            if(loginForm.upwd.value==""){
                alert("密码不能为空!");
                return;
            }
            if(loginForm.upwd1.value==""){
                alert("确认密码不能为空!");
                return;
            }
            if(loginForm.uage.value==""){
                alert("年龄不能为空!");
                return;
            }
            loginForm.submit();
        }
    </script>


    <form name="loginForm" action="dozhuce.jsp" method="post">
    stuid:<input type="number" name="stuid" ><br> 
    用户名:<input type="text" name="uname" value="aa"><br> 
    密码: <input  type="password" name="upwd"  value="aa"><br> 
    确认密码: <input  type="password" name="upwd1"  value="aa"><br> 
    年龄:<input type="number" name="uage" ><br> 
    <a href="login.jsp">返回登录</a>
        <input type="button" value="注册" onClick="validate()">    
    </form>

</body>
</html>
<%@page import="com.nn.Dao.StuDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>

<body>
<%
    
    request.setCharacterEncoding("utf-8");
    
    String id =request.getParameter("stuid");
    Integer stuid =Integer.parseInt(id);
    
    String uname = request.getParameter("uname");
    
    String upwd = request.getParameter("upwd");
    
    String upwd1 = request.getParameter("upwd1");
    
    String age =request.getParameter("uage");
    Integer uage =Integer.parseInt(age);
    
        if((upwd.equals(upwd1))){
            StuDao sd=new StuDao();
            sd.reg(stuid, uname, upwd, uage);
            out.print("注册成功,即将跳回登录页.....");
            response.setHeader("refresh", "5;url=login.jsp");
    }
    else{
        out.print("两次密码不一致,即将跳回注册页.....");
            response.setHeader("refresh", "5;url=zhuce.jsp");
    }
        
    
    
 %>
</body>
</html>
<%@page import="com.nn.Dao.Product"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>
<head>
</head>

<body>
欢迎你${uname}   <a href="logout.jsp">退出登陆</a><br>
您购物车是:<Br>
<%
//在session中拿到集合,遍历
    List<Product> list=(ArrayList<Product>)session.getAttribute("list");
    if(list!=null){
        for(int i=0;i<list.size();i++){
            out.print(list.get(i).getName()+","+list.get(i).getPrice());
            out.print("<a href='del.jsp?name="+list.get(i).getName()+"'>删除</a><br>");
            
        }
        
        
        
    }
 %>
<a href="stulist.jsp">所有学生列表</a>

</html>
package com.nn.Dao;

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

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class BaseDao {


    //获取连接
    protected Connection getConnection(){
        Connection conn=null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                // 2.建立连接
                conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/test", "root", "501002");
            } catch (Exception e) {
                e.printStackTrace();
            } 
            return conn;
    }    
    

    
    
    //关闭连接
    protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
    try {
        if(rs != null)
            rs.close();
        if(ps != null)
            ps.close();
        if(con != null)
            con.close();
        
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
}
package com.nn.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StuDao extends BaseDao {

    public boolean login(String name, String pwd) {
        boolean f = false;
        Connection conn = getConnection();
        String sql = "select * from stu where uname=? and upwd=?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, pwd);
            rs = ps.executeQuery();
            if (rs.next())
                f = true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, rs);
        }
        return f;
    }

    public void reg(Integer stuid,String uname, String upwd,Integer uage) {
        Connection conn = getConnection();
        PreparedStatement ps = null;
        try {
            String sql = "insert into stu(stuid,uname,upwd,uage) values(?,?,?,?)"; // 2个占位符
            // 4.执行SQL语句
            ps = conn.prepareStatement(sql);
            
            ps.setInt(1, stuid);
            ps.setString(2, uname);
            ps.setString(3, upwd);
            ps.setInt(4, uage);
            ps.executeUpdate();// 增删改都用这个
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            closeAll(conn, ps, null);
        }

    }
}

 

 

 

 

 

posted @ 2022-05-08 21:12  F85  阅读(5)  评论(0编辑  收藏  举报