Chinaunix首页 | 论坛 | 博客
  • 博客访问: 410201
  • 博文数量: 101
  • 博客积分: 2324
  • 博客等级: 大尉
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-19 19:28
文章分类

全部博文(101)

文章存档

2012年(3)

2011年(60)

2010年(38)

分类: 系统运维

2010-11-19 20:23:40

构造简单mvc显示本地IP及系统时间

需要修改的文件有web.xml

需要创建的文件有HelloWorld.java

(一)web.xml的配置如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns=""
    xmlns:xsi=""
    xsi:schemaLocation=
"
    /web-app_2_5.xsd"
>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
     <servlet-name>helloworldServlet</servlet-name>
     <servlet-class>shark.zd.servlets.HelloWorld</servlet-class>
     <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloworldServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

</web-app>

(二)所建的HelloWorld类

package shark.zd.servlets;

import java.io.IOException;

import java.io.PrintWriter;

import java.net.InetAddress;

import java.util.Calendar;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

protected void doGet(HttpServletRequest request,

HttpServletResponse response)

 throws ServletException, IOException

{ // 获取系统当前时间start

int y,m,d,h,mi,s;

Calendar cal=Calendar.getInstance();

y=cal.get(Calendar.YEAR);

m=cal.get(Calendar.MONTH);

d=cal.get(Calendar.DATE);

h=cal.get(Calendar.HOUR_OF_DAY);

mi=cal.get(Calendar.MINUTE);

s=cal.get(Calendar.SECOND);

//end

PrintWriter out = response.getWriter();

InetAddress inet = InetAddress.getLocalHost();

out.print("Local ip :"+inet.getHostAddress()

+" "+"nowdata:"+y+"Y"+m+"D"+d+"D"+" "+h+":"+mi+":"+s

+" ");

try { Thread.sleep(5000); }

catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

out.print("Local ip :"+inet.getHostAddress()

+" "+"nowdata:"+y+"Y"+m+"M"+d+"D"+" "+h+":"+mi+": "+

s+" ");

}

protected void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException

{ int y,m,d,h,mi,s; Calendar cal=Calendar.getInstance();

y=cal.get(Calendar.YEAR);

m=cal.get(Calendar.MONTH);

d=cal.get(Calendar.DATE);

h=cal.get(Calendar.HOUR_OF_DAY);

mi=cal.get(Calendar.MINUTE);

s=cal.get(Calendar.SECOND);

PrintWriter out = response.getWriter();

InetAddress inet = InetAddress.getLocalHost();

out.print("Local ip :"+inet.getHostAddress()

+" "+"nowdata:"+y+"Y"+m+"M"+d+"D"+" "+h+":"+mi+": "+

s+" ");

try { Thread.sleep(5000); }

catch (InterruptedException e)

{ // TODO Auto-generated catch block

e.printStackTrace();

}

out.print("Local ip :"+inet.getHostAddress()+" "+"nowdata:"+y+"Y"+m+"M"+d+"D"+" "+h+":"+mi+": "+s+" ");

}

}

(三)上述配置完后把项目加入Tomcat然后开启Tomcat 之后再Internet中输入即可显示结果其中ServletDemo为所建的项目名


阅读(590) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:蝶恋花

给主人留下些什么吧!~~