jsp组成
- html + java + jsp标签
- jsp中无须创建即可使用的对象(9个)
- java脚本格式
- <%...%> java代码片段
- <%=...%> java表达式
- <%!...%> 声明
简单的例子
- 一个数据提交的表单
<form action="./NumberServlet" method="POST">
<input type="text" name="num1"/>
<br/>
<input type="text" name="num2">
<br/>
<input type="submit" name="submit">
</form>
- NumberServlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String s1 = request.getParameter("num1");
String s2 = request.getParameter("num2");
request.setAttribute("result", s1+s2);
request.getRequestDispatcher("./jsps/result.jsp").forward(request, response);
}
- - -
- 结果显示的jsp
<body> result: <%=request.getAttribute("result") %> </body>
jsp原理
- 第一次访问jsp的时候回讲jsp编译成java --》 class
- jsp其实是一个Servlet 。tomcat work下面
- jsp代码和编译后的java代码
<body>
This is my JSP page. <br>
<a href="./demo">hello</a>
<%
int a = 100;
out.print(a);
a++;
%>
<%
out.print(a);
a++;
%>
<%!
int a = 90;
%>
<%
out.print(this.a++);
%>
</body>
- - -
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
int a = 90;
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.List<java.lang.String> _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.List<java.lang.String> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
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 {
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
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, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write('\r');
out.write('\n');
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" ");
out.write("\r\n");
out.write(" \r\n");
out.write(" <title>My JSP 'index.jsp' starting page</title>\r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write(" This is my JSP page. <br>\r\n");
out.write(" <a href=\"./demo\">hello</a>\r\n");
out.write(" ");
int a = 100;
out.print(a);
a++;
out.write("\r\n");
out.write(" ");
out.print(a);
a++;
out.write("\r\n");
out.write(" ");
out.write("\r\n");
out.write(" ");
out.print(this.a++);
out.write("\r\n");
out.write(" </body>\r\n");
out.write("</html>\r\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
JSP三大指令
<% @ 指令名 attr1="" attr2="" %>
- page
- pageEncoding 指定当前jsp页面的编码(只要pageEncoding的编码和文件的编码一致就不会由乱码,jsp-->class的时候会去读取这个属性)
- contentType 添加一个响应头
- 这两个只要一个就可以了
``` - import
<%@ page import="class1,class2,class3"%> - errorPage isErrorPage
- include 静态包含
<%@ include file="*.jsp"%>
jsp编译成java文件时完成,他们共同生成一个java文件 - taglib 导入标签库
- prefix 前缀
- uri 标签库的位置
- <%@taglib prefix="s" uri="/struts-tags"%>
JSP动作标签
- <jsp:forward>
- <jsp:include>
- <jsp:param>
Cookie
由服务器创建保存到客户端浏览器的一个键值对,服务器保存cookie的相应头:Set-Cookie: key=value Set-Cookie: key1=value1 当浏览器请求服务器时,会把该服务器保存的Cookie随请求发送给服务器: Cookie: key=value; key1=value1
一个Cookie最大4KB
一个服务器最多向一个浏览器保存20个Cookie
一个浏览器最多保存300个Cookie
-
Cookie的maxAge cookie.setMaxAge(int)
- maxAge > 0 浏览器会保存到硬盘上 单位 S
- maxAge < 0 只会保存到内存中 默认
- maxAge = 0 需要删除该Cookie
-
Cookie的path
javaWeb Cookie
response.addCookie();
request.getCookies();
JSP中使用Cookie
//保存cookie
Cookie nameCookie = new Cookie("name","dapi");
Cookie numberCookie = new Cookie("number","12101010320");
response.addCookie(nameCookie);
response.addCookie(numberCookie);
//获取cookie
Cookie[] cookies = request.getCookies();
if(cookies != null){
for(int i=0;i < cookies.length;i++){
out.print(cookies[i].getName()+" "+cookies[i].getValue()+"<br/>");
}
}
HttpSession
- 由javaWeb提供用来会话(一个用户对服务器的连续请求,多次请求没有关闭浏览器)跟踪的类
- session是服务器端对象保存在服务器端
- HttpSession是Servlet三大域之一(request,session,application)
- 底层依赖Cookie或者URL重写
//servlet
request.getSession();
//jsp 可以直接使用 session
session.setAttribute("name", "xx");
session.getAttribute("name");
session.removeAttribute("name");
request.getSession() 首先是会获取Cookie中的sessionId,如果id不存在,创建session保存sessionid,如果存在则通过id找session,如果没找到则创建session并将id保存到cookie中。
sessionId保存的时候maxAge为-1
web.xml中可以配置session最大不活动时间
<session-config>
<session-timeout>30</session-timeout>
</session-config>
Url重写
response.encodeURL(String url);//将cookie --> params
Nothing is certain in this life. The only thing i know for sure is that. I love you and my life. That is the only thing i know. have a good day