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----