When learning the servlet today, the Web page effect displayed by the browser is different from what is expected, looking closely for the found real <! Doctype> the issue of the statement, as follows:
The code is as follows:
PackageSecondservlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classSecondservletextendsHttpServlet {/** * */ Private Static Final LongSerialversionuid = 87826386236823L; /*** Execute this function when accessing a page in Get mode * Executing the Doget method executes the Getlastmodified method, and if the browser discovers that the Getlastmodified method returns * The same value as the last returned value, the document is considered not to be updated. The browser takes the cache without executing the Doget method * If the Getlastmodified method returns-1, it is considered to be a constant update and always executes the function*/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException { This. log ("Execute Doget Method ..."); This. Execute (Request, response); } /*** This function is executed when the page is accessed by post. Getlastmodified method not executed before execution **/ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException { This. log ("Execute Dopost Method ..."); This. Execute (Request, response); } @Override Public Longgetlastmodified (HttpServletRequest request) { This. log ("Execute Getlastmodified Method ..."); return0; } Public voidExecute (httpservletrequest request, httpservletresponse response)throwsioexception{response.setcharacterencoding ("UTF-8"); Request.setcharacterencoding ("UTF-8"); String RequestUri=Request.getrequesturi (); String Method=Request.getmethod (); String param= Request.getparameter ("param"); Response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");//out.println ("<! DOCTYPE html> ");Out.println ("<HTML>"); Out.println ("); Out.println ("<body>"); Out.println ("+ Method +" to access the page. The Param parameters are: "+ param +" <br/> "); Out.println ("<form action=" + RequestUri + "' method= ' get ' > <input type= ' text ' name= ' param ' value= ' param string ' ><in Put type= ' submit ' value= ' Get way Query page Requestservlet ' ></form> '); Out.println ("<form action=" + RequestUri + "' method= ' post ' > <input type= ' text ' name= ' param ' value= ' param string ' ><i Nput type= ' Submit ' value= ' post to page Requestservlet ' ></form> '); Out.println ("<script>document.write (' This page last updated: ' + document.lastmodified + ' <br/> '); </script> "); Out.println ("<script>document.write (' this page URL: ' + location + ' <br/> '); </script> "); Out.println ("</body>"); Out.println ("</HTML>"); Out.flush (); Out.close (); }}
Java Web Note servlet (1)