网站开发的技术简介是什么织梦做的网站在百度搜索页劫取
web/
2025/9/27 8:33:24/
文章来源:
网站开发的技术简介是什么,织梦做的网站在百度搜索页劫取,襄阳网站建设外包,门户网站什么意思来源#xff1a;http://blog.csdn.net/alex197963/article/details/2219912
在Web应用程序开发中#xff0c;除了将请求参数自动设置到Action的字段中#xff0c;我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息#xff0c;甚至需要直接对JavaSe…来源http://blog.csdn.net/alex197963/article/details/2219912
在Web应用程序开发中除了将请求参数自动设置到Action的字段中我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息甚至需要直接对JavaServlet Http的请求(HttpServletRequest)响应(HttpServletResponse)操作。 我们需要在Action中取得request请求参数username的值:
ActionContext context ActionContext.getContext();
Map params context.getParameters();
String username (String) params.get(username);ActionContext(com.opensymphony.xwork.ActionContext)是Action执行时的上下文上下文可以看作是一个容器(其实我们这里的容器就是一个Map而已)它存放的是Action在执行时需要用到的对象。 一般情况我们的ActionContext都是通过 ActionContext context (ActionContext) actionContext.get(); 来获取的详细见ActionContext.class。我们再来看看这里的actionContext对象的创建 static ThreadLocal actionContext new ActionContextThreadLocal(); ActionContextThreadLocal是实现ThreadLocal的一个内部类。ThreadLocal可以命名为“线程局部变量”它为每一个使用该变量的线程都提供一个变量值的副本使每一个线程都可以独立地改变自己的副本而不会和其它线程的副本冲突。这样我们ActionContext里的属性只会在对应的当前请求线程中可见从而保证它是线程安全的。 下面我们看看怎么通过ActionContext取得我们的HttpSession
Map session ActionContext.getContext().getSession(); ServletActionContext ServletActionContext(com.opensymphony.webwork. ServletActionContext)这个类直接继承了我们上面介绍的ActionContext它提供了直接与JavaServlet相关对象访问的功能它可以取得的对象有: 1 javax.servlet.http.HttpServletRequestHTTPservlet请求对象 2javax.servlet.http.HttpServletResponseHTTPservlet相应对象 3javax.servlet.ServletContextServlet 上下文信息 4javax.servlet.ServletConfigServlet配置对象 5javax.servlet.jsp.PageContextHttp页面上下文 下面我们看看几个简单的例子,让我们了解如何从ServletActionContext里取得JavaServlet的相关对象: 1, 取得HttpServletRequest对象:
HttpServletRequest request ServletActionContext. getRequest();2, 取得HttpSession对象:HttpSession session ServletActionContext. getRequest().getSession();ServletActionContext和ActionContext有着一些重复的功能在我们的Action中该如何去抉择呢? 我们遵循的原则是如果ActionContext能够实现我们的功能那最好就不要使用ServletActionContext让我们的Action尽量不要直接去访问JavaServlet的相关对象。
在使用ActionContext时有一点要注意不要在Action的构造函数里使用ActionContext.getContext()因为这个时候ActionContext里的一些值也许没有设置这时通过ActionContext取得的值也许是null。 如果我要取得Servlet API中的一些对象如requestresponse或session等应该怎么做这里的execute不像Struts 1.x的那样在参数中引入。开发Web应用程序当然免不了跟这些对象打交道。在Strutx 2.0你可以有两种方式获得这些对象非IoC(控制反转Inversion of Control)方式和IoC方式。
非IoC方式 要获得上述对象关键Struts 2.0中com.opensymphony.xwork2.ActionContext类。我们可以通过它的静态方法getContext()获取当前Action的上下文对象。另外org.apache.struts2.ServletActionContext 作为辅助类(Helper Class)可以帮助您快捷地获得这几个对象。
HttpServletRequest request ServletActionContext.getRequest();
HttpServletResponse response ServletActionContext.getResponse();
HttpSession session request.getSession();如果你只是想访问session的属性(Attribute)你也可以通过ActionContext.getContext().getSession() 获取或添加session范围(Scoped)的对象。 NonIoCServlet.java
package tutorial;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class NonIoCServlet extends ActionSupport {
private String message;public String getMessage() {
return message;
}Override
public String execute() {
ActionContext.getContext().getSession().put(msg, Hello World from Session!);HttpServletRequest request ServletActionContext.getRequest();
HttpServletResponse response ServletActionContext.getResponse();
HttpSession session request.getSession();StringBuffer sb new StringBuffer(Message from request: );
sb.append(request.getParameter(msg));
sb.append(brResponse Buffer Size: );
sb.append(response.getBufferSize());
sb.append(brSession ID: );
sb.append(session.getId());message sb.toString();
return SUCCESS;
}
}IoC方式 要使用IoC方式我们首先要告诉IoC容器(Container)想取得某个对象的意愿通过实现相应的接口做到这点。具体实现请参 IocServlet.java。 IoCServlet.java
package tutorial;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class IoCServlet extends ActionSupport implements SessionAware, ServletRequestAware, ServletResponseAware {
private String message;
private Map att;
private HttpServletRequest request;
private HttpServletResponse response; public String getMessage() {
return message;
}publicvoid setSession(Map att) {
this.att att;
}publicvoid setServletRequest(HttpServletRequest request) {
this.request request;
}publicvoid setServletResponse(HttpServletResponse response) {
this.response response;
}Override
public String execute() {
att.put(msg, Hello World from Session!);HttpSession session request.getSession();StringBuffer sb new StringBuffer(Message from request: );
sb.append(request.getParameter(msg));
sb.append(brResponse Buffer Size: );
sb.append(response.getBufferSize());
sb.append(brSession ID: );
sb.append(session.getId());message sb.toString();
return SUCCESS;
}
}Servlet.jsp% page contentTypetext/html; charsetUTF-8 %
% taglib prefixs uri/struts-tags%
html
head
titleHello World!/title
/head
body
h2
s:property valuemessage escapefalse/
brMessage from session: s:property value#session.msg/
/h2
/body
/html
struts.xml中NonIocServlet和IoCServlet Action的配置action nameNonIoCServlet classtutorial.NonIoCServlet
result/Servlet.jsp/result
/action
action nameIoCServlet classtutorial.IoCServlet
result/Servlet.jsp/result
/action运行Tomcat,在浏览器地址栏中键入http://localhost:8080/Struts2_Action/NonIoCServlet.action?msgHello%20World! 或http://localhost:8080/Struts2_Action/IoCServlet.action?msgHello%20World!在Servlet.jsp中,我用了两次property标志,第一次将escape设为false为了在JSP中输出br转行,第二次的value中的OGNL为#session.msg,它的作用与session.getAttribute(msg)等同.
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82655.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!