java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器
实例1:实现客户端IP地址和访问方式输出到浏览器。
IpAction.java
package com.amos.web.action; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /**
* @ClassName: IpAction
* @Description: TODO
* @author: amosli
* @email:amosli@infomorrow.com
* @date Jan 5, 2014 4:22:06 PM
*/
public class IpAction extends ActionSupport {
private static final long serialVersionUID = -5488264951167516910L; public String execute() throws Exception {
// 获取HttpServletRequest对象和HttpServletResponse对象
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
// 获取客户端ip地址
String clientIP = request.getRemoteAddr();
// 取得客户端的访问方式
String method = request.getMethod();
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("客户端访问的IP为:" + clientIP + "<br>");
response.getWriter().write("客户端的访问方式为:" + method);
return null;
}
}
ip_struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="ip" namespace="/" extends="struts-default">
<action name="IpAction" class="com.amos.web.action.IpAction"
method="execute"></action>
</package>
</struts>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<!-- 加载其他配置文件 -->
<include file="config/ip_struts.xml"></include>
</struts>
配置完成,部署运行,查看输出。
输出结果截图:

代码分析:
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
package com.amos.web.action; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /**
* @ClassName: IpAction
* @Description: TODO
* @author: amosli
* @email:amosli@infomorrow.com
* @date Jan 5, 2014 4:22:06 PM
*/
public class IpAction extends ActionSupport {
private static final long serialVersionUID = -5488264951167516910L;
public String execute() throws Exception {
// 获取HttpServletRequest对象和HttpServletResponse对象
HttpServletRequest request = ServletActionContext.getRequest();
// HttpServletResponse response = ServletActionContext.getResponse();
// 获取客户端ip地址
String clientIP = request.getRemoteAddr();
// 取得客户端的访问方式
String method = request.getMethod();
// response.setContentType("text/html;charset=utf-8");
// response.getWriter().write("客户端访问的IP为:" + clientIP + "<br>");
// response.getWriter().write("客户端的访问方式为:" + method);
request.setAttribute("clientIP", clientIP);
request.setAttribute("method", method);
System.out.println("method:"+method);
return "toJSP";
}
}
ip_struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="ip" namespace="/" extends="struts-default">
<action name="IpAction" class="com.amos.web.action.IpAction"
method="execute">
<result name="toJSP" type="dispatcher">
/ip.jsp
</result>
</action>
</package>
</struts>
ip.jsp是放在webapp目录下的
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String method=request.getAttribute("method").toString();
String clientIP=request.getAttribute("clientIP").toString();
%>
客户端IP是:<%=clientIP %><br/>
客户端的访问方式为:<%=method %><br><hr>
客户端访问的IP为:${requestScope.clientIP }<br>
客户端的访问方式为:${requestScope.method}<br>
</body>
</html>
输出结果截图:
代码分析:
IpAction.java中使用request对象的setAttribute方法传值到request域中。如下所示:
request.setAttribute("clientIP", clientIP);
request.setAttribute("method", method);
并且return “toJSP”,不再局限于"success",其实只要ip_struts.xml中result中name属性值与其对应也就ok了。
ip_struts.xml 中使用转发默认方式(转发),name的属性值为"toJSP"与 IpAction.java中execute方法返回值相对应。然后将值转发到ip.jsp里
<result name="toJSP" type="dispatcher">
/ip.jsp
</result>
ip.jsp中采用了两种取值方式,第一种是基础的传值方式,传值是成功的,另外一种是简写的,一种都没成功,这里先帖出来,以后再慢慢研究吧。
TODO:servlet和jsp 的传值问题。
使用request对象的getAttribute方法来取值,然后再使用jsp的基本输出方式将值输出。
servlet有三个域对象:HttpServletRequest、ServletContext、HttpSession
方法二:
使用ServletContext如何进行传值呢?如下所示,IpAction类中的execute方法改写为:
// 获取HttpServletRequest对象和HttpServletResponse对象
HttpServletRequest request = ServletActionContext.getRequest();
// 获取servletcontext域
ServletContext servletContext = ServletActionContext.getServletContext();
// 获取客户端ip地址
String clientIP = request.getRemoteAddr();
// 取得客户端的访问方式
String method = request.getMethod(); // 将信息绑定到servletContext中
servletContext.setAttribute("clientIP", clientIP);
servletContext.setAttribute("method", method); return "toJSP";
ip.jsp改写为:
<%
String method=application.getAttribute("method").toString();
String clientIP=application.getAttribute("clientIP").toString();
%>
客户端IP是:<%=clientIP %><br/>
客户端的访问方式为:<%=method %><br><hr>
[客户端访问的IP为]:${applicationScope.clientIP }<br>
[客户端的访问方式为]:${applicationScope.method}<br>
输出结果为:

方法三(重点):
Ip_Action.java 中execute方法需要改的地方:
//获取HttpSession域
Map<String, Object> session = ActionContext.getContext().getSession();
//将信息绑定到servletSession中
session.put("clientIP", clientIP);
session.put("method", method);
ip.jsp中需要改动的地方:
<%
String method=session.getAttribute("method").toString();
String clientIP=session.getAttribute("clientIP").toString();
%>
客户端IP是:<%=clientIP %><br/>
客户端的访问方式为:<%=method %><br><hr>
[[客户端访问的IP为]]:${sessionScope.clientIP }<br>
[[客户端的访问方式为]]:${sessionScope.method}<br>
这里使用ActionContext获取session对象,再用getAttribute方法获取传过来的值。
输出结果为:

为什么ActionContext.getContext().getSession()会是Map集合?
struts2中有很多个默认拦截器(interceptor),在struts2源码中struts-default.xml中可以查看具体有哪些拦截器。interceptor-stack中有18个拦截器。
其中有一个拦截器将map中的值转为session值,例如,String method=map.get("method");HttpSession.setAttribute("method",method)
自动将map中的值转为session,这样做的目的是尽量少出现或者不出现ServletAPI,以达到解耦的作用。
java struts2入门学习实例--将客户端IP地址和访问方式输出到浏览器的更多相关文章
- java struts2入门学习实例--用户注册
一.用户注册示例 register.jsp <%@ page language="java" contentType="text/html; charset=UT ...
- java struts2入门学习实例--使用struts2快速实现上传
一.文件上传快速入门 1).关于上传表单三要素 >>尽量以POST请求方式上传,因为GET支持文件大小是有限制的. >>必须要加上enctype="multipart ...
- java struts2入门学习实例--使用struts进行验证
一.为什么要进行验证? 验证几乎是注册登录的必须前提,验证的主要作用有两点: 1.安全性 2.对用户提供差异化服务. 二.如何验证? ActionSupport类中有一个validate()方法,这是 ...
- java struts2入门学习实例--使用struts2快速实现多个文件上传
一.错误提示信息配置 昨天说到更改默认错误配置信息,我测试很多遍,一直都不对.下面贴出来,待以后有好方法了再补充吧. 首先新建一个properties文件,这里命名为testupload.proper ...
- java struts2入门学习实例--用户注册和用户登录整合
需求: 1.用户注册(user_register.jsp)-->注册成功(UserRegister.action)-->显示注册信息(register_success.jsp)2.用户登录 ...
- java struts2入门学习---中文验证、对错误消息的分离、结果(result)类型细节配置
一.需求 利用struts2实现中文验证并对错误消息的抽离. 详细需求:用户登录-->不填写用户名-->页面跳转到用户登录页面,提示用户名必填(以英文和中文两种方式提示)-->填写英 ...
- java struts2入门学习---国际化
一.国际化的概念 1.不同国家的人访问同一个网站,显示的语言不同. 2.对JSP页面进行国际化 属性(properties)文件命名规则:基名---语言--国家如, message_zh_CN.pro ...
- java struts2入门学习--OGNL语言常用符号和常用标签学习
一.OGNL常用符号(接上一篇文章): 1.#号 1)<s:property value="#request.username"/> 作用于struts2的域对象,而不 ...
- java struts2入门学习--OGNL语言基本用法
一.知识点学习 1.struts2中包含以下6种对象,requestMap,sessionMap,applicationMap,paramtersMap,attr,valueStack; 1)requ ...
随机推荐
- js遍历Object所有属性
在js中经常需要知道Object中的所有属性及值,然而若是直接弹出Object,则是直接显示一个对象,它的属性和值没有显示出来, 不是我们想要的结果,从而需要遍历Object的所有属性. var ob ...
- [Git] Undo a commit that has already been pushed to the remote repository
If we pushed our changes already to the remote repository we have to pay attention to not change the ...
- 浅谈压缩感知(六):TVAL3
这一节主要介绍一下压缩感知中的一种基于全变分正则化的重建算法——TVAL3. 主要内容: TVAL3概要 压缩感知方法 TVAL3算法 快速哈达玛变换 实验结果 总结 1.TVAL3概要 全称: To ...
- ReSharper修改命名风格
默认情况下,ReSharper会建议你全局变量命名使用下划线开头,且第一个字母小写.否则,会给你标记出来,如下: 但我个人不喜欢这种风格,一般使用首字母大写且不带下划线,可以通过配置来调整:ReSha ...
- android中去掉ListView控件中的分割线
通过设置android:divider="@null" ,可以去掉ListView控件中的分割线 也可以自定义分割线的颜色,比如: <ListView android:id= ...
- ZH奶酪:Python中range和xrange的区别
range 函数说明:range([start,] stop[, step]),根据start与stop指定的范围以及step设定的步长,生成一个序列.range示例: >>> ...
- Word2007视频教程
超级好教程 http://v.youku.com/v_show/id_XMTAwOTgwNTIw.html 视频: oeasy教你玩转office系列之Word视频教程01 http://v.youk ...
- springboot整合mybatis的两种方式
https://blog.csdn.net/qq_32719003/article/details/72123917 springboot通过java bean集成通用mapper的两种方式 前言:公 ...
- NGUI 降低drawcall
前置说明一: Unity中的drawcall定义: 每次引擎准备数据并通知GPU的过程称为一次Draw Call. Unity(或者说基本全部图形引擎)生成一帧画面的处理过程大致能够这样简化描写叙述: ...
- Python调用Windows外部程序
在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...
