HttpServlet----

Source: Internet
Author: User

Package secondweb;

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

Import Javax.servlet.ServletContext;
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 second extends HttpServlet {

@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
Resp.setcontenttype ("Text/html;charset=utf-8");
PrintWriter out = Resp.getwriter ();
/*application and Session
* 1, both can use a name to bind an object, so as to access within the scope
* 2, the life cycle is different. The session only exists in the declared time; application exist during Web application run */
ServletContext con=this.getservletconfig (). Getservletcontext ();//Get Application Context
String Context=con.getinitparameter ("context");//Get the value called "context"
Out.print ("The value of the context saved by application is:" +context+ "<br/>");

cookie[] cookies = req.getcookies ();//Get all cookies stored by the browser
if (cookie = null) {
for (Cookie cookie:cookies) {
if (Cookie.getname () equals ("name")) {//Find a cookie that is called "name"
Out.print ("The name value saved by the cookie is:" + cookie.getvalue () + "<br/>");
}
}
}

HttpSession SE = req.getsession (false);//The session stored in the server is obtained based on the SessionID saved in the cookie
if (SE = = null) {
Out.print ("No Saved Session");
}else{
Out.print (the PWD value saved by the session is: "+ se.getattribute (" pwd ");}
}

@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
Doget (req, resp);
}

}

HttpServlet----

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.