如何优化网站关键词排名平台做推广的技巧
文章目录
- Day 5
- JSP指令
- JSP标签
- JSTL标签库
- EL表达式
- 过滤器
Day 5
2019年7月17日。
这是我学习JavaWeb的第五天。
这一天,我学到了以下的知识。
JSP指令
语法
<%@ 指定名 属性名=属性值:[属性名=属性]... @%>
在JSP中,只有三种指令:page、include、tarlib
-
page指令
- autoFlush:自动刷新(true,false:默认)
- contentType:页面文本类型"text/html"
- errorPage:如果存在错误页面,就跳转至指定的页面【不推荐使用,但需要掌握】
可以在web.xml配置<.error-page>标签接下来的属性:
- error-code:错误码
- location:错误页面
- exception-type:异常类型 - language:JSP使用的语言,默认是Java
- pageEncoding:页面编码
- import:因为jsp本质上是一个servlet,所以需要导入相关jar包才能使用
-
include指令(需要掌握)
导入其他页面包含到本页,网站中一般有一些公用的位置,我们可以提取出来,比如网站的头部和尾部
file属性:【要导入页面,一般都是不完整的网页,只包含部分】 -
taglib指令
标签库,在jsp中有大量的java代码,所以开发中可以使用一些线程的标签库,就相当于使用一些替代java代码的标签语言
例如:
out.print() —> <c:out> :这个c就是别人定义好的标签库,像这样的库有非常多,甚至可以自己定义
page-errorPage命令测试
在 web.xml中,指定一个页面作为404错误时的页面,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><error-page><error-code>404</error-code><location>/common/header.jsp</location></error-page><error-page><error-code>500</error-code><location>/common/footer.jsp</location></error-page>
</web-app>
include命令测试
在项目目录中新建一个header.jsp和footer.jsp,在这两个页面中分别编写代码,然后再在index.jsp中使用include指令将这两个页面合并起来,代码如下:
- header.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<h1>我是header页面</h1>
</body>
</html>
- footer.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
</head>
<body>
<h1>我是footer页面</h1>
</body>
</html>
- index.jsp
<html><head></head><body><%--使用include指定加载网页其他部分--%><%@include file="common/header.jsp"%><h1>我是index页面</h1><%@include file="common/footer.jsp"%></body>
</html>
JSP标签
作用
避免在JSP中有大量的Java代码,从而不好维护
语法
<jsp:xxx>
常用JSP标签
- jsp:include
page属性:用于表示要包含的页面地址 - jsp:forward
本质:就是request的请求转发,可以携带参数
page属性:表示要转发到的页面,不会改变url - jsp:param
作用:一般配合jsp:forwad使用 - jsp:useBean
作用:用于引入一个自定义的对象 - jsp:setProperty
作用:用于设置自定义对象的属性值,常与jsp:setProperty标签配合,默认使用的是实体类中的set方法 - jsp:getProperty
作用:用于获取自定义对象的属性值,常与jsp:getProperty标签配合,默认使用的是实体类中的get方法
jsp:forward和jsp:param标签测试
项目中有两个jsp页面:index.jsp和tag.jsp,在tag.jsp中设置相应的参数值,从而可以在index.jsp接收到,代码如下:
- tag.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><%--<jsp:include page="tag.jsp"/>
<h1>我是tag页面</h1>
<jsp:include page="common/header.jsp"/>--%><jsp:forward page="index.jsp"><jsp:param name="username" value="abc"/><jsp:param name="age" value="18"/>
</jsp:forward>
</body>
</html>
- index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head></head><body><%--使用include指定加载网页其他部分--%><%@include file="common/header.jsp"%><h1>我是index页面</h1><%@include file="common/footer.jsp"%>名字:<%=request.getParameter("username")%>年龄:<%=request.getParameter("age")%></body>
</html>
jsp:useBean标签测试
项目中有一个实体类:Student类,通过pojo.jsp获取该类的对象,并且设置和显示对象中的属性值,代码如下:
- Student.java
public class Student {// 面向对象编程三大特性:封装、继承、多态// 封装:属性私有,不让用户随意操作,于是我们设置操作属性的公开方法,我们会在公开的方法中定义安全性代码private String name;private int id;private int age;private boolean isGraduate; // 是否毕业public Student() {}public Student(String name, int id, int age, boolean isGraduate) {this.name = name;this.id = id;this.age = age;this.isGraduate = isGraduate;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isGraduate() {return isGraduate;}public void setGraduate(boolean graduate) {isGraduate = graduate;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", id=" + id +", age=" + age +", isGraduate=" + isGraduate +'}';}
}
- pojo.jsp
<%@ page import="pojo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><%Student student = new Student();
%><%--
jsp:useBean标签创建一个类的实例化对象;java代码:Student student = new Student();属性:id:实例化对象的名称,对象名class:要实例化的类scope:对象的作用域,默认是page,千万不要放到application中;
--%>
<jsp:useBean id="student" class="pojo.Student" scope="page"/><%--设置对象中的属性值--%>
<%student.setName("aaa");student.setAge(18);student.setId(001);student.setGraduate(true);
%><%--显示对象中的属性值·方法一--%>
<%=student.getName()%>
<%=student.getAge()%>
<%=student.getId()%>
<%=student.isGraduate()%><hr><%--显示对象中的属性值·方法二--%>
${student.name}
${student.age}
${student.id}
${student.graduate}</body>
</html>
jsp:setProperty和jsp:getProperty标签测试
项目中一个实体类:Student类,通过pojo.jsp获取该类的对象,并且设置和显示对象中的属性值,代码如下:
- Student.java
public class Student {// 面向对象编程三大特性:封装、继承、多态// 封装:属性私有,不让用户随意操作,于是我们设置操作属性的公开方法,我们会在公开的方法中定义安全性代码private String name;private int id;private int age;private boolean isGraduate; // 是否毕业public Student() {}public Student(String name, int id, int age, boolean isGraduate) {this.name = name;this.id = id;this.age = age;this.isGraduate = isGraduate;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public boolean isGraduate() {return isGraduate;}public void setGraduate(boolean graduate) {isGraduate = graduate;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", id=" + id +", age=" + age +", isGraduate=" + isGraduate +'}';}
}
- pojo.jsp
<%@ page import="pojo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body><%Student student = new Student();
%><%--jsp标签--%>
<%--setProperty默认使用的是实体类中的set方法--%>
<jsp:setProperty name="student" property="name" value="aaa"/>
<jsp:setProperty name="student" property="age" value="18"/>
<jsp:setProperty name="student" property="id" value="001"/>
<jsp:setProperty name="student" property="graduate" value="true"/><%--setProperty默认使用的是实体类中的get方法--%>
<jsp:getProperty name="student" property="age"/>
<jsp:getProperty name="student" property="name"/>
<jsp:getProperty name="student" property="id"/></body>
</html>
JSTL标签库
作用
和jsp标签一样,为了解决jsp中嵌入java代码看着不舒服
注意点
- 项目要导入对应的jstl标签库的jar包
- Tomcat中也需要放置对应的jar包
- 代码中,有些特殊符号需要使用转义字符
<%@ page import="com.kuang.pojo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %><%--
使用jstl或者其他标签库需要先导入,需要下载对应的jar包-jstl-1.2;
prefix:需要使用的标签库名
tagdir:标签库的本地目录
uri : 对应库的网络地址
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html>
<head><title>Title</title>
</head>
<body><%--
<c:out 输出
<c:out value="西部开源"/>
--%>
<c:out value="西部开源"/><%--
<c:iftest: 需要判断的条件 ,等价于 if(exp)中的expif(){
}
--%><%request.getSession().setAttribute("name","qinjiang");Student student = new Student();student.setName("qinjiang");
%><%=student.getName().equals("qinjiang")%>
${sessionScope.name=='qinjiang'}<c:if test="<%=student.getName().equals(\"qinjiang\")%>"><c:out value="admin"/>
</c:if></body>
</html>
EL表达式
作用
简化jsp的代码难度
${表达式}
${page、session、request、Application的变量}
过滤器
主要用它来实现过滤乱码问题!
过滤器一般就用来过滤一些违规词语,解决乱码问题;主要还是为了解耦,职责统一
实现步骤
- 写一个类实现Filter接口,必定会重写三个方法
- 初始化
- doFilter(req,resp,doFilterChain)
- doFilterChain(req,resp)
- 注销
- 配置web.xml
<filter><filter-name>myFilter</filter-name><filter-class>com.kuang.filter.MyFilter</filter-class>
</filter><!--这里url代表需要过滤的请求路径
/* 所有的请求都会被过滤
/regist.do 只有这个请求会被过滤
过滤的注销和初始化时随着Tomcat容器一起启动或者关闭
--><filter-mapping><filter-name>myFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>
测试代码
import javax.servlet.*;
import java.io.IOException;//filter过滤实现
//1.实现Filter接口 : javax.servlet.Filter;
//2.配置web.xml
public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {System.out.println("MyFilter初始化了");}@Overridepublic void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {//主要过滤方法编写在doFilter中req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");if (req.getParameter("username")!=null){System.out.println(req.getParameter("username"));if (req.getParameter("username").equals("aaa")){resp.getWriter().println("?????????????????????????????");}}System.out.println("执行方法filterChain.doFilter(req,resp);前===========");filterChain.doFilter(req,resp); //这行代码代表过滤器放行System.out.println("执行方法filterChain.doFilter(req,resp);后===========");}@Overridepublic void destroy() {System.out.println("MyFilter注销了");}
}