JavaWeb learning, day2

Forwarding and Redirection

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    登陆成功<br>
    欢迎你:
    <%
        String name=request.getParameter( "username" );
    %>
<%=name%>
</body>
</html>

Forwarded

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show</title>
</head>
<body>
    <%
        //设置编码
        request.setCharacterEncoding( "UTF-8" );
        String name=request.getParameter( "username" );
        String pwd=request.getParameter( "password" );
        if(name.equals( "zs" )&&pwd.equals( "abc" )){
            request.getRequestDispatcher( "success.jsp" ).forward( request,response );
            //可以获取到数据,并且地址栏没有改变(任然保留转发时的地址)
        }else{
            out.print( "登陆失败" );
        }
    %>
</body>
</html>

Here Insert Picture Description
Redirect

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show</title>
</head>
<body>
    <%
        //设置编码
        request.setCharacterEncoding( "UTF-8" );
        String name=request.getParameter( "username" );
        String pwd=request.getParameter( "password" );
        if(name.equals( "zs" )&&pwd.equals( "abc" )){
            response.sendRedirect( "success.jsp" );       //重定向会导致数据丢失
		}else{
            out.print( "登陆失败" );
        }
    %>
</body>
</html>

Here Insert Picture Description
![

The difference between forwarding and redirection
Request forwarding Redirect
Whether to change the address bar constant change
Whether to retain data when the first request Retention Not Retained
The number of requests 1 2
Jump position occurs server The second client makes the jump

](https://img-blog.csdnimg.cn/20200313174522838.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l1Y2FuMTIzNA==,size_16,color_FFFFFF,t_70)

Session

Cookie (client, not built-in object):
 cookie: Key = value
 is not built-in objects is provided by a packet java.servlet.http.Cookie java the

 conventional method
 public Cookie (String key, String value ) constructor
 String getName (); Get name
 String the getValue (); Get value
 void the setMaxAge (int expiry); set period (seconds)



 cookies generated by the server, and then sent to the client saved. Equivalent to the local cache of the role of
 such visit, the first visit when the client generates a Cookie, sent to the client, then the second visit, it is a very fast.
 Server sends to the client process
  client is ready Cookie: respond.addCookie (Cookie cookie);
  page jump (forward, redirect);
  client obtains cookie: request. the getCookie ();
 Note: a. Increase server cookie: response objects; client obtains the object; request objects
     b. Not directly past a certain object only once to get all of the cookies
  advantages: improve access rate, but poor security.
  Disadvantages: poor security (passwords, etc.)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show</title>
</head>
<body>
    <%
        Cookie cookie1=new Cookie( "name","b" );
        Cookie cookie2=new Cookie( "password","34" );
        response.addCookie( cookie1 );
        response.addCookie( cookie2 );
        //页面跳转到客户端
        response.sendRedirect( "result.jsp" );
    %>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            out.print(cookie.getName()+"--"+cookie.getValue()+"<br>");
        }
    %>
</body>
</html>

Showing results
Here Insert Picture Description
View console found by F12, in addition to its own set of objects Cookies, there is a name for the JSESSIONID of Cookie


Cookie use Autosave username.

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"
    import="java.util.Date" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%!
        String name;
    %>
    <%
        Cookie[] cookies=request.getCookies();
        for(Cookie cookie:cookies){
            if(cookie.getName().equals( "name" )){
                name=cookie.getValue();
            }
        }
    %>
  <form action="show.jsp" method="post">
      用户名:<input type="text" name="username" value="<%=(name==null?"":name)%>"/><br/>		
      //这里有个等式表达式包括三目运算符的地方,需要注意
      密码:<input type="password" name="password"/><br/>
      <input type="submit" value=登录>
  </form>
  </body>
</html>

show.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>show</title>
</head>
<body>
    <%
    	//将相关信息存放到cookies中
        request.setCharacterEncoding( "utf-8" );
        String name=request.getParameter( "username" );
        String password=request.getParameter( "password" );
        Cookie cookie_name=new Cookie( "name",name );
        response.addCookie( cookie_name );
    %>
</body>
</html>

  The first time into the browser, the user name is blank, but the second time of entry, automatically displays the user name entered for the first time

session (session)

  Visit the website: Start - close Shopping: Browse - Payment - to withdraw from the series is a session
session mechanism:
 when a client first request to the server, the server generates a session object (the user to save the customer information);
 and each session object, there will be a unique sessionId (used to distinguish it from other session)
 server will produce a cookie, and the cookie name = JSESSIONID, value = value sessionId server, then the server response to the client at the same time, send the cookie to the client, so far, the client will have a cookie (the JSESSIONID); therefore, the client can cookie and one-server session (corresponding to the JSESSIONID sessionId)
 after a request, the server based on the client the cookie to serve JSESSIONID end of the session matching sessionId If a match (and the session cookie of JSESSIONID sessionId) describes the user is not the first visit, without logging in.


Commonly used methods:
  String geiId (); // get sessionId
  boolean the IsNew (); // determine whether it is a new user (first visit)
  void the invalidate (); // the session failure; (Log cancellation)
  setAttribute () ;
  the getAttribute ();
  void the setMaxInactiveInterval (); // set the maximum effective inactive time (sec)
  int getMaxInactiveInterval (); // get the maximum effective period of inactivity





case, using the session to obtain a user name
index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java"
    import="java.util.Date" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <form action="check.jsp" method="post">
      用户名:<input type="text" name="username" /><br/>
      密码:<input type="password" name="password"/><br/>
      <input type="submit" value=登录>
  </form>
  </body>
</html>

check.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        request.setCharacterEncoding( "utf-8" );
        String name=request.getParameter( "username" );
        String password=request.getParameter( "password" );
        if(name.equals( "zs" )&&password.equals( "abc" )){
            //只有登录成功session中才会存在username和password
            session.setAttribute( "username",name );
            session.setAttribute( "password",password );
            request.getRequestDispatcher( "sessionDemo.jsp" ).forward( request,response );
        }else {
            response.sendRedirect( "index.jsp" );
        }
    %>
</body>
</html>

sessionDeom.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    欢迎你:
    <%
        String name= (String) session.getAttribute( "username" );
        //如果用户没有登陆,直接访问sessionDemo.jsp则必然获得name==null
        if(name!=null){
            out.print( name );
        }else{
            response.sendRedirect( "index.jsp" );
        }
    %>
</body>
</html>
Published 11 original articles · won praise 0 · Views 164

Guess you like

Origin blog.csdn.net/yucan1234/article/details/104845783