JAVA遇见HTML——JSP篇(学习笔记)
学习资源路径 https://www.imooc.com/learn/166
结构如下
通过http://localhost:8080/myhome/index.jsp即可访问myhome下的index.jsp文件。
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> this is the first page <!-- html注释 客户端可见 --> <%-- html注释 客户端不可见 --%> <% out.println("哈哈"); // 脚本中注释,不可见 %> <%!String abc = "哈哈";%> <%=abc%> </body> </html> |
out对象
request对象
中文参数乱码问题
①设置请求编码
request.setCharacterEncoding(“UTF-8”)的作用是设置对客户端请求和数据库取值时的编码,不指定的话使用iso-8859-1。(只解决POST乱码) |
②server.xml设置默认编码
response对象
session对象
<%@ page language="java" import="java.util.*,java.text.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <!-- session创建时间戳 --> <%= session.getCreationTime() %> <br/> <% SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); // session创建时间 out.println(simpleDateFormat.format(new Date(session.getCreationTime()))); // session_id out.println("sessionId:"+session.getId()); // 设置session_id关联的属性值 session.setAttribute("username", "Mr.Wang"); // 获取session_id关联的属性值 out.println(session.getAttribute("username")); session.setAttribute("age", 11); // 获取session_id关联的属性名 String[] values = session.getValueNames(); for(int i = 0;i<values.length;i++){ out.println(); out.println(values[i]+':'+session.getAttribute(values[i])); } // 获得session的最大存活时间,默认是1800秒 out.println(session.getMaxInactiveInterval()); // 设置session的最大存活时间 session.setMaxInactiveInterval(1000); out.println(session.getMaxInactiveInterval()); // 销毁会话 session.invalidate(); %> </body> </html> |
设置session超时时间
application对象
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% application.setAttribute("city", "JiNing"); application.setAttribute("area", "WeiShan"); %> 城市:<%=application.getAttribute("city")%><br/> 县区:<%=application.getAttribute("area")%><br/> servlet引擎以及版本号:<%=application.getServerInfo()%><br/> 所有属性名: <% Enumeration attribute = application.getAttributeNames(); while(attribute.hasMoreElements()){ out.println(attribute.nextElement()); } %> </body> </html> |
输出如下:
城市:JiNing
县区:WeiShan
servlet引擎以及版本号:Apache Tomcat/8.5.46
所有属性名: javax.servlet.context.tempdir area org.apache.catalina.resources city org.apache.tomcat.InstanceManager org.apache.catalina.jsp_classpath javax.websocket.server.ServerContainer org.apache.jasper.compiler.ELInterpreter org.apache.jasper.compiler.TldCache org.apache.tomcat.JarScanner org.apache.jasper.runtime.JspApplicationContextImpl
page对象
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> Object类:<%= page.getClass() %><br/> Object类的hash码:<%= page.hashCode() %><br/> Object类转换成String对象:<%= page.toString() %> </body> </html> |
输出如下:
Object类:class org.apache.jsp.page_jsp
Object类的hash码:351642781
Object类转换成String对象:org.apache.jsp.page_jsp@14f5a49d
pageContext对象
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> 获取session对象并使用:<%= pageContext.getSession().getAttribute("username") %> <% pageContext.forward("index.jsp"); // 跳转页面 %> </body> </html> |
config对象
exception对象
自定义一个异常错误处理页面
<%@ page language="java" contentType="text/html; charset=utf-8" isErrorPage="true"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <%= exception.getMessage() %><br/> <%= exception.toString() %> </body> </html> |
当发生异常时,指定异常处理页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" errorPage="exception.jsp"%> <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <% out.println(100/0); %> </body> </html> |
JavaResource目录下点击创建包package,点击包创建一个class类,目录结构如下:
①Eclipse使用普通方式创建JavaBean
package com; public class User { private String username; private int age; public User() { // TODO Auto-generated constructor stub } @Override public String toString() { return "User [username=" + username + ", age=" + age + ", toString()=" + super.toString() + "]"; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
备注:可以使用ALT+/创建成员方法和变量,Alt+Shift+S快速生成getter和setter
引用javaBean
<%@ page language="java" contentType="text/html; charset=utf-8"%> <%@ page import="com.User" %> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% User user = new User(); user.setUsername("丫丫"); user.setAge(36); %> 用户名:<%=user.getUsername() %><br/> 年龄:<%=user.getAge() %> </body> </html> |
②使用jsp:useBean方式
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <jsp:useBean id="user" class="com.User" scope="page"></jsp:useBean> <% user.setUsername("丫丫"); user.setAge(36); %> 用户名:<%=user.getUsername() %><br/> 年龄:<%=user.getAge() %> </body> </html> |
jsp:setProperty的四种使用方式
表单页面
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <form method="post" action="do_login.jsp"> 用户名:<input type="text" name="username"/> 年龄:<input type="text" name="age"/> <button>提交</button> </form> </body> </html> |
提交页面
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); %> <jsp:useBean id="user" class="com.User" scope="page"></jsp:useBean> <!-- ①匹配所有属性 --> <jsp:setProperty property="*" name="user"/> <!-- ②使用request对象中的一个参数值来指定Bean中的一个属性值 --> <jsp:setProperty property="username" name="user"/> <!-- ③和表单无关 手工赋值 --> <jsp:setProperty property="username" name="user" value="佟丽娅"/> <!-- ④和request参数关联 --> <jsp:setProperty property="username" name="user" param="username"/> 用户名:<%= user.getUsername() %><br/> 年龄:<%= user.getAge() %> </body> </html> |
jsp:getProperty使用
—表单同上—
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); %> <jsp:useBean id="user" class="com.User" scope="page"></jsp:useBean> <jsp:setProperty property="*" name="user"/> 用户名:<jsp:getProperty property="username" name="user"/> 年龄:<jsp:getProperty property="age" name="user"/> </body> </html> |
javaBean的四个作用域范围
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <%@ page import="com.User" %> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <jsp:useBean id="user" class="com.User" scope="application"></jsp:useBean> <!-- 使用jsp:getProperty获取 --> 用户名:<jsp:getProperty property="username" name="user"/> 年龄:<jsp:getProperty property="age" name="user"/> <hr/> <!-- 使用内置对象获取 --> 用户名:<%= ((User)application.getAttribute("user")).getUsername() %> 年龄:<%= ((User)application.getAttribute("user")).getAge() %> </body> </html> |
创建一个登陆处理JavaBean
package com; public class LoginDao { public static boolean doLogin(User u) { if ("admin".equals(u.getUserName()) && "123456".equals(u.getPassword())) { return true; } else { return false; } } } |
jsp中调用javabean重写登陆逻辑
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <jsp:useBean id="userBean" class="com.User" scope="page"></jsp:useBean> <jsp:useBean id="LoginDao" class="com.LoginDao" scope="page"></jsp:useBean> <!DOCTYPE html> <html> <head> <title>登录处理页面</title> </head> <body> <% request.setCharacterEncoding("utf-8"); %> <jsp:setProperty property="*" name="userBean"/> <% if(LoginDao.doLogin(userBean)){ out.println("登录成功"); }else{ out.println("登录失败"); } %> </body> </html> |
http状态管理之cookie
登陆表单 login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% Cookie[] cookies = request.getCookies(); String userName = ""; String password = ""; if(cookies!=null && cookies.length>0){ for(Cookie c:cookies){ if("username".equals(c.getName())){ userName = c.getValue(); } if("password".equals(c.getName())){ password = c.getValue(); } } } %> <form method="post" action="do_login.jsp"> 用户:<input type="text" name="username" value="<%= userName %>"/> 密码:<input type="text" name="password" value="<%= password%>"/> 十天免登录:<input type="checkbox" name="is_remember" /> <button>登录</button> </form> </body> </html> |
do_login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String[] isCheckBox = request.getParameterValues("is_remember"); if (isCheckBox != null && isCheckBox.length > 0) { Cookie userName = new Cookie("username",request.getParameter("username")); Cookie password = new Cookie("password",request.getParameter("password")); userName.setMaxAge(864000); password.setMaxAge(864000); response.addCookie(userName); response.addCookie(password); }else{ Cookie[] cookies = request.getCookies(); for(Cookie c:cookies){ c.setMaxAge(0); } } %> <a href="user_info.jsp">查看用户信息</a> </body> </html> |
user_info.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% String userName = ""; String password = ""; Cookie[] cookies = request.getCookies(); for( Cookie c:cookies){ if("username".equals(c.getName())){ userName =c.getValue(); } if("password".equals(c.getName())){ password = c.getValue(); } } %> 用户名:<%=userName %> 密码:<%=password %> </body> </html> |
include指令
file.jsp
<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.text.DateFormat"%> <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <% Date d = new Date(); DateFormat df = new SimpleDateFormat("yyyy年MM月dd日"); String date = df.format(d); %> <%=date %> </body> </html> |
include.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <%@include file="file.jsp" %> </body> </html> |
<jsp:include page="file.jsp" flush="false"></jsp:include> |
<%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <jsp:forward page="user.jsp"> <jsp:param value="admin@qq.com" name="email"/> </jsp:forward> </body> </html> |