分类: 系统运维
2010-11-19 20:23:40
构造简单mvc显示本地IP及系统时间
需要修改的文件有web.xml
需要创建的文件有HelloWorld.java
(一)web.xml的配置如下
|
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为所建的项目名