一、什么是JSP
JSP(Java Server Pages)其根本是一个特殊的Servlet。
JSP文件是在html中插入Java程序段和JSP标记的文件
二、JSP原理
——当jsp页面第一次被访问时,服务器会把jsp编译成java文件(这个java其实是一个servlet类)——然后再把java编译成.class
——然后创建该类对象
——最后调用它的service()方法
——第二次请求同一jsp时,直接调用service()方法。
三、JSP的作用
在原有html的基础上添加java脚本,构成jsp页面。结合了html与servlet的优点,将动态资源和静态资源整合在一起
在Web项目中通常与Servlet配合开发
——JSP:
作为请求发起页面,例如显示表单、超链接。
作为请求结束页面,例如显示数据。
——Servlet:
作为请求中处理数据的环节。
简单实现:
MyJsp.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>加法计算器</h1>
<h2>请输入</h2><br>
<form action="/JSP/AServlet" method="post">
加数1:<input type="text" name="num1"/>
加数2:<input type="text" name="num2"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
AServlet:
package cn.JspTest.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取参数
String s1=request.getParameter("num1");
String s2=request.getParameter("num2");
int num1=Integer.parseInt(s1);
int num2=Integer.parseInt(s2);
int sum=num1+num2;
//保存结果到request域中
request.setAttribute("sum", sum);
//转换到Result.jsp
request.getRequestDispatcher("/JspServlet/Result.jsp").forward(request, response);
}
}
Result.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>结果是</h3>
<%
Integer sum = (Integer)request.getAttribute("sum");
%>
<%=sum %>
</body>
</html>
四、JSP的基本语法
1.html部分2.Java代码部分
——<%...%>:java代码片段
——<%=...%>:java表达式,用于输出response.getWriter().print( ... );这里能放什么,它就可以放什么!
——<%!...%>:只能声明,用来创建类的成员变量和成员方法
3.jap标签部分
4.jsp注释
<%-- jsp注释 --%>:不会再客户显示
<--html注释-->:不会显示在页面,但查看源码能看到注释