Second, Java Web--use servlet to display server time

Source: Internet
Author: User
Tags java web

First, create the following package under the project, and the servlet

The resulting servlet is as follows:

@WebServlet (name= "Timmerservlet", Urlpatterns = "/showtime.do" ) Public classTimmerservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L;  PublicTimmerservlet () {Super(); }    protected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//set Refresh Auto-load event interval to 1 secondsResponse.setintheader ("Refresh", 1); //Setting the response content typeResponse.setcontenttype ("text/html"); //gets the current timeCalendar now = calendar.getinstance (Timezone.gettimezone ("Gmt+8")); PrintWriter out=Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>");          Out.print (Now.gettime ()); Out.println ("</BODY>"); Out.println ("</HTML>");          Out.flush ();    Out.close (); }    protected voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}

Second, @WebServlet annotations

Under servlet3.0, Webservlet annotations are provided to replace the steps to configure <servlet>, <servlet-mapping> under Web. Xml.

The annotated source code is as follows:

 Packagejavax.servlet.annotation;Importjava.lang.annotation.Annotation;Importjava.lang.annotation.Documented;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target; @Target ({Java.lang.annotation.ElementType.TYPE}) @Retention ( Retentionpolicy.runtime) @Documented Public@InterfaceWebservlet {//Specifies the Name property of the Servlet, equivalent to <servlet-name>. If not explicitly specified, the value of the Servlet is the fully qualified name of the class.      Public AbstractString name (); //This property is equivalent to the Urlpatterns property. Two attributes cannot be used at the same time.      Public Abstractstring[] Value (); //Specifies the URL-matching pattern for a set of Servlets. Equivalent to <url-pattern> tag.      Public Abstractstring[] Urlpatterns (); //specifies the order in which the Servlet is loaded, equivalent to the <load-on-startup> tag.      Public Abstract intLoadonstartup (); //specifies a set of Servlet initialization parameters, equivalent to the <init-param> tag.      Public Abstractwebinitparam[] InitParams (); //declares whether the Servlet supports asynchronous operation patterns, equivalent to <async-supported> tags.      Public Abstract Booleanasyncsupported ();  Public AbstractString SmallIcon ();  Public AbstractString LargeIcon (); //The Servlet's descriptive information is equivalent to the <description> tag.      Public AbstractString description (); //the display name of the Servlet, usually used in conjunction with the tool, is equivalent to the <display-name> tag.      Public AbstractString displayName ();}

Second, Urlpatterns wildcard characters

There are only three matching methods, either path matching ( beginning with the "/" character and ending with "/*" ) or extension matching ( "*."). Start ), either exact match, three matching methods cannot be combined, do not assume that wildcards or regular rules are used.

such as <url-pattern>/user/*.action</url-pattern> is illegal

Also note that:<url-pattern>/aa/*/bb</url-pattern> is precisely matched, legal, and here the * is not the meaning of the wildcard

<servlet-mapping>    <Servlet-name>Servlet</Servlet-name>    <Url-pattern>*.do</Url-pattern></servlet-mapping><servlet-mapping>    <Servlet-name>Servlet</Servlet-name>    <Url-pattern>/action/*</Url-pattern></servlet-mapping>

Three, start Tomcat, type http://localhost:8080/jeb/showtime.do, you can see the server time is displayed every second

Second, Java Web--use servlet to display server time

Related Article

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.