JavaWeb study notes (continuously updated --- Session)

一.Java Servlet :

   Java program is run on a Web server or application server, which is a database or between applications on the server and HTTP request from the Web browser or other HTTP clients intermediate layer.

The use of Servlet, you can collect user input from a Web form, showing records from a database or other source, you can also create dynamic web pages.

You may receive a request to return a response

Servlet unrelated to the root directory of access paths and packet addresses / representatives when configured with only comment url is project

   

 

By accessing the servlet jsp:

(1) Jsp and servlet same package: Direct Access

(2) 1.Jsp the servlet different packages, but in the upper jsp: such as: root directly coupled "servlet package name" / "servlet name"

2.Jsp different packages with a servlet, jsp lower layer: ".." / "servlet package name" / "servlet name"

 

Reverse similar access.

 

 

two. JSP:

JSP stands for Java Server Pages, HTML + CSS + is a dynamic web development technology. It uses the Java code in JSP tag into the HTML page. Tags typically begin with <% in%> end.

 

JSP is a Java servlet on nature, mainly used for the user interface portion implement Java web applications. Web developers through a combination of HTML code, XHTML code, XML elements and embedded operating JSP and commands to write JSP.

JSP get user input data, access databases and other data sources via a web form, and then dynamically create web pages.

JSP-based Java Servlet API, therefore, JSP has a variety of powerful enterprise-level Java API, including JDBC, JNDI, EJB, JAXP, and so on.

 

 

three. Parameters passed in three ways:

   1.URL传参: eg. <a href="RegServlet?name=admin&pwd=123">访问regServlet</a>

   2. Form parameter passing:

Pass parameters to achieve the form:

      Form element name corresponds to the parameter name

      Values ​​in the form corresponding to the input parameter value

      Request.getParamerter("c")

   3. hidden fields to achieve mass participation (of the form elements):

<input type="hidden" name="hide" value="testHide"/>

 

doGet ---- get treatment type

4.get and post a request for the information package is a difference

1. Security:

     DoGet information in the address bar

     Information Location:

Get: URL;

        Post: request objects;

  2. Submit Content Size

     Get less content

     Post more content

  3. encoding issues process

     Post: coding setting request object

     Get: URL, coding setting request does not work, it is necessary to manually set encoding

 

// Form form: action = "? Post (or get) mass participation"

       Get can not pass

       Post can pass

5. solve the Chinese garbled in three ways:

1. Set encoded request and response objects (doPost method is only valid for, because the information in the request is not doGet method)

(1) response.setContentType("text/html;charset=utf-8");

(2) request.setCharacterEncoding("UTF-8");

 

2. transcoding manually force (for the doGet)

String name=new String(userName.getBytes("ISO-8859-1"),"utf-8");

 

3. Modify the url encoding tomcat (doGet way for once and for all)

6. Response (concept of flow):

  

7. redirection request forwarding and

Redirect: The client browser makes a request twice (the second request data request can not be the first time)

    response.sendRedirect("response.jsp");

   

Forwards the request: the address bar does not change, only the client browser sends a request

request.getRequestDispatcher("response.jsp").forward(request, response);

 

8.request scope (Information Sharing area)

(Request to forward valid)

 Request.setAttribute ( "name", "content"): the request information into the shared area (key-value pairs)

 Request.getAttribute ( "name"): take the request information from the shared area

 

the reason:

Redirect: two requests, information sharing area put! = Area information sharing taken

Forwards the request: a request, the discharge area information sharing information sharing area taken ==

9. Session technologies: session, cookie

1.Session: the server side information shared area information sharing across multiple requests.

 

2. Common use:

Jsp automatically creates a session object;

Servlet does not automatically create session, create session objects by getSession ()

SessionServlet get inside the session:

HttpSession session=request.getSession();

Session.setAttribute(“name”,”my name is Leon.”);

Jsp access session:

 <%String name= session.getAttribute(“name”);

 Out.print (name); ------------------- because in the jsp, there is no system

%>

 

3.Session common API:

 

(1) obtaining session:

HttpSession session = request.getSession (); // get the session object, and if not, created after the return;

Session=request.getSession(true);

// If it is true: getSession () will get the session; / If it is false: get the current session, if there is no return Null

 

(2) obtain sessionId:

System.out.println(session.getId());

(3) a method of obtaining the session spaced time:

System.out.println (session.getCreationTime ()); // create time of the session

System.out.println (session.getLastAccessedTime ()); // after the last time to get this conversation over how much time

Session.setMaxInactiveInterval (10); // set the session 10S survival time

System.out.println (session.getMaxInactiveInterval ()); // session is survival

(4) Session of the cancellation:

Session.invalidate();

// session set the session time in web.xml

//<session-config>

//<session-timout></session-timeout>

//</session-config>

Guess you like

Origin www.cnblogs.com/LeonLiuBlog/p/11829485.html