jsp原理
访问 jsp 页面时,jsp 页面会被翻译成 .java 文件
.java 文件会被编译成 .class 文件(字节码文件)
例如:在网页中显示当前时间
<%@ page import="java.util.Date"%>
<%@ 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>
<%
// 正常写 Java 代码
Date date = new Date();
out.print(date.toLocaleString());
// 被废弃的方法,使用仅为测试
%>
</body>
</html>
纯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>Insert title here</title>
</head>
<body>
<form action="/Demo/dologin.jsp" method="post">
账号:<input type="text" name="username">
<br>
密码:<input type="password" name="password">
<br>
<input type="submit" value="登录">
<br>
<%
String msg = (String)request.getAttribute("msg");
if(msg != null){
out.print(msg);
}
%>
</form>
</body>
</html>
<%@ 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 password = request.getParameter("password");
// 处理逻辑
if(username.equals("test") && password.equals("123")){
// 登录成功
// 跳转页面 -> 欢迎页面,并且显示名字
// 传值时,如果是非表单提交的数据,可以保存在域对象中
// 如果是表单提交过来的数据,直接使用请求转发就行了
request.getRequestDispatcher("/success.jsp").forward(request, response);
} else {
// 登录失败 返回登录页面
// 相对于服务器内,不用添加工程名,例如:请求转发
// 相对于服务器外(相对于网址的8080后),需要添加工程名,例如:请求重定向
// 失败后,在页面中显示错误信息
// 使用请求转发,比较合适
request.setAttribute("msg", "登录失败,请确认用户名和密码!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
// 跳转页面
%>
</body>
</html>
<%@ 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>
<%
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
%>
<h1>欢迎你
<%
// String username = (String)session.getAttribute("username");
String username = request.getParameter("username");
out.write(username);
%>
</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page session="true" %>
<!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>
<%-- jsp注释:安全效率高 --%>
<%!
// 加上叹号会被翻译到类的下面,即为全局变量
// 还可以声明方法,静态块
// 可以这么用,但是一般没人这么去写!
int num2 = 10;
%>
<%
// 测试session开关
session.getAttribute("123123");
int num1 = 10;
num1++;
num2++;
// out.print(num1);
%>
<%=num1+2 %>
<%=num2 %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page errorPage="/error.jsp" %>
<!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>
<%
int num = 10 / 0;
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
<!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 msg = exception.getMessage();
out.print(msg);
%>
服务器正在紧张的建设当中...请稍后重试
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--@ include file="/4.jsp" --%>
<%-- <jsp:include page="/4.jsp"></jsp:include> --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<c:if test=""></c:if>
bbbbbb
</body>
</html>
<%@ 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>
aaaaaaaa
</body>
</html>
<%@ page import="Demo.User"%>
<%@ 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>
<%
User user = new User();
user.setName("wanglong");
user.setPwd("123");
out.print(user.getName());
%>
<br>
<jsp:useBean id="user1" class="Demo.User"></jsp:useBean>
<%-- <jsp:useBean id="user1" class="Demo.User"/> --%>
<jsp:setProperty property="name" name="user1" value="dingpeng" />
<jsp:setProperty property="pwd" name="user1" value="456" />
<%=user1 %>
<br>
<jsp:getProperty property="name" name="user1"/>
<jsp:getProperty property="pwd" name="user1"/>
</body>
</html>
public class User {
private String name;
private String pwd;
public User() {
super();
}
public User(String name, String pwd) {
super();
this.name = name;
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [name=" + name + ", pwd=" + pwd + "]";
}
}