JSP:jsp是Java所提供的一种web的动态视图技术
JSP = html + css + js + jsp指令/脚本片段
<%…%>:Java语句 ; 所有的语句 都包含在_jspService方法中
<%=…%>:Java表达式 ;使用out.print进行输出
<%! … %>: 类的成员 可以是成员变量 也可以是成员方法
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %><%--导包--%>
<%--
Created by IntelliJ IDEA.
User: Adminstrator
Date: 2020/2/13
Time: 15:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>这是一个jsp页面</title>
</head>
<body>
<%-- jsp的注释
<%...%>:Java语句;
<%=…%>:Java表达式;
--%>
<h1 style="color: red">这是一个jsp页面</h1>
<%--jsp的脚本片段--%>
<%
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateStr = sdf.format(date);
%>
<%=dateStr%>
<%-- 定义一个变量 是一个成绩 根据成绩来判断 --%>
<%
int score = 89;
if(score< 0 || score >100){
%>
<h3>你所输入的数据非法</h3>
<%
}else{
if(score >= 90){
%>
<h3 style="color:red">你的成绩判定为A</h3>
<%
}else if(score >=80 && score < 90){
%>
<h3 style="color:gold">你的成绩判定为B</h3>
<%
}
}
%>
</body>
</html>
内置对象 就是不需要定义就可以 直接使用的对象
<%--表达式后边不能有分号出现--%>
<%=request.getParameter("username")%>
<%=response instanceof HttpServletResponse %>
<%=pageContext.getRequest().getParameter("username")%>
<%=session.getId()%>
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
<%=application.getInitParameter("app")%>
<%--表达式会最终被翻译为--% out.print()>--%>
<%
out.print(application.getInitParameter("app"));
%>
<%
out.print(config.getInitParameter("username"));
%>
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" language="java" %>
pageContext
request
session
application
同时四大域对象都有以下方法:
setAttribute(String key ,Object value)
getAttribute(String key)
removeAttribute(String key)
域属性的范围:
pageContext< request < session<application
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Home</title>
</head>
<body>
<%
pageContext.setAttribute("pagecontext","pagecontext.....");
request.setAttribute("request","request..........");
session.setAttribute("session","session.........");
application.setAttribute("application","application......");
%>
<%= pageContext.getAttribute("pagecontext")%><br><br>
<%= request.getAttribute("request")%><br><br>
<%= session.getAttribute("session")%><br><br>
<%= application.getAttribute("application")%><br><br>
<%--<%--%>
<%--request.getRequestDispatcher("/attr2.jsp").forward(request,response);--%>
<%--%>--%>
<a href="attr2.jsp">跳转到attr2.jsp</a>
</body>
</html>
attr2.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>attr2的页面</h1>
<%= pageContext.getAttribute("pagecontext")%><br><br>
<%= request.getAttribute("request")%><br><br>
<%= session.getAttribute("session")%><br><br>
<%= application.getAttribute("application")%><br><br>
</body>
</html>
JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分。
JSP指令的基本语法格式:
<%@ 指令 属性名=“值” %>
举例:<%@ page contentType=“text/html;charset=gb2312”%>
注意:属性名部分是大小写敏感的
V:jsp
C:Servlet
M:
显示用户列表 按钮:实现添加
操作
删除/修改
当前一个Servlet只能处理一个请求
技术难点:
多个请求如何使用一个 Servlet ?
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("_method");
switch (method){
case "delete":
delete(req, resp);
break;
case "update":
update(req, resp);
break;
case "add":
add(req, resp);
default:
list(req, resp);
}
}
public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("删除一个用户"+req.getParameter("id"));
list(req, resp);
}
public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("修改一个用户"+req.getParameter("id"));
list(req, resp);
}
public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("新增一个用户");
}
public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<User> users = new ArrayList<>();
User u1 = new User("张三",24,"男","太原");
User u2 = new User("张三",24,"男","太原");
User u3 = new User("张三",24,"男","太原");
User u4 = new User("张三",24,"男","太原");
User u5 = new User("张三",24,"男","太原");
users.add(u1);
users.add(u2);
users.add(u3);
users.add(u4);
users.add(u5);
req.setAttribute("user",users);
req.getRequestDispatcher("/list.jsp").forward(req,resp);
}
}
<%@ page import="java.util.List" %>
<%@ page import="org.lanqiao.mvc.domain.User" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/4/3
Time: 15:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style type="text/css">
table{
border: 1px solid;
}
td{
border: 1px solid;
}
</style>
</head>
<body>
<h1>用户数据列表</h1>
<%
List<User> users = (List<User>) request.getAttribute("user");
%>
<a href="add.jsp">添加</a>
<table>
<thead>
<tr>
<th>用户名</th>
<th>年龄</th>
<th>性别</th>
<th>住址</th>
</tr>
</thead>
<tbody>
<%
for (User user : users){
%>
<tr>
<td><%=user.getName()%></td>
<td><%=user.getAge()%></td>
<td><%=user.getSex()%></td>
<td><%=user.getAddr()%></td>
<td><a href="/user.do?method=delete&id=1">删除</a></td>
<td><a href="/user.do?method=update&id=3">修改</a></td>
</tr>
<%
}
%>
</tbody>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/user.do" method="post">
<input type="hidden" name="_method" value="add">
<input type="submit" value="保存">
</form>
</body>
</html>