"Head first Servlets and JSP" Note 16:jsp implicit objects (built-in objects)

Source: Internet
Author: User

Pick up Note 15.

1. Whether <%%> in JSP or <%\=%> will eventually be in the servlet's method body, is there an element that can be declared as a member of a class?

The answer is yes, and very very simple, this element is <%!%>, and we use it to solve the problem in note 15, just add an exclamation mark on the original basis.

Import= "sample.counter"%><%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>< html><%!     int count = 0; %><p>the page count is: </p><%= ++count%></body>

2, we can also add the class member method in the exclamation mark.

JSP implicit object (built-in object)

1, out is not the only JSP built-in Object!

What is a JSP built-in object? Essentially, all of the JSP built-in objects will eventually be mapped to something in the Servlet/jsp API.

For example, the built-in object request is essentially a mapping of the service's parameter httpservletrequest.

2. Textbook Practice

Index.html

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title></title></Head><Body><formAction= "Hobbypage.do"Method= "POST">    <P>Choose a hobby:</P>    <Selectname= "Hobby"ID=""size= "1">        <optionvalue= "reading">Reading</option>        <optionvalue= "Glory of the King">Play The Glory of Kings</option>        <optionvalue= "Write code">Write code</option>        <optionvalue= "Water Group">Water Group</option>    </Select>    <BR/>    <inputtype= "Submit"></form></Body></HTML>

Xml

<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "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

 PackageSample;ImportJavax.servlet.RequestDispatcher;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List; Public classAddfriendsextendsHttpServlet {@Overrideprotected voidDoPost (httpservletrequest req, HttpServletResponse resp)throwsservletexception, IOException {req.setcharacterencoding ("Utf-8"); List names=NewArrayList (); 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

<%@ pageImport= "Java.util.List"%><%@ pageImport= "Java.util.Iterator"%><%--Created by IntelliJ. USER:XKFX Date:2017/6/14Time :17:27 to change ThisTemplate Use File | Settings |File Templates.--%><%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>//response.setcharacterencoding ("Utf-8");%> <p>hello i ' m result</p> <p>the friends who share your hobby <%=request.getparameter ("Hobb Y ")%> is:</p> <%List Names= (List) request.getattribute ("names"); Iterator it=Names.iterator ();  while(It.hasnext ()) {out.println (It.next () )+ "<br/>"); }    %></body>

Write about the two extreme pits that the problem encounters:

    1. In HTML, the argument to submit is value, not text! More details can be found here: Java Web development servlet Get form values

    2. To eventually get the HTML display in Chinese, neither in Addfriends.java resp.setcharacterencoding ("Utf-8"); Nor in the result.jsp response.setcharacterencoding ("Utf-8"); But in the Addfriends.java req.setcharacterencoding ("Utf-8");

"Head first Servlets and JSP" Note 16:jsp implicit objects (built-in objects)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.