Quick self-level language programming java dry notes -JSP for data transfer and storage

JSP is a dynamic web technology, Java Server Pages, Java + HTML, HTML is a page content is displayed, Java is as dynamic logic.

It is essentially a template technique, and then through the Tomcat Jasper components that can be translated as .java file, then compile and run.

The composition of each piece inside it will be translated to a corresponding position .java file.

Script:<% %> ->_jspService()方法内

Expression:<%= %> ->_jspService()方法内 的 out.print()

Disclaimer:<%! %> ->.java文件内的成员位置

JSP's built-in objects

1. master the use of the request and response

1.1 JSP nine built-in objects [interview questions]

OUT : output content to a page

out.print(); // 它可以输出任何数据类型  将对应的数据类型转换为字符串 它用于向页面输出我们Java一些变量信息
out.write(); // 它只能输出字符(字符串)内容 它用于向页面输出HTML内容

Request : it represents is the client's request

the Response : It represents the response of the server

the session : Technical Session

the Application : Application Context

page: It refers to the current page, of course, if it is after the translation, it refers to the current object

pageContext : It is used to obtain other scopes content and the context it is also the same page

config: It can obtain the initial configuration parameters (web.xml)

exception: it is possible to obtain an exception to the information that appears on a page and generally <% @ page errorPage = "error.jsp"%> combination

1.2 request objects

Simulation of a registration:

<form action="doRegister.jsp" method="POST">
    <p>
        帐号:<input type="text" name="username" value="admin"/>
    </p>
    <p>
        密码:<input type="password" name="password" value="123456"/>
    </p>
    <p>
        爱好:
        <input type="checkbox" name="hobby" value="1"/>编程
        <input type="checkbox" name="hobby" value="2"/>学习
        <input type="checkbox" name="hobby" value="3"/>写作业
    </p>
    <p>
        <input type="submit" value="注册" />
    </p>
</form>
<%
	// 接收客户端的请求参数
	// 根据name属性获取对应value的值
	// 获取单个值的参数
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	
	// 获取多个值的参数
	String[] hobbies = request.getParameterValues("hobby");
%>

<!-- out.print() -->
<%="接收到的用户名为:"+username  %> <br/>
<%="接收到的密码为:"+password  %> <br/>
<%
	out.print("接收到的爱好有:");
	for(String hobby : hobbies){
		out.print(hobby+" ");
	}
%>
  • String getParamepter (String name); name value value according to the acquiring or null if not obtain
  • String [] getParamepterValues ​​(String name); obtaining a plurality of values ​​according to the value of the parameter name
  • Map <String, String []> getParamepterMap (); injection into all request parameters set Map
  • String getMethod (); acquisition request mode
  • String getRemoteAddr (); obtains an IP address of the client
  • String getContextPath (); to get the root path of the project
  • String getHeader (String name); acquisition request header
  • InputStream getInputStream (); acquiring parameters input stream of bytes
  • void setAttribute (String name, Object val); but this information is only valid in the same request to store information request scope
  • Object getAttribute (String name); extract information from the request scope or null if there is no
  • RequestDispatcher getRequestDispatcher (String path); obtaining the object forwards the request
    • forward (request, response); forward the request to achieve

1.3 GET request and the POST request of the difference [face questions]

GET:

  • The address bar displays the parameters passed
  • Generally browser GET request data length limit
  • URL has borne
  • Unsafe

POST:

  • Address bar is not displayed parameters passed by the request transmission member
  • In general, the browser is no data length limit
  • URL does not have borne
  • Relatively safe

2. Master solve the Chinese garbled request

Chinese garbled POST request:

  • Method of request: void setCharacterEncoding (character set);

Chinese distortion GET request: the data is transferred through the URL address but Tomcat 8.0 following default URL encoded as ISO-8859-1

  • Palliatives: first decoded and then encoded

    • The obtained first parameter data (original code) is decoded to ISO-8859-1

      String的方法:byte[] getBytes(String encoding);
      
    • The contents then decoded UTF-8 (new code) re-encoding

      String的构造方法:String(byte[] bytes,String encoding);
      
  • Root of the problem:

    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>
    

3. understand the difference between the forwarding and redirection [face questions]

Forwards the request:

  • Address bar does not change or the original request
  • It was a request to forward the request
  • It can only jump forward the request to the server's internal resources
  • Server forwards the request is to help you make the jump page

Redirect:

  • The address bar will change into a new request address
  • Redirection is two requests
  • Redirect can jump to any address / resources
  • Redirect server to the client in response to an identification (302), and the client re-page jump

Borrow money: Chen Xu borrow money (associative memory).

4. master the use of session objects

  • void setAttribute (String name, Object value); this same content in a single session is effective to store the contents of the scope of the session

  • Object getAttribute (String name); take out the content from the session scope

  • void removeAttribute (String name); remove content from the session scope

  • String getId (); Get the current session of numbers

  • void invalidate(); 作废session

  • void setMaxInactiveInterval (int seconds); inactive session set period (default 30 minutes)

      未来要更改时间可以在自己项目中的web.xml配置如下内容。
      <!-- ==================== Default Session Configuration ================= -->
      <!-- You can set the default session timeout (in minutes) for all newly   -->
      <!-- created sessions by modifying the value below.                       -->
        <session-config>
            <session-timeout>30</session-timeout>
        </session-config>
    
  • int getMaxInactiveInterval (); get session period of inactivity

session life cycle (single session): When the browser (client) to access the server, the session started (to help you create a session object (each browser for each user are independent)), then as long as you is not fully closed / end corresponding to the browser (client), or does not exceed the session settings 非活动有效期(if you do not access the server, this will start to inactive time), the session has been maintained.

Early general session are used to store user login information, temporary information stored in the cart ...

The difference between the master and the session cookie [face questions]

Some sites such as Baidu in recorded history in the search tips

Some websites where you can check seven days free or login remember my login and other functions

It cookie and client related.

Remember me realize analog functions:

slightly

Essentially session is to use session-level cookie. When the browser first accesses the server, the server generates a session and writes the session id session-level cookie to the browser when the browser accesses the server will carry this cookie, session id in the cookie server will verify, if it means that same session, otherwise the session description does not exist or has expired, then it will repeat the previous step.

Cookie and Session difference:

  1. cookie can only store values ​​of type String, and session can store any type of content
  2. You can set a session cookie level can also be set lasting level, and only session-level session
  3. A cookie is a content stored by the client, and the session is stored in the content server
  4. cookie is not enough security, session relative safety, some important information session on the recommendation of the

6. Mastering application object

Application and request its general session and almost, it is also a scope.

tomcat vessel, there are four scope , which scope is used to store information.

page: The current page information can only be used in the current page

request: same request

session: same session

application: the same application (as long as the server does not shut down, application are the same)

Visitor statistics simulate a website:

  • void setAttribute(String name,Object value);
  • Object getAttribute(String name);
  • void removeAttribute(String name);
  1. When a user accesses the site, give the number of visitors to be incremented by one
  2. If you are the first user to access the site, then the number is initialized to 1 Access Statistics

Guess you like

Origin blog.csdn.net/weixin_44793608/article/details/94315158