首页 > 代码库 > 【Head First Servlets and JSP】笔记16:JSP隐式对象(内置对象)

【Head First Servlets and JSP】笔记16:JSP隐式对象(内置对象)

接笔记15.

1、不管是JSP中的<%%>还是<%\=%>最终都将处于servlet的方法体中,那么有没有一种元素可以声明成类的成员呢?

——答案是有,而且非常非常简单,这个元素就是<%!%>,我们用它来解决笔记15中的问题,只需要在原基础上加一个感叹号就可以了。

<%@ page import="Sample.Counter" %><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Basic Counter</title></head><body><%!    int count = 0;%><p>The page count is: </p><%= ++count %></body></html>

 

2、我们还可以在感叹号里添加类的成员方法。

技术分享

 

JSP隐式对象(内置对象)

1、out并不是唯一的JSP内置对象!

什么是JSP内置对象呢?本质上来说,所有的JSP内置对象最终都将映射为Servlet/JSP API中的某个东西。

例如,内置对象request实质上就是service的参数HttpServletRequest的一个映射。

技术分享

 

2、课本练习

技术分享

index.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title></title></head><body><form action="HobbyPage.do" method="post">    <p>choose a hobby:</p>    <select name="hobby" id="" size="1">        <option value="看书">看书</option>        <option value="打王者荣耀">打王者荣耀</option>        <option value="写代码">写代码</option>        <option value="水群">水群</option>    </select>    <br />    <input type="submit"></form></body></html>

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <servlet>        <servlet-name>hi</servlet-name>        <servlet-class>Sample.AddFriends</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>hi</servlet-name>        <url-pattern>/HobbyPage.do</url-pattern>    </servlet-mapping></web-app>

AddFriends.java

package Sample;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;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;public class AddFriends extends HttpServlet {    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        req.setCharacterEncoding("utf-8");        List names = new ArrayList();        names.add("Fred");        names.add("Pradeep");        names.add("Philippe");        req.setAttribute("names", names);        RequestDispatcher view = req.getRequestDispatcher("result.jsp");        view.forward(req, resp);    }}

result.jsp

<%@ page import="java.util.List" %><%@ page import="java.util.Iterator" %><%--  Created by IntelliJ IDEA.  User: xkfx  Date: 2017/6/14  Time: 17:27  To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>Title</title></head><body>    <%        // response.setCharacterEncoding("utf-8");    %>    <p>hello i‘m result</p>    <p>The friends who share your hobby <%=request.getParameter("hobby")%>  is :</p>    <%        List names = (List) request.getAttribute("names");        Iterator it = names.iterator();        while(it.hasNext()) {            out.println(it.next() + "<br />");        }    %></body></html>

 写这道题碰到的两个极端坑爹的地方:

  1. html中,submit的参数是value而不是文本!更详细可以参考这里:Java Web开发之Servlet获取表单值

  2. 要想最终得到的html显示中文,既不是在AddFriends.java中 resp.setCharacterEncoding("utf-8");也不是在result.jsp中  response.setCharacterEncoding("utf-8"); 而是在AddFriends.java中 req.setCharacterEncoding("utf-8");

【Head First Servlets and JSP】笔记16:JSP隐式对象(内置对象)