web folder html file:
1 <form action="login" method="post"> 2 User: <input type="text" name="name"><br> 3 Password: <input type="password" name="password"><br> 4 <input type="submit" value="login"> 5 </form>
action for the login, the corresponding mapped login -> LoginServlet
After clicking submit, automatically jump to / login
WEB-INF web.xml file in the file is configured to:
1 <servlet> 2 <servlet-name>LoginServlet</servlet-name> 3 <servlet-class>LoginServlet</servlet-class> 4 </servlet> 5 6 <servlet-mapping> 7 <servlet-name>LoginServlet</servlet-name> 8 <url-pattern>/login</url-pattern> 9 </servlet-mapping>
When comparing two values, the previous constants, avoid NullPointerException
Examples of the servlet tomcat, and call its doGet or doPost method, the incoming request and response; the method of operation of unwinding ordered through the following response, tomcat get response, based on the content of the response generated html string to the browser, html browser to obtain the string according to the http protocol, and rendered on screen.
HttpServleti in the service without having to rewrite the source code
607 protected void service(HttpServletRequest req, HttpServletResponse resp) 608 throws ServletException, IOException { 609 610 String method = req.getMethod(); 611 612 if (method.equals(METHOD_GET)) { 613 long lastModified = getLastModified(req); 614 if (lastModified == -1) { 615 // servlet doesn't support if-modified-since, no reason 616 // to go through further expensive logic 617 doGet(req, resp); 618 } else { 619 long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); 620 if (ifModifiedSince < (lastModified / 1000 * 1000)) { 621 // If the servlet mod time is later, call doGet() 622 // Round down to the nearest second for a proper compare 623 // A ifModifiedSince of -1 will always be less 624 maybeSetLastModified(resp, lastModified); 625 doGet(req, resp); 626 } else { 627 resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 628 } 629 } 630 631 } else if (method.equals(METHOD_HEAD)) { 632 long lastModified = getLastModified(req); 633 maybeSetLastModified(resp, lastModified); 634 doHead(req, resp); 635 636 } else if (method.equals(METHOD_POST)) { 637 doPost(req, resp); 638 639 } else if (method.equals(METHOD_PUT)) { 640 doPut(req, resp); 641 642 } else if (method.equals(METHOD_DELETE)) { 643 doDelete(req, resp); 644 645 } else if (method.equals(METHOD_OPTIONS)) { 646 doOptions(req,resp); 647 648 } else if (method.equals(METHOD_TRACE)) { 649 doTrace(req,resp); 650 651 } else { 652 // 653 // Note that this means NO servlet supports whatever 654 // method was requested, anywhere on this server. 655 // 656 657 String errMsg = lStrings.getString("http.method_not_implemented"); 658 Object[] errArgs = new Object[1]; 659 errArgs[0] = method; 660 errMsg = MessageFormat.format(errMsg, errArgs); 661 662 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); 663 } 664 }
service doGet, doPost, service parameter passing are the same
Servlet is a single instance, constructors and initialization methods only once
destroy:
1. Servlet web application that resides restart
2. tomcat Close
reloadable: If this property is set to true, tomcat server running in the state will monitor the changes in class files in the WEB-INF / classes and WEB-INF / lib directory, if there is to monitor the class file is updated, the server will automatically re Web application load.
During the development phase will reloadable property to true, useful for debugging and other servlet class files, but that the server is running with heavier loads, it is recommended fat deposit stage of the reloadable Web application is set to false.
Jump server
request.getRequestDispatcher("success.html").forward(request, response);
Jump Client
response.sendRedirect("fail.html");
servlet life cycle is to start in the browser to access the specified directory before, but in some cases they need to start using servlet's service, so it is necessary from the start, since the start in the init () can do some work in the business code
<load-on-startup> 10 </ load-on-startup> where 10 represents the priority, the lower the number, the priority
--- end --- restore content
web folder html file:
1 <form action="login" method="post"> 2 User: <input type="text" name="name"><br> 3 Password: <input type="password" name="password"><br> 4 <input type="submit" value="login"> 5 </form>
action for the login, the corresponding mapped login -> LoginServlet
After clicking submit, automatically jump to / login
WEB-INF web.xml file in the file is configured to:
1 <servlet> 2 <servlet-name>LoginServlet</servlet-name> 3 <servlet-class>LoginServlet</servlet-class> 4 </servlet> 5 6 <servlet-mapping> 7 <servlet-name>LoginServlet</servlet-name> 8 <url-pattern>/login</url-pattern> 9 </servlet-mapping>
When comparing two values, the previous constants, avoid NullPointerException
Examples of the servlet tomcat, and call its doGet or doPost method, the incoming request and response; the method of operation of unwinding ordered through the following response, tomcat get response, based on the content of the response generated html string to the browser, html browser to obtain the string according to the http protocol, and rendered on screen.
HttpServleti in the service without having to rewrite the source code
607 protected void service(HttpServletRequest req, HttpServletResponse resp) 608 throws ServletException, IOException { 609 610 String method = req.getMethod(); 611 612 if (method.equals(METHOD_GET)) { 613 long lastModified = getLastModified(req); 614 if (lastModified == -1) { 615 // servlet doesn't support if-modified-since, no reason 616 // to go through further expensive logic 617 doGet(req, resp); 618 } else { 619 long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE); 620 if (ifModifiedSince < (lastModified / 1000 * 1000)) { 621 // If the servlet mod time is later, call doGet() 622 // Round down to the nearest second for a proper compare 623 // A ifModifiedSince of -1 will always be less 624 maybeSetLastModified(resp, lastModified); 625 doGet(req, resp); 626 } else { 627 resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 628 } 629 } 630 631 } else if (method.equals(METHOD_HEAD)) { 632 long lastModified = getLastModified(req); 633 maybeSetLastModified(resp, lastModified); 634 doHead(req, resp); 635 636 } else if (method.equals(METHOD_POST)) { 637 doPost(req, resp); 638 639 } else if (method.equals(METHOD_PUT)) { 640 doPut(req, resp); 641 642 } else if (method.equals(METHOD_DELETE)) { 643 doDelete(req, resp); 644 645 } else if (method.equals(METHOD_OPTIONS)) { 646 doOptions(req,resp); 647 648 } else if (method.equals(METHOD_TRACE)) { 649 doTrace(req,resp); 650 651 } else { 652 // 653 // Note that this means NO servlet supports whatever 654 // method was requested, anywhere on this server. 655 // 656 657 String errMsg = lStrings.getString("http.method_not_implemented"); 658 Object[] errArgs = new Object[1]; 659 errArgs[0] = method; 660 errMsg = MessageFormat.format(errMsg, errArgs); 661 662 resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg); 663 } 664 }
service doGet, doPost, service parameter passing are the same
Servlet is a single instance, constructors and initialization methods only once
destroy:
1. Servlet web application that resides restart
2. tomcat Close
reloadable: If this property is set to true, tomcat server running in the state will monitor the changes in class files in the WEB-INF / classes and WEB-INF / lib directory, if there is to monitor the class file is updated, the server will automatically re Web application load.
During the development phase will reloadable property to true, useful for debugging and other servlet class files, but that the server is running with heavier loads, it is recommended fat deposit stage of the reloadable Web application is set to false.
Jump server
request.getRequestDispatcher("success.html").forward(request, response);
Jump Client
response.sendRedirect("fail.html");
servlet life cycle is to start in the browser to access the specified directory before, but in some cases they need to start using servlet's service, so it is necessary from the start, since the start in the init () can do some work in the business code
<load-on-startup> 10 </ load-on-startup> where 10 represents the priority, the lower the number, the priority
1 import java.io.File; 2 import java.io.FileNotFoundException; 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 import java.io.PrintWriter; 6 import java.util.Iterator; 7 import java.util.List; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import org.apache.commons.fileupload.FileItem; 15 import org.apache.commons.fileupload.FileUploadException; 16 import org.apache.commons.fileupload.disk.DiskFileItemFactory; 17 import org.apache.commons.fileupload.servlet.ServletFileUpload; 18 19 public class UploadPhotoServlet extends HttpServlet { 20 21 public void doPost(HttpServletRequest request, HttpServletResponse response) { 22 23 String filename = null; 24 try { 25 Factory = DiskFileItemFactory new new DiskFileItemFactory (); 26 is ServletFileUpload = Upload new new ServletFileUpload (Factory); 27 // upload file size is limited to 1M 28 factory.setSizeThreshold (1024 * 1024 ); 29 30 List items = null ; 31 is the try { 32 = items upload.parseRequest (Request); 33 is } the catch (FileUploadException E) { 34 is e.printStackTrace (); 35 } 36 37 [ the Iterator ITER = items.iterator (); 38 is the while (iter.hasNext ()) { 39 the FileItem Item = (the FileItem) iter.next (); 40 IF (! Item.isFormField ()) { 41 is 42 is // The time create a stamp picture file 43 filename = System.currentTimeMillis () + ".jpg" ; 44 45 // get the current directory, and follow it with "uploaded" 46 . photoFolder String = request.getServletContext () getRealPath ( "uploaded" ); 47 48 File F = new new File (photoFolder, filename); 49 f.getParentFile () mkdirs ();. // New Directory 50 51 is // Get the input stream browser files uploaded by item.getInputStream () 52 is the InputStream IS = item.getInputStream (); 53 is 54 is // copy files Cong read bytes in the input stream, the output stream from bytes written back 55 a FileOutputStream fos = new new a FileOutputStream (F); 56 is byte B [] = new new byte [1024 * 1024 ]; 57 is int length = 0 ; 58 the while (-1! = (length = is.read (B))) { 59 fos.write(b, 0, length); 60 } 61 fos.close(); 62 63 } else { 64 System.out.println(item.getFieldName()); 65 String value = item.getString(); 66 value = new String(value.getBytes("ISO-8859-1"), "UTF-8"); 67 System.out.println(value); 68 } 69 } 70 71 String html = "<img width='200' height='150' src='uploaded/%s' />"; 72 response.setContentType("text/html"); 73 PrintWriter pw= response.getWriter(); 74 75 pw.format(html, filename); 76 77 } catch (FileNotFoundException e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 } catch (Exception e) { 81 // TODO Auto-generated catch block 82 e.printStackTrace(); 83 } 84 } 85 }
Dynamic web Project:
Ide run directly inside, you do not configure server.xml, but also need to configure web.xml of the project;
classes in the build, if there is the package name, then, web.xml in the servlet-class still have to add the package name;
After the run, the domain name you want to add the project name plus the url-pattern
Ordinary java project converted to Dynamic web Project:
Properties Facets => Dynamic Web Module => Further configuration available => content directory写web