您的位置:首页 > Web前端 > JavaScript

jsp杂记

2016-09-04 11:36 323 查看
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"> 密   码:<input type="password" name="pwd" id="pwd"></div>
<div id="div3">验证码   <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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: