HttpServlet----One

Source: Internet
Author: User

Package secondweb;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import javax.servlet.ServletException;
Import Javax.servlet.http.Cookie;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;

public class One extends HttpServlet {

@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
Resp.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Resp.getwriter ();
Gets the value of the foreground individual element
String userName = Req.getparameter ("UserName");
String pwd = req.getparameter ("pwd");
String[] loves = Req.getparametervalues ("Love");//Get a set of values for an element with the same name
OUT.PRINTLN ("The data obtained from the front desk are as follows:" + "<br/>");
Out.println ("UserName:" + userName + "<br/>");
Out.println ("pwd:" + pwd + "<br/>");
Out.println ("Love:");
if (loves! = null) {
for (String s:loves) {
Out.print (s + "\ T");
}
}
/*
* The difference between the cookie and the session 1, the storage location is different. The cookie is stored on the client, and the contents of the session are stored on the server
* 2. The session has a dependency on the cookie. The ID of a session is stored in the cookie.
* 3, the size is different. The size of the cookie is fixed, each limit is 4K, and the number of cookies supported by each browser is approximately 120-140;
* Session can store large amounts of data, but consumes server resources
* 4, the security is different. The content of the cookie is the browser visible, the security is low; The session content is highly secure on the server
*/
HttpSession se = req.getsession ();//Create a HttpSession
Se.setmaxinactiveinterval (3600);//set session to expire at 3,600 seconds on the server
Se.setattribute ("pwd", pwd);//Set a key-value pair for the session
Cookie cookie = new Cookie ("name", userName);//Create a cookie that contains a key-value pair
Cookie.setmaxage (3600); Set the cookie expiration time to 3,600 seconds
Resp.addcookie (cookies);//Add the newly created cookie to the file
/* When the browser is closed, the cookie that originally saved SessionID will be removed. When we reopen the browser, although the server still has the session record,
* But the actual will open a session, in order to get the value of the original session, you can save the first time SessionID * *
Cookie Secookie = new Cookie ("Jsessionid", Se.getid ());//Create a session cookie that holds the session ID
Cookie.setmaxage (3600);
Resp.addcookie (Secookie);

/*
* The difference between request forwarding and response redirection: 1. The completed objects are different. Forwarding is performed on the server side; redirection is done by the client browser
* 2, the range is different. Forwarding can only be carried out within the current project of the server; The destination page of the redirect can be either the current project interior or an external page
* 3, the final address bar is different. Forwarding only the business to other pages processing, and then return to the sender, the address bar will not change; Redirect is the entire page jumps to a new path
* 4, the number of requests is different. Forward only one request, the requested information will not change, the redirect will occur at least two requests, the first request information will no longer exist
**/
Resp.sendredirect ("second");//redirect to a URL of second on the path
Req.getrequestdispatcher ("Second"). Forward (req,
RESP)//forwarding to a path with a URL of second
}

@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
System.out.println ("Enter the Post Method");
/*
* The difference between the Get and post methods 1, the type of data submitted. Get can only submit text; Post can submit text and binary text
* 2, the length of the submitted data. Get cannot exceed 255 characters; Post is theoretically unlimited
* 3, the visibility of the submitted data. The data in get is displayed as part of the URL address in the browser address bar, the user is visible, the security is low, the post data as the message body, the user is not visible,
* High Security 4, submit data cache. The Get method is cached in the history state of the browser URL, and the Post method is not cached by the browser
*/
Doget (req, resp);//When using post, place specific logical operations in the Doget method
}

}

HttpServlet----One

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.