Java Fundamentals Chapter 14th (servlet declaration cycle, Servlet provides data to JSP, servlet jump JSP, JSP writes Java code)

Source: Internet
Author: User

One, servlet declaration cycle

The 1.Servlet declaration cycle is generally divided into four steps: Load, instantiate, service, destroy.

2. Instantiation is performed only once throughout the life cycle.

Second, JSP

1.Servlet providing data to the JSP

Request.setattribute (,);

Example: Request.setattribute ("Key", "Hello");

2.Servlet Jump JSP

(1) Same request method: Request.getrequestdispatcher ("/path"). Forward (request, response);

The request jump can only be requested in the same way.

(2) Different request method: Response.sendredirect ("path");

The address bar will change after execution.

Write Java code in 3.jsp

(1) <%=%>

Can only write expressions, variable values

Example: <%= 1 + 1%>

<%= Request.getattribute ("Hello")%>

(2) <%

code block;

%>

Example: <%

String value = (string) request.getattribute ();

%>

4.HttpSession session = Request.getsession ();

Session.setattribute (,);

Can be under the same request, or under different requests, but in the same browser window.

Example: String value = (string) session.getattribute ("");

5.servletContext sc = Session.getservletcontext ();

Sc.setattribute (,);

You can also be under different browsers under the same request and at different requests.

String value = (string) application.getattribute ("");

Java Fundamentals Chapter 14th (servlet declaration cycle, Servlet provides data to JSP, servlet jump JSP, Java code written in JSP)

Related Article

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.