jsp笔记
jsp
- jsp声明
- jsp注解
- jsp表达式
- jsp脚本
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/2/23
Time: 上午 8:25
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%--jsp的声明--%>
<%! public int count;
public String info() {
return "hello java";
}
%>
</head>
<body>
欢迎学习java web,现在的时间是:<%out.println(new java.util.Date());%>
<br>
<% for (int i = 0; i < 5; i++) {
out.println("<font size='" + i + "'>java</font><br>");
}%>
<br>
<h4>jsp的声明</h4>
<%
//每次刷新都会增加
out.println(count++);
%>
<br>
<%
//输出info()方法的返回值
out.println(info());
%>
<h4>输出jsp表达式,注意表达式的后面不能有分号</h4>
<%=count++%>
<%=info()%>
<h4>java脚本</h4>
<table border="1" width="300px">
<%
for (int i = 0; i < 5; i++) {
%>
<tr>
<td>循环值:</td>
<td><%=i%>
</td>
</tr>
<%
}
%>
</table>
<h4>脚本中使用jdbc</h4>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://192.168.225.128:33306/new_info","root","123456");
Statement sm = conn.createStatement();
ResultSet rs = sm.executeQuery("select * from t_info");
%>
<table border="1" width="300px">
<tr>
<td>id</td>
<td>info</td>
</tr>
<%
while (rs.next()){
%>
<tr>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
jsp的3个编译指令
- page 只针对当前页面的指令
- include 用于指定包含另一个页面
- taglib 用于定义和访问自定义标签
<%@ 编译指令名 属性名="属性值"%>
page
- import
<%--类似于导入包--%>
<%@page import="java.sql.*" %> - 错误信息
<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> - info
<%@page info="this is a jsp" %>
<%--输出info信息--%>
<%=getServletInfo()%>
include
<%--静态文件的导入--%>
<%@include file="error.jsp"%>
<%--动态文件建议使用的方式--%>
<jsp:include page="a.jsp"/>
jsp的7个动作指令:
- jsp:forward 页面转向,将请求的处理转发到下一个页面
- jsp:param 传递参数
- jsp.include 动态引入一个jsp
- jsp.plugin 用于下载一个javabean或applet到客户端执行
- jsp.useBean 创建一个javabean的实例
- jsp.setProperty 设置javabean实例的属性值
- jsp.getProperty 输出。。。。
jsp:forward与jsp:param
<jsp:forward page="res.jsp">
<%--传递参数--%>
<jsp:param name="age" value="29"/>
</jsp:forward>
jsp.include
<jsp:include page="res.jsp">
<%--传递参数--%>
<jsp:param name="age" value="29"/>
</jsp:include>
<%--使用request内置对象获取age参数的值--%>
<%=request.getParameter("user")%>
其他
<jsp:useBean id="p1" class="fly.pojo.Person" scope="page"/>
<jsp:setProperty name="p1" property="name" value="zhangsan"/>
<jsp:setProperty name="p1" property="age" value="20"/>
<jsp:getProperty name="p1" property="name"/>
<jsp:getProperty name="p1" property="age"/>
<%
Person p = new Person();
pageContext.setAttribute("p2",p);
p.setName("lisi");
p.setAge(22);
%>
<%=p.getName()%>
<%=p.getAge()%>
<%=pageContext.getAttribute("p2")%>
jsp脚本中的9个内置对象
只能在jsp脚本,jsp输出表达式中使用
- application 对整个web有效
- config
- exception
- out
- page 当前页面有效
- pageContext
- request 本次请求有效
- response
- session 一次会话有效
application
<body>
<%! int i;%>
<%
application.setAttribute("count",String.valueOf(++i));
%>
<%=application.getAttribute("count")%>
</body>
java代码获取
public class GetApplication extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
PrintWriter writer = resp.getWriter();
writer.println("<html><head><title>");
writer.println("</title></head><body>");
ServletContext sc = getServletConfig().getServletContext();
writer.println("application中当前的count值为:");
writer.println(sc.getAttribute("count"));
writer.println("</body></html>");
}
}
web.xml部分设置:
<servlet>
<servlet-name>get-app</servlet-name>
<servlet-class>fly.servlet.GetApplication</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>get-app</servlet-name>
<url-pattern>/get-app</url-pattern>
</servlet-mapping>
pom文件:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
读取web.xml 配置参数:
web.xml:
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
jsp页面:
<%--从配置中获取参数--%>
<%
String driver = application.getInitParameter("driver");
out.println(driver);
%>
config对象:
web.xml:
<servlet>
<servlet-name>servletName</servlet-name>
<jsp-file>/e.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>servletName</servlet-name>
<url-pattern>/e.jsp</url-pattern>
</servlet-mapping>
e.jsp:
<%--获取servlet的名字,默认为jsp--%>
<%=config.getServletName()%>
exception:
<%=exception.getClass()%>
<%=exception.getMessage()%>
pom文件关于tomcat的配置说明
<build>
<plugins>
<plugin>
<!--启动命令-->
<!--clean tomcat7:run-->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!--<port>9001</port>-->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
jdk 1.7设置:
<profiles>
<profile>
<id>jdk1.7</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
filter过滤,访问log案例:
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* 过滤器,过滤所有
*/
@WebFilter(filterName = "log", urlPatterns = {"/*"})
public class MyFilter implements Filter {
private FilterConfig config;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.config = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("开始过滤...");
long begin = System.currentTimeMillis();
HttpServletRequest request1 = (HttpServletRequest) request;
System.out.println("用户请求:" + request1.getServletPath());
chain.doFilter(request, response);
long end = System.currentTimeMillis();
System.out.println("过滤结束...");
System.out.println("前往:" + request1.getRequestURI());
System.out.println("消耗时间:" + (end - begin));
}
@Override
public void destroy() {
this.config = null;
}
}