【Head First Servlets and JSP】筆記16:JSP隱式對象(內建對象)

來源:互聯網
上載者:User

標籤:str   obb   action   instance   ref   strong   servlets   parameter   ons   

接筆記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隱式對象(內建對象)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.