第十周Jsp作业

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

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
  </head>
  
  <body>
 <script type="text/javascript">
        function date(){
            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="date()">    
        <a href="coinn.jsp">还没有账号?点击注册</a>
        </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="yy.m.xr.Stu"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>

  </head>
  
  <body>
   <%
    Stu sd=new Stu();
    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"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
   <script type="text/javascript">
        function date(){
            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="docoin.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="date()">    
    </form>

  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="yy.m.xr.Stu"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </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))){
            Stu sd=new Stu();
            sd.reg(stuid, uname, upwd, uage);
            out.print("注册成功,即将跳回登录页.....");
            response.setHeader("refresh", "5;url=login.jsp");
    }
    else{
        out.print("两次密码不一致,即将跳回注册页.....");
            response.setHeader("refresh", "5;url=coinn.jsp");
    }
        
    
    
 %>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
  </head>
  
  <body>
   <%
  String uname=(String)session.getAttribute("uname");
  //如果他是空,说明没登陆,直接访问该页面了
  if(uname==null)
      response.sendRedirect("login.jsp");
   %>
   欢迎你<%=uname %>
  <a href="exit.jsp">退出登录</a> <br>
  </body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  
  <body>
   <%
    session.invalidate();
    response.setHeader("refresh", "5;url=login.jsp");
  %>
退出成功!
  </body>
</html>

 

posted @ 2022-05-08 12:16  星星邮寄者  阅读(28)  评论(0编辑  收藏  举报