欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSP 标签

程序员文章站 2022-06-08 23:24:20
...

index.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>JSP常用标签</title>
</head>
<body>
	<%-- jsp:include与pageContext.include()效果一样 动态包含,把执行结果聚合包含--%>
	<jsp:include page="/login.jsp"></jsp:include>
	
	
	<%-- jsp:forward 跳转页面 --%>
	<%--
	<jsp:forward page="/Servlet">
		<jsp:param name="username" value="xxxx"></jsp:param>
	</jsp:forward>
	--%>
	
	
	<%--jsp:userbean 实例化并获取JavaBean(实体类)对象,如果已经实例化就不再实例化,而是直接获取已经实例化的JavaBean对象 --%>
	<jsp:useBean id="user1" class="cn.et.User" scope="session">
		<%-- useBean标签体只在useBean标签实例化JavaBean时执行 --%>
		Session会话中创建一个User对象
	</jsp:useBean>
	<%--
	http://localhost:8080/JSP3/index.jsp?username=lu&userage=20
	jsp:setProperty 支持8种基本数据类型的自动转换
	--%>
	<jsp:setProperty name="user1" property="name" value="ul"/>
	<jsp:setProperty name="user1" property="age" value="02"/>
	<%-- <jsp:setProperty name="user1" property="birthday" value="<%=new Date()%>"/>  --%>
	<%=user1%>
	
	<br/>
	<jsp:useBean id="user2" class="cn.et.User" scope="page">
		<%-- useBean标签体只在useBean标签实例化JavaBean时执行 --%>
		每访问一次,创建一个User对象
	</jsp:useBean>
	<%--http://localhost:8080/JSP3/index.jsp?username=lu&userage=20--%>
	<%--<jsp:setProperty name="user2" property="name" param="*"/>--%>
	<jsp:setProperty name="user2" property="name" param="username"/>
	<jsp:setProperty name="user2" property="age" param="userage"/>
	<%=user2%>
	<%--
	<%user2.getName()>
	<jsp:getProperty name="user2" property="name"/>
	<jsp:getProperty name="user2" property="age"/>
	<%user2.getAge()>
	--%>
</body>
</html>

login.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>
		<script type="text/javascript">
			/*用户名验证*/
			function check_name() {
				var nameNode = document.getElementsByName("user")[0];
				var name = nameNode.value;
				var spanNode = document.getElementById("userNameSpan");
				if(name.length > 6){
					spanNode.innerHTML = "用户名不能超过6个字".fontcolor("red");
					return false;
				} 
				if(name.length == 0) {
					spanNode.innerHTML = "用户名不能为空".fontcolor("red");
					return false;
				}
				spanNode.innerHTML="";
				return true;
			}
			/*密码验证*/
			function check_password() {
				var nameNode = document.getElementsByName("password")[0];
				var name = nameNode.value;
				var spanNode = document.getElementById("userPassSpan");
				if(name.length > 16){
					spanNode.innerHTML = "密码不能超过16个字".fontcolor("red");
					return false;
				}
				if(name.length == 0) {
					spanNode.innerHTML = "密码不能为空".fontcolor("red");
					return false;
				}
				spanNode.innerHTML="";
				return true;
			}
			function submitForm() {
				return check_name()&&check_password();
			}
		</script>
	</head>
	<body>
		
		<form align="center" action="#" method="get" onsubmit="return submitForm()">
			<h1>登  入</h1>
			用  户<input type="text" name="user" onblur="check_name()" placeholder="请输入账号"/><br> 
			<span id="userNameSpan"></span><br/>
			密  码<input type="password" name="password" onblur="check_password()" placeholder="请输入密码"/><br> 
			<span id="userPassSpan"></span><br/>
			<input type="submit" value="提交" />
			<input type="reset" value="重置"/><br/>
			<a href="register.html">我要注册</a>
		</form>
	</body>
</html>
User.java

package cn.et;

public class User {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + "]";
	}
}