Struts2中Action访问Servlet API
2016-06-19 13:56
309 查看
Struts2的Action如何访问Servlet API呢,WEB应用中通常需要访问的Servlet API就是HttpServletRequest、HttpSession和ServletContext,这三个接口分别代表JSP内置对象中的request、session和application。下面介绍一种简单的方式访问Servlet API。即使用struts2内置静态对象ServletActionContext访问Servlet API,这个类包含了几个静态方法。
static PageContext getPageContext():取得web应用的PageContext对象
static HttpServletRequest getRequest():取得web应用的HttpServletRequest对象
static HttpServletResponse getResponse():取得web应用的HttpServletResponse对象
static ServletContext getServletContext():取得web应用的ServletContext对象
在JSP页面中新建一个form表单提交用户名密码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="<%=path %>/loginAction.action" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
新建一个Action类
<?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="userLogin" namespace="/" extends="struts-default">
<action name="loginAction" class="com.action.LoginAction">
<result>/index.jsp</result>
</action>
</package>
</struts>
这样即可在Action中获得Servlet API并通过API获得表单提交的数据
static PageContext getPageContext():取得web应用的PageContext对象
static HttpServletRequest getRequest():取得web应用的HttpServletRequest对象
static HttpServletResponse getResponse():取得web应用的HttpServletResponse对象
static ServletContext getServletContext():取得web应用的ServletContext对象
在JSP页面中新建一个form表单提交用户名密码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="<%=path %>/loginAction.action" method="post">
用户名:<input type="text" name="username"/><br>
密码:<input type="password" name="password"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
新建一个Action类
package com.action; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.jsp.PageContext; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ @Override public String execute() throws Exception { HttpServletRequest request=ServletActionContext.getRequest();//取得HttpServletRequest对象 HttpServletResponse response=ServletActionContext.getResponse();//取得HttpServletResponse对象 PageContext pageContext=ServletActionContext.getPageContext();//取得PageContext对象 ServletContext context=ServletActionContext.getServletContext();//取得ServletContext对象 HttpSession session=request.getSession();//通过上面的request获得HttpSession对象 String username=request.getParameter("username");//获得表单提交的username System.out.println(username); return SUCCESS; } }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="userLogin" namespace="/" extends="struts-default">
<action name="loginAction" class="com.action.LoginAction">
<result>/index.jsp</result>
</action>
</package>
</struts>
这样即可在Action中获得Servlet API并通过API获得表单提交的数据
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树