java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器

2014-11-24 03:31:51 · 作者: · 浏览: 0
实例1:实现客户端IP地址和访问方式输出到 浏览器
IpAction.java
View Code
ip_struts.xml
复制代码
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
method="execute">
复制代码
struts.xml
复制代码
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
复制代码
配置完成,部署运行,查看输出。
输出结果截图:
代码分析:
IpAction.java
struts2提供了使用ServletActionContext封装了常用的request和response对象,这里
>>1、使用ServletActionContext.getRequest()获取request对象,
>>2、ServletActionContext.getResponse()获得response对象。
>>3、使用request.getRemoteAddr()方法获取访问端的ip地址;
>>4、request.getMethod()方法获取客户端访问的方式;
ip_struts.xml
包名必须唯一,namespace可以省略,result属性这里没有用到,因为IpAction中return null,如果retrun "string" ,则必须配置result;
struts.xml
这里不需要配置其他的,只需要引入ip_struts.xml文件即可,使用include引入,引入的地址为相对路径,是否引入成功,查看tomcat启动时的提示信息,或者ctrl点击路径看能不能访问。
实例2:将实例1中的信息通过 jsp输出
方法一:
IpAction.java
View Code
ip_struts.xml
复制代码
< xml version="1.0" encoding="UTF-8" >
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
method="execute">
/ip.jsp
复制代码