jsp的本质就是一个Servlet,JSP主要负责和用户交互,将最终的界面呈现给用户,HTML+CSS+JS+Java的混合文件
当服务器接收到一个后缀是jsp的请求时,将给请求交给jsp引擎去处理,每一个jsp页面第一次被访问的时候,jsp引擎会将它翻译成一个servlet文件,由web容器调用servlet去响应
下面就是jsp引擎翻译出来的index_jsp.java文件,jsp中的有关页面的元素都会调用write方法写到web容器中,同时jsp比直接使用html文件的好处在于,jsp中还可以写入java,因为最终会被翻译成.java文件
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/9.0.65
* Generated at: 2022-09-14 08:14:35 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = null;
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
final java.lang.String _jspx_method = request.getMethod();
if ("OPTIONS".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
return;
}
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
return;
}
}
final javax.servlet.jsp.PageContext pageContext;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, false, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
out = pageContext.getOut();
_jspx_out = out;
out.write('\n');
out.write('\n');
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "https://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");
out.write("\n");
out.write("\n");
out.write("\n");
out.write(" \n");
out.write(" \n");
out.write(" " );
out.print(request.getServletContext().getServerInfo() );
out.write("\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write("\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" "
);
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${pageContext.servletContext.serverInfo}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" If you're seeing this, you've successfully installed Tomcat. Congratulations!
\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" Recommended Reading:
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("security-howto.html\">Security Considerations How-To
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("manager-howto.html\">Manager Application How-To
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("cluster-howto.html\">Clustering/Session Replication How-To
\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write("
\n");
out.write(" \n");
out.write(" \n");
out.write(" Developer Quick Start
\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write("
\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" Managing Tomcat
\n");
out.write(" For security, access to the manager webapp is restricted.\n"
);
out.write(" Users are defined in:\n");
out.write(" $CATALINA_HOME/conf/tomcat-users.xml
\n");
out.write(" In Tomcat 9.0 access to the manager application is split between\n"
);
out.write(" different users. );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("manager-howto.html\">Read more...\n");
out.write("
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("RELEASE-NOTES.txt\">Release Notes
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("changelog.html\">Changelog
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("migration.html\">Migration Guide
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("security.html\">Security Notices
\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" Documentation
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("\">Tomcat 9.0 Documentation
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("config/\">Tomcat 9.0 Configuration
\n");
out.write(" Tomcat Wiki
\n");
out.write(" Find additional important configuration information in:
\n");
out.write(" $CATALINA_HOME/RUNNING.txt
\n");
out.write(" Developers may be interested in:
\n");
out.write(" \n"
);
out.write(" - Tomcat 9.0 Bug Database
\n");
out.write(" - );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatDocUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("api/index.html\">Tomcat 9.0 JavaDocs
\n");
out.write(" - Tomcat 9.0 Git Repository at GitHub
\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" Getting Help
\n");
out.write(" );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("faq/\">FAQ and );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("lists.html\">Mailing Lists
\n");
out.write(" The following mailing lists are available:
\n");
out.write(" \n"
);
out.write(" - );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("lists.html#tomcat-announce\">tomcat-announce
\n" );
out.write(" Important announcements, releases, security vulnerability notifications. (Low volume).\n");
out.write(" \n");
out.write(" - );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("lists.html#tomcat-users\">tomcat-users
\n" );
out.write(" User support and discussion\n");
out.write(" \n");
out.write(" - );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("lists.html#taglibs-user\">taglibs-user
\n" );
out.write(" User support and discussion for );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("taglibs/\">Apache Taglibs\n");
out.write(" \n");
out.write(" - );
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${tomcatUrl}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write("lists.html#tomcat-dev\">tomcat-dev
\n" );
out.write(" Development mailing list, including commit messages\n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write(" \n");
out.write("
\n");
out.write(" \n");
out.write(" \n");
out.write(" Copyright ©1999-"
);
out.write((java.lang.String) org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate("${year}", java.lang.String.class, (javax.servlet.jsp.PageContext)_jspx_page_context, null));
out.write(" Apache Software Foundation. All Rights Reserved\n");
out.write(" \n");
out.write(" \n");
out.write("\n");
out.write("\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
方式一:jsp脚本,执行java逻辑代码
<%
String s = "hello";
System.out.println(s);
%>
方式二:jsp声明,定义java方法
<%!
public void test() {
System.out.println("hello world");
}
%>
<%
test();
%>
方式三:jsp表达式,把java对象直接输出到html页面
<%=s.getClass()
%>
案例:
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.entity.User" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<%
ArrayList<User> users = new ArrayList<>();
users.add(new User("张三", 18));
users.add(new User("李四", 28));
users.add(new User("王五", 17));
users.add(new User("赵六", 43));
%>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
<%
for (int i = 0; i < users.size(); i++) {
%>
<tr>
<td>
<%=users.get(i).getName()%>
</td>
<td>
<%=users.get(i).getAge()%>
</td>
</tr>
<%
}
%>
</table>
</body>
</html>
request:表示一次请求,HttpServletRequest
response:表示一次响应,HttpServletResponse
pageContext:页面上下文,获取页面信息,PageContext
session:表示一次会话,保存用户信息,HttpSession
application:表示当前web应用,全局对象,保存所有用户共享信息,ServletContext
config:当前JSP对应的Servlet的ServletConfig对象,获取当前Servlet信息
out:向浏览器输出数据,JSPWriter
page:当前JSP对应的Servlet对象,Servlet
exception:表示JSP页面发生的异常,Exception
常用的是:request、response、session、application、pageContext
request 常用方法:
200:正常
404:找不到资源
400:请求类型不匹配
500:java程序抛出的异常
response常用方法
1、 sendRedirect(String path)重定向
转发getRequestDispatcher和重定向sendRedirect的区别:
用户登录,如果用户名密码正确,则跳转首页(转发),并展示用户名,否则重新回到登录页面(重定向)
login.jsp
<%--
Created by IntelliJ IDEA.
User: bijing
Date: 2022/9/16
Time: 20:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<div>
<form action="/check.jsp" method="post">
<table>
<tr>
<td> 用户名:</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="psd">
</td>
</tr>
</table>
<input type="submit" value="登录">
</form>
</div>
</body>
</html>
check.jsp
<%--
Created by IntelliJ IDEA.
User: bijing
Date: 2022/9/16
Time: 20:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>check</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
String psd = request.getParameter("psd");
if (userName != null && psd != null) {
System.out.println(userName + "_" + psd);
if (userName.equals("张三") && psd.equals("12345")) {
request.setAttribute("userName", userName);
request.setAttribute("psd", psd);
request.getRequestDispatcher("success.jsp").forward(request, response);
} else {
response.sendRedirect("login.jsp");
}
}
%>
</body>
</html>
success.jsp
<%--
Created by IntelliJ IDEA.
User: bijing
Date: 2022/9/16
Time: 20:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Success</title>
</head>
<body>
<%
String userName = (String) request.getAttribute("userName");
String psd = (String) request.getParameter("psd");
%>
<h1>用户名:<%=userName%>
</h1>
<h1>密码:<%=psd%>
</h1>
</body>
</html>