jsp杂记

1.out.print和<%= %>的区别
out.print(““);
String alter=”验证码不正确”;<%=alter %>
2.jsp跨页面验证并返回上级页面弹出提示,注释部分重点
上级页面代码

<%@ 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=UTF-8">
<title>注册</title>
<style type="text/css">
#hand{
    text-align:center;
    font-weight:bold;
}
#form1{
    text-align: center;
}
#div1{
    margin-top:30px;
    }
#div2{
    margin-top:30px;
    }
#div3{
    margin-top:20px;
    }
#div4{
    margin-top:20px;
    }
input[type="submit"]{
    width:120px;
    height:30px;
    background-color:#09C;
    margin-left:50px;
    }
#userName{
    width:250px;
    height:30px;
    }
#code{
    height:30px;
    }
input[type="password"]{
    width:250px;
    height:30px;
    }
</style>
<%--本页面验证提示 --%>
<script type="text/javascript">
    function checkUserInfo(){
        var userName=document.getElementById("userName").value;
        var pwd=document.getElementById("pwd").value;
        var codes=document.getElementById("codes").value;
        if(userName.length<=0 ||pwd.length<=0 || codes.length<=0){
            alert("请输入完整信息")
            return false;
        }
        else{
            return true;
        }
    }
</script>

</head>
<body>
<%
    //接收下级页面UserInfo返回过来的状态码status,用UserInfo页面status的值来判断本页面弹出的提示
    String status=request.getParameter("status");
    if(status!="" && status!=null){
        if(status.equals("0")){
            out.print("<script type='text/javascript'>alert('验证码不正确!');</script>");
        }
        if(status.equals("1")){
            out.print("<script type='text/javascript'>alert('用户名或密码错误!');</script>");
        }
    }
%>
<div id="hand">请 输 入 注 册 信 息</div>
<!--post请求方式-->
   <form id="form1" method="post" action="UserInfo.jsp" onsubmit="return checkUserInfo()">
        <div id="div1">用户名:<input type="text" name="userName" id="userName"></div>
        <div id="div2"> 密&nbsp;&nbsp;&nbsp;码:<input type="password" name="pwd" id="pwd"></div>
        <div id="div3">验证码&nbsp;&nbsp;&nbsp;<img border=0 src="image.jsp" id="imgRandom" onclick="this.src='image.jsp?abc='+Math.random()">
        <input type="text" id="codes" name="codes">
        </div>
        <div id="div4"><input type="submit" value="确定" name="confirm"></div>
   </form>
 <%
    session.setAttribute("name", "admin");
    session.setAttribute("pwd", "123456");
%>
</body>
</html>

userInfo代码

<%@page import="javafx.scene.control.Alert"%>
<%@page import="javax.swing.JOptionPane"%>
<%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%  
    String userName=request.getParameter("userName");
    String pwd=request.getParameter("pwd");
    String code=request.getParameter("codes");  
    String codeSessin=session.getAttribute("code").toString();

    if(userName.equals("admin") && pwd.equals("123456"))
    {
        if( codeSessin.equals(code)){  
            response.sendRedirect("MainPage.jsp");
        }       
        else{       
        //这自定义状态码status,传到regis以用来辨识弹出状态
            response.sendRedirect("Register.jsp?status=0");
        }
    }
    else{
        response.sendRedirect("Register.jsp?status=1");
    }

%>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值