Overview of Session Technology and JSP

Source: Internet
Author: User

First, session technology

1, Concept: Share data in a session, in the Web middle, the browser and the server communication. Contains multiple requests, and multiple responses.

You can share data in multiple requests for a single session.

2. Client session Technology: Cookies

Save data on client server the pressure is small but the data is unsafe; Cookies are stored in the browser's memory by default

To send a cookie:

Cookie C = new Cookie ("MSG", "hehe");

Response.addcookie (c);

Accept cookies:

Cookie[] cs = request.getcookies ();

if (cs! = null) {

for (Cookie C:cs) {

String name = C.getname ();

if ("msg". Equals (name)) {

String value = C.getvalue ();

}

}

}

Set the lifetime of a cookie so that it can be persisted

Setmaxage (int second):

Positive number: present on hard disk

Negative number: Default-1 exists in browser memory

0: Delete Cookies

Use cookies to deliver Chinese

Cookie UC = new Cookie ("username", Urlencoder.encode (username, "utf-8"));//Encoding

Username = Urldecoder.decode (C.getvalue (), "utf-8");//decoding

3. Service-side session technology: Sessions

Storing data on server-side data security servers stressful; session relies on cookie presence

1. Get session

HttpSession session = Request.getsession ();

2. Storing data

Session.setattribute ("msg", "hehe");

3. Get Data

Object msg = Session.getattribute ("msg");

Destroy session

Session.invalidate ()

Destroy session expires in 30 minutes, automatically destroyed.

<session-config>

<session-timeout>30</session-timeout>

</session-config>

Second, Jsp--java servlet page

1, Concept: is a can write HTML language can also write Java code page, the nature of JSP is a servlet

Grammar:

<%! int num=1000; %> generated Java code in the member location

<% System.out.println ("Hello"); %> generated code in the service () method

<%= "Hello World"%>out.print ("Hello word");

<%--JSP Special Comment--%> Comment

2. Three major instructions: Configure the properties of the JSP page or import some additional content

<%@ directive Name Property name = "attribute value"%>

1) Page directive

Language:java

Import: Importing Packages

Pageencoding:utf-8

AutoFlush: Auto Refresh

Buffer: size of buffers

ErrorPage: Specify error page

Iserrorpage: Whether it is an error page can use built-in objects exception

2) taglib Instruction

Import additional content

<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>

Prefix: Specifies the label prefix, this thing can be arbitrarily named

URI: Specifies the URI of the third-party tag library (unique identifier)

3) include directive

Static contains <% @include file= "page"%> contains, only one. class file exists

3, six Action tags

1) <jsp:usebean id= "Object name" class= "package name. Class name" scope= "Scope (request/page/application/session)"/>

<jsp:setproperty name= "JavaBean object" property= "JavaBean object property name" param= "username"/>

<jsp:getproperty name= "JavaBean object" property= "JavaBean object property name"/>

2) <jsp:include page= "included.jsp" flush= "true"/> Dynamic contains multiple. Class bytecode files

3)<jsp:forward page= "/index.jsp" >

<jsp:param name= "msg" value= "hehe"/>

</jsp:forward>

4, nine built-in objects

1) out output Object to the client output data, byte stream. such as Out.print ("Hello");

2) Request Object receives HTTP request from client

3) Response Response Object A:addcookie (cookie cookie); b:sendredirect ("/wel.jsp")

4) Session Object getattribute (String name)

5) Application Application Object It is an instance of the ServletContext class

6) Page Object represents the JSP entity itself, that is, the current page is valid. Equivalent to this in Java

7) Exception exception objects represent runtime exceptions

8) PageContext Page Context object PageContext object provides access to all objects and namespaces within a JSP page

9) Config configuration object jsp corresponding servlet configuration, you can get the initial parameters in Web. xml


Overview of Session Technology and JSP

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.